bye-dude

百度拜拜。功能 1、搜索时排除百家号; 2、阻止搜索框占位关键词推广。 启用或者禁用百度拜拜: 1、白名单包括 放假、万年历、[快递单号] ; 2、输入框内使用快捷键 【Ctrl+K】切换启用或者禁用。

  1. // ==UserScript==
  2. // @name bye-dude
  3. // @namespace https://greasyfork.org/users/866159
  4. // @version 0.0.4
  5. // @description 百度拜拜。功能 1、搜索时排除百家号; 2、阻止搜索框占位关键词推广。 启用或者禁用百度拜拜: 1、白名单包括 放假、万年历、[快递单号] ; 2、输入框内使用快捷键 【Ctrl+K】切换启用或者禁用。
  6. // @author Song
  7. // @match *://www.baidu.com
  8. // @match *://www.baidu.com/s?*
  9. // @icon https://www.baidu.com/favicon.ico
  10. // @license MIT
  11. // @grant none
  12. // ==/UserScript==
  13. (function () {
  14. 'use strict';
  15.  
  16.  
  17. // -(site:baijiahao.baidu.com)
  18. const excludeSites = ['baijiahao.baidu.com', 'csdn.net'];
  19. const ignoreWords = ['假期', '放假', '万年历'];
  20.  
  21. /**
  22. * 常见快递单号特征:纯数字12-18位 / 字母+数字组合10-20位
  23. * @type {RegExp[]}
  24. */
  25. const expressPatterns = [
  26. /^\d{12,18}$/, // 纯数字型(如顺丰、韵达)
  27. /^[A-Za-z0-9]{10,20}$/, // 字母数字混合(如EMS、京东)
  28. /^[A-Za-z]{2}\d{9,11}$/, // 2字母开头+数字(如圆通YT开头)
  29. /^SF\d{12}$/ // 顺丰特殊格式
  30. ];
  31. const byeDu = {
  32. enabled: true,
  33. toggle() {
  34. this.setEnabled(!this.enabled)
  35. return this.enabled;
  36. },
  37. setEnabled(enabled) {
  38. this.enabled = enabled;
  39. let btn = document.querySelector('.s_btn')
  40. if (btn) {
  41. btn.style.transition = 'background-color 0.4s ease';
  42. btn.style.borderColor = btn.style.backgroundColor = enabled ? '#04AA6D' : '#3385ff';
  43. setTimeout(() => btn.value = enabled ? '百度两下' : '百度一下', 300)
  44. } else {
  45. console.warn('[bye-du]', '未找到搜索按钮')
  46. }
  47.  
  48. }
  49. }
  50.  
  51.  
  52. /**
  53. * 疑似快递单号
  54. * @param {string} num 待检测字符串
  55. * @returns {boolean} 是否为疑似快递单号
  56. */
  57. function likeExpressNumber(num) {
  58. if (num.length < 10) return false;
  59. return expressPatterns.some(pattern => pattern.test(num));
  60. }
  61.  
  62. /**
  63. *
  64. * @param {string} word
  65. * @return {boolean}
  66. */
  67. function shouldIgnore(word) {
  68. for (let w of ignoreWords) {
  69. if (word.indexOf(w) > -1) {
  70. return true;
  71. }
  72. }
  73. return likeExpressNumber(word);
  74. }
  75.  
  76.  
  77. function beforeSubmit() {
  78. const input = document.querySelector('#kw');
  79. input.addEventListener('keydown', (e) => {
  80. if (e.ctrlKey && e.key === 'k') {
  81. e.preventDefault(); // 阻止默认行为(如浏览器搜索快捷键)
  82. byeDu.toggle();
  83. console.log('Ctrl+K 被按下');
  84. // 在这里添加你的自定义逻辑
  85. }
  86. })
  87. input.addEventListener('focus', () => {
  88. let w = input.value;
  89. if (w.length > 7) {
  90. excludeSites.forEach(s => {
  91. w = w.replaceAll(` -site:${s}`, '');
  92. });
  93. console.info('focus 处理后', w)
  94. input.value = w;
  95. } else {
  96. console.info('focus value', w.length, w)
  97. }
  98. });
  99. document.querySelector('#form')
  100. .addEventListener('submit', (_e) => {
  101. const w = input.value;
  102. if (!byeDu.enabled) return;
  103. if (shouldIgnore(w)) return;
  104. const sites = excludeSites.map(s => `-site:${s}`).filter(s => w.indexOf(s) < 0);
  105. if (sites.length > 0) {
  106. input.value = w.trim() + ' ' + sites.join(' ');
  107. }
  108.  
  109. }, {capture: true});
  110. }
  111.  
  112. function clearPlace() {
  113. const input = document.querySelector('#kw');
  114. input.placeholder = '';
  115. Object.defineProperty(input, 'placeholder', {
  116. set(value) {
  117. console.info('prevent set placeholder to', value)
  118. },
  119. get() {
  120. return '';
  121. }
  122. })
  123. }
  124.  
  125. clearPlace();
  126. byeDu.setEnabled(true);
  127. beforeSubmit();
  128. })();