移除csdn

自动从搜索结果中移除csdn, 目前支持 google, bing, baidu(回车或点击按钮触发). 有任何问题请提交反馈

  1. // ==UserScript==
  2. // @name 移除csdn
  3. // @version 0.9
  4. // @description 自动从搜索结果中移除csdn, 目前支持 google, bing, baidu(回车或点击按钮触发). 有任何问题请提交反馈
  5. // @author zhylmzr
  6. // @grant none
  7. // @run-at document-start
  8.  
  9. // @include https://www.google.*/*
  10. // @include https://*.bing.com/*
  11. // @include https://www.baidu.com/*
  12. // @namespace http://tampermonkey.net/
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. const host = location.host
  17. const params = parseUrlParams()
  18.  
  19. if (~host.indexOf('google')) {
  20. removeFromGoogle()
  21. } else if (~host.indexOf('bing')) {
  22. removeFromBing()
  23. } else if (~host.indexOf('baidu')) {
  24. removeFromBaidu()
  25. }
  26.  
  27. function removeFromBaidu() {
  28. let res = generateSearch('wd')
  29. if (res.redirect) {
  30. location.search = res.search
  31. return
  32. }
  33.  
  34. window.onload = () => {
  35. let searchInput = document.getElementById('kw')
  36. let searchSubmit = document.getElementById('su')
  37. const extraKeyword = '-csdn'
  38.  
  39. searchInput.value = searchInput.value.replace(extraKeyword, '').trim()
  40. searchInput.addEventListener('keydown', e => {
  41. if (e.key === 'Enter') {
  42. _modifyQuery()
  43. }
  44. }, true)
  45. searchSubmit.addEventListener('click', () => {
  46. _modifyQuery()
  47. }, true)
  48.  
  49. function _modifyQuery() {
  50. searchInput.value += ` ${extraKeyword}`
  51. setTimeout(() => {
  52. searchInput.value = searchInput.value.replace(extraKeyword, '').trim()
  53. })
  54. }
  55. }
  56. }
  57.  
  58. function removeFromBing() {
  59. // 判断是否使用国际版
  60. const cookies = parseCookies()
  61. const isInternaltional = 'ensearch' in params || cookies.ENSEARCH.includes('BENVER=1')
  62.  
  63. // 国际版 "NOT csdn" 排除不了
  64. const extraKeyword = isInternaltional ? 'NOT blog.csdn.net' : 'NOT csdn'
  65. let res = generateSearch('q', extraKeyword)
  66. console.log(isInternaltional, extraKeyword, res, params)
  67. if (res.redirect) {
  68. location.search = res.search
  69. return
  70. }
  71.  
  72. window.onload = () => {
  73. let searchInput = document.getElementById('sb_form_q')
  74. let searchForm = document.getElementById('sb_form')
  75.  
  76. let originFunc = Element.prototype.appendChild
  77. Element.prototype.appendChild = function (n) {
  78. originFunc.apply(this, arguments)
  79. if (n.name === 'pq') {
  80. n.value += ` ${extraKeyword}`
  81. }
  82. }
  83.  
  84. try {
  85. searchInput.value = searchInput.value.replace(extraKeyword, '').trim()
  86. searchForm.addEventListener('submit', () => {
  87. searchInput.value += ` ${extraKeyword}`
  88. }, true)
  89. } catch (e) {
  90. // DON'T HANDLER
  91. }
  92. }
  93. }
  94.  
  95. function removeFromGoogle() {
  96. let res = generateSearch(['q', 'oq'])
  97.  
  98. if (res.redirect) {
  99. location.search = res.search
  100. return
  101. }
  102. window.onload = () => {
  103. let searchInput = document.getElementsByClassName('gLFyf')[0]
  104. let searchForm = document.getElementsByClassName('tsf')[0]
  105. let searchBtn = document.getElementsByClassName('Tg7LZd')[0]
  106.  
  107. const extraKeyword = '-csdn'
  108. searchInput.value = searchInput.value.replace(extraKeyword, '').trim()
  109.  
  110. searchForm.addEventListener('submit', (e) => {
  111. e.stopPropagation
  112. _restore()
  113. }, true)
  114. searchBtn.addEventListener('click', () => {
  115. _restore()
  116. }, true)
  117.  
  118. function _restore() {
  119. searchInput.value += ` ${extraKeyword}`
  120. let hiddenInput = document.querySelector('[name=oq]')
  121. if (hiddenInput && !~hiddenInput.value.indexOf(extraKeyword)) {
  122. hiddenInput.value += ` ${extraKeyword}`
  123. }
  124. let originFunc = window.s__we
  125. window.s__we = (a, b) => {
  126. originFunc(a, b)
  127. hiddenInput = document.querySelector('[name=oq]')
  128. if (!~hiddenInput.value.indexOf(extraKeyword)) {
  129. hiddenInput.value += ` ${extraKeyword}`
  130. }
  131. }
  132. }
  133. }
  134. }
  135.  
  136. // 生成重定向url参数
  137. function generateSearch(keyNameArray = [''], extraParam = '-csdn') {
  138. if (!(keyNameArray instanceof Array)) {
  139. keyNameArray = [keyNameArray]
  140. }
  141.  
  142. let flag = false
  143. for (const k of keyNameArray) {
  144. // 如果关键字存在 并且 关键值中不包含有额外参数值
  145. if (k in params && !params[k].includes(extraParam)) {
  146. params[k] += ` ${extraParam}`
  147. flag = true
  148. }
  149. }
  150.  
  151. return {
  152. redirect: flag,
  153. search: Object.keys(params)
  154. .map(k => [k, encodeURIComponent(params[k])]) // 生成[k, encode(v)]式参数数组
  155. .map(e => e.join('=')) // 生成k=encode(v)式参数数组
  156. .join('&')
  157. }
  158. }
  159.  
  160. // 解析url的参数
  161. function parseUrlParams() {
  162. const url = new URL(location.href)
  163. const params = url.searchParams
  164. const it = params.keys()
  165.  
  166. let obj = {}
  167. let result = it.next()
  168. while (!result.done) {
  169. obj[result.value] = params.get(result.value)
  170. result = it.next()
  171. }
  172. return obj
  173. }
  174.  
  175. // 解析cookies
  176. function parseCookies() {
  177. let obj = {}
  178. document.cookie.split(";").forEach(e => {
  179. let v = e.trim()
  180. let i = v.indexOf("=")
  181. let pair = [v.substring(0, i), v.substring(i + 1, v.length)]
  182. obj[pair[0]] = pair[1]
  183. })
  184. return obj
  185. }
  186. })()