百度搜索自动加上 -李彦宏

在百度搜索时自动在搜索词后面加上 -李彦宏,支持地址栏搜索并避免自动触发搜索

  1. // ==UserScript==
  2. // @name 百度搜索自动加上 -李彦宏
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.9
  5. // @description 在百度搜索时自动在搜索词后面加上 -李彦宏,支持地址栏搜索并避免自动触发搜索
  6. // @author 你的名字
  7. // @match *://www.baidu.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 检查并调整 -李彦宏
  16. function checkAndAdjustLiYanhong() {
  17. const searchInput = document.getElementById('kw');
  18. if (searchInput) {
  19. let searchQuery = searchInput.value;
  20. const liYanhong = ' -李彦宏';
  21.  
  22. // 如果搜索词中还没有 -李彦宏,则加上
  23. if (!searchQuery.includes(liYanhong)) {
  24. searchQuery += liYanhong;
  25. searchInput.value = searchQuery;
  26.  
  27. // 将光标定位在 -李彦宏 前面
  28. const cursorPosition = searchQuery.length - liYanhong.length;
  29. searchInput.setSelectionRange(cursorPosition, cursorPosition);
  30. return true; // 表示进行了修改
  31. }
  32.  
  33. // 如果 -李彦宏 不在最后,则调整到前面
  34. const liYanhongIndex = searchQuery.indexOf(liYanhong);
  35. if (liYanhongIndex !== searchQuery.length - liYanhong.length) {
  36. // 提取 -李彦宏 之前和之后的内容
  37. const beforeLiYanhong = searchQuery.slice(0, liYanhongIndex);
  38. const afterLiYanhong = searchQuery.slice(liYanhongIndex + liYanhong.length);
  39.  
  40. // 将 -李彦宏 调整到前面
  41. searchQuery = beforeLiYanhong + afterLiYanhong + liYanhong;
  42. searchInput.value = searchQuery;
  43.  
  44. // 将光标定位在 -李彦宏 前面
  45. const cursorPosition = searchQuery.length - liYanhong.length;
  46. searchInput.setSelectionRange(cursorPosition, cursorPosition);
  47. return true; // 表示进行了修改
  48. }
  49. }
  50. return false; // 表示没有进行修改
  51. }
  52.  
  53. // 拦截页面内搜索
  54. function interceptSearch() {
  55. const searchForm = document.getElementById('form');
  56. if (searchForm) {
  57. searchForm.addEventListener('submit', function(event) {
  58. // 检查并调整 -李彦宏
  59. const isModified = checkAndAdjustLiYanhong();
  60.  
  61. // 如果搜索词被修改,则阻止默认行为并重新提交
  62. if (isModified) {
  63. event.preventDefault();
  64. searchForm.submit();
  65. }
  66. // 否则允许默认行为
  67. });
  68. }
  69. }
  70.  
  71. // 处理地址栏搜索
  72. function handleAddressBarSearch() {
  73. const urlParams = new URLSearchParams(window.location.search);
  74. const query = urlParams.get('wd');
  75. if (query && !query.includes(' -李彦宏')) {
  76. urlParams.set('wd', query + ' -李彦宏');
  77. window.location.search = urlParams.toString();
  78. }
  79. }
  80.  
  81. // 初始化
  82. function init() {
  83. checkAndAdjustLiYanhong(); // 检查并修正搜索词
  84. interceptSearch(); // 拦截页面内搜索
  85. handleAddressBarSearch(); // 处理地址栏搜索
  86. }
  87.  
  88. // 页面加载完成后初始化
  89. init();
  90.  
  91. // 监听动态内容变化(适用于百度搜索结果页面的动态更新)
  92. const observer = new MutationObserver(function(mutations) {
  93. mutations.forEach(function(mutation) {
  94. if (mutation.type === 'childList' && mutation.target === document.body) {
  95. // 重新初始化脚本
  96. init();
  97. }
  98. });
  99. });
  100.  
  101. // 监听 body 的子节点变化
  102. observer.observe(document.body, { childList: true });
  103. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址