搜索屏蔽CSDN

自动在搜索条件后面增加 -csdn, 以此屏蔽csdn网站信息。

目前为 2024-07-12 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 搜索屏蔽CSDN
  3. // @icon https://csdnimg.cn/public/favicon.ico
  4. // @namespace https://github.com/ArcherTrister
  5. // @version 0.1
  6. // @license MIT
  7. // @description 自动在搜索条件后面增加 -csdn, 以此屏蔽csdn网站信息。
  8. // @author ArcherTrister
  9. // @match *://www.baidu.com/s*
  10. // @match *://www.baidu.com/$
  11. // @match *://cn.bing.com/search*
  12. // @match *://cn.bing.com/$
  13. // @match *://www.so.com/s*
  14. // @match *://www.so.com/$
  15. // @match *://www.sogou.com/web*
  16. // @match *://www.sogou.com/$
  17. // @match *://www.google.com/search*
  18. // @match *://www.google.com/$
  19. // @grant none
  20. // ==/UserScript==
  21.  
  22. (function () {
  23. ('use strict');
  24.  
  25. let searchKeys = [];
  26. searchKeys['baidu'] = 'wd';
  27. searchKeys['bing'] = 'q';
  28. searchKeys['so'] = 'q';
  29. searchKeys['sogou'] = 'query';
  30. searchKeys['google'] = 'q';
  31.  
  32. function replaceQueryParam(url, keyWordName, sites) {
  33. const parsedUrl = new URL(url);
  34. const params = new URLSearchParams(parsedUrl.search);
  35. const keyWord = params.get(keyWordName);
  36. if (!keyWord || keyWord.indexOf('-site:') != -1)
  37. {
  38. return;
  39. }
  40.  
  41. const newValue = keyWord + sites;
  42. params.set(keyWordName, newValue);
  43. parsedUrl.search = params.toString();
  44. window.location.href = parsedUrl.toString();
  45. }
  46.  
  47. var wordInput;
  48. var key;
  49. //baidu
  50. if (
  51. window.location.hostname == 'www.baidu.com' &&
  52. window.location.pathname == '/s'
  53. ) {
  54. wordInput = document.getElementById('kw');
  55. key = "baidu";
  56. }
  57. //bing
  58. else if (
  59. window.location.hostname == 'cn.bing.com' &&
  60. window.location.pathname == '/search'
  61. ) {
  62. wordInput = document.getElementById('sb_form_q');
  63. key = "bing";
  64. }
  65. //360 so
  66. else if (
  67. window.location.hostname == 'www.so.com' &&
  68. window.location.pathname == '/s'
  69. ) {
  70. wordInput = document.getElementById('keyword');
  71. key = "so";
  72. }
  73. //sogou
  74. else if (
  75. window.location.hostname == 'www.sogou.com' &&
  76. window.location.pathname == '/web'
  77. ) {
  78. wordInput = document.getElementById('upquery');
  79. key = "sogou";
  80. }
  81. //google
  82. else {
  83. wordInput = document.getElementsByClassName('gLFyf')[0];
  84. key = "google";
  85. }
  86.  
  87. if (wordInput) {
  88. wordInput.addEventListener('keydown', function (e) {
  89. console.info("keydown value", this.value);
  90. if (
  91. e.key == 'Enter' &&
  92. this.value.length > 0 &&
  93. this.value.indexOf('.csdn.') == -1
  94. ) {
  95. console.info("location", window.location);
  96. this.value += ' -site:*.csdn.net -site:*.csdn.com';
  97. }
  98. });
  99. wordInput.addEventListener('blur', function () {
  100. if (
  101. this.value.length > 0 &&
  102. this.value.indexOf('.csdn.') == -1
  103. ) {
  104. this.value += ' -site:*.csdn.net -site:*.csdn.com';
  105. }
  106. });
  107. wordInput.addEventListener('focus', function () {
  108. if (this.value.indexOf('.csdn.net') != -1) {
  109. this.value = this.value.substring(
  110. 0,
  111. this.value.indexOf(' -site:*.csdn.net -site:*.csdn.com')
  112. );
  113. }
  114. });
  115. }
  116. replaceQueryParam(window.location.href, searchKeys[key], ' -site:*.csdn.net -site:*.csdn.com');
  117. })();