DuckDuckGo 增强

屏蔽指定域名、链接不携来源、快捷回到顶部(右键两侧空白处)

스크립트 설치?
개발자의 추천 스크립트

自动无缝翻页는 어떤가요?

스크립트 설치
  1. // ==UserScript==
  2. // @name DuckDuckGo 增强
  3. // @name:zh-CN DuckDuckGo 增强
  4. // @name:zh-TW DuckDuckGo 增強
  5. // @name:en DuckDuckGo Enhancements
  6. // @version 1.0.5
  7. // @author X.I.U
  8. // @description 屏蔽指定域名、链接不携来源、快捷回到顶部(右键两侧空白处)
  9. // @description:zh-CN 屏蔽指定域名、链接不携来源、快捷回到顶部(右键两侧空白处)
  10. // @description:zh-TW 屏蔽指定域名、鏈接不攜來源、快捷回到頂部(右鍵兩側空白處)
  11. // @description:en Block the specified domain name, link without source, and quickly return to the top (the blank space on both sides of the right button)...
  12. // @match https://duckduckgo.com/*
  13. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAHAElEQVR4nJ2XX2xT9xXHP7/r6z/YceKQuihhLHZh0Ugn1a3DhjSJhE2qFDSkVEklAhXwMCL2MEEbHqpJG0Sd1D6UFjZpMHgBNAqVyJo21UB9adKHtip489YQ1mzgmzESwIQYnNiOc31/e7j2tR07QHeero/v73y/5/zOvyt4Qol1dfhyroddNkS7lIRQCCDxASBIYKBJQVQiR22Z2qHg0EjiSeyKxwJv2xhA6vukIndbgE8gEk4p2AeC57/U/i8Csa4OHyseHpRS7C/oXOvDuDd0sKI1jOpvRHF7ATBSSbLaBAuTE6Quj5C5FikSEeKIktYHgkPRxBMTiG3bGJAsfgoEAGo2baW+Zw+qv+lRzliix6eYvXCSuc+GCypNYN9cLRoVBGLbN4SkYXwABFR/E6v638bR3GJ5+uDiOTLjEfT4NHp8CgDF48XR3IK7rQNPW7tFVI9PMf3G3sJ7mlCUl4LvXY4uS6DUc++mrazc9RqK24sen2L+lobT4cRIJZm/PEoqMoIxn6wagZr2rdR3mxEzUknixwZIXRmpGokyAje2hWNAwN3Wwar+twF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MMKcFT0YSE02DOAAD/3oMoHm/xdOwqmcRMVcP2JQPoX402/tuglOkUj7dszgAIOBUciiYUAEXmjgDMjQ6T1SZQ/U3Ud5cPlvSNawDMLSb5+v7fmNfnAHC1vsCMVyHlFPz5R3Z+3+ngUqi8uuu7+6rMmdwAgAoQPB/VrveGjwrJvjvvHGD1W2ep7ezFmE8ymx/LxthXpDf+hEu3hjkdOwbAs3XPcTdzm/jLToycgVAEAsoiUN/dZ9pKJZl+Yy8AUnL0mfejGoD1puLMHQK0wqoF5jbTsLMfxeNFTGtkMuU19o+Zv3I7le8ZAnJ6DoD7NQLF46VhZz++HjOS8WMD1nK69v3Ifgu38BA8FU0IcpsBbW50mDuHD2CkktR29rL6zbPUrHmG9OwMa2u/h5TSjIphYNft2HU7gf8sEL6WoeZempX2p1j95lnL8/jx0qU0t7nUiceu5Y2/Pl5cs3WdjEzzyic/w2Ov5UDoNzz/9AYAa3es3dJrdbzs5AR3Dh8oWctzLwXfi0YfSQAgti0UkNiKHyYlazaYiVhj91Y7ahKNTzE7eLJ0sdEEuc3B8+a9P5ZAQa73ho8Iyb7Cb1drGHdbB87mFhyBlrLert+dJn0tQurKSFkPkZKjykLu0Lf6NCuVfDQOAbse926J1YQ0OK2I3JFqXn8rAhaRrpAv51K6FEQHQjwHsvzzHKEJKaI5cqO2jDG0nMdL5X/1IiQsOjJcBQAAAABJRU5ErkJggg==
  14. // @grant GM_registerMenuCommand
  15. // @grant GM_unregisterMenuCommand
  16. // @grant GM_openInTab
  17. // @grant GM_getValue
  18. // @grant GM_setValue
  19. // @grant GM_notification
  20. // @license GPL-3.0 License
  21. // @run-at document-idle
  22. // @namespace https://github.com/XIU2/UserScript
  23. // @supportURL https://github.com/XIU2/UserScript
  24. // @homepageURL https://github.com/XIU2/UserScript
  25. // ==/UserScript==
  26.  
  27. (function() {
  28. 'use strict';
  29. var menu_ALL = [
  30. ['menu_blockDomainBtn', '显示屏蔽按钮', '显示屏蔽按钮', true],
  31. ['menu_blockDomain', '编辑屏蔽域名', '编辑屏蔽域名', []],
  32. ['menu_backToTop', '快捷回到顶部', '快捷回到顶部', true]
  33. ], menu_ID = [];
  34. for (let i=0;i<menu_ALL.length;i++){if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};}
  35. registerMenuCommand();
  36.  
  37. // 注册脚本菜单
  38. function registerMenuCommand() {
  39. // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
  40. if (menu_ID.length > menu_ALL.length){for (let i=0;i<menu_ID.length;i++){GM_unregisterMenuCommand(menu_ID[i]);}}
  41. // 循环注册脚本菜单
  42. for (let i=0;i<menu_ALL.length;i++){
  43. if (menu_ALL[i][0] === 'menu_blockDomain') {
  44. menu_ID[i] = GM_registerMenuCommand(`#️⃣ ${menu_ALL[i][1]}`, function(){customBlockDomain()});
  45. } else {
  46. menu_ID[i] = GM_registerMenuCommand(`${GM_getValue(menu_ALL[i][0])?'✅':'❌'} ${menu_ALL[i][1]}`, function(){menu_switch(GM_getValue(menu_ALL[i][0]), menu_ALL[i][0], menu_ALL[i][2])});
  47. }
  48. }
  49. menu_ID[menu_ID.length] = GM_registerMenuCommand('💬 反馈 & 建议', function () {GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true}); GM_openInTab('https://greasyfork.org/zh-CN/scripts/436428/feedback', {active: true,insert: true,setParent: true});});
  50. }
  51.  
  52. // 菜单开关
  53. function menu_switch(Status, Name, Tips) {
  54. if (Status == true) {GM_setValue(Name, false); GM_notification({text: `已关闭 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});} else {GM_setValue(Name, true); GM_notification({text: `已开启 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});}
  55. registerMenuCommand();
  56. };
  57.  
  58.  
  59. document.documentElement.appendChild(document.createElement('style')).textContent = `
  60. .blockDomainBtn {padding: 0 6px !important; font-size: 12px !important; line-height: normal !important; margin-left: 6px !important; border-radius: 3px !important; vertical-align: top !important; opacity: 0.4 !important; top: 3px; cursor: cell;}
  61. li[data-layout=organic]>div[arial-label] {display: none;}
  62. a[data-testid="result-title-a"]{display: inline-block}`
  63. mutationObserver(); // 屏蔽指定域名 链接不携来源
  64. setTimeout(backToTop, 500); // 快捷回到顶部
  65.  
  66.  
  67. // 自定义屏蔽指定域名
  68. function customBlockDomain() {
  69. let nowBlockDomain = '';
  70. GM_getValue('menu_blockDomain').forEach(function(item){nowBlockDomain += '|' + item})
  71. let newBlockDomain = prompt('编辑 [屏蔽指定域名]\n(不同域名之间使用 "|" 分隔,例如:a.com|b.com|c.com )', nowBlockDomain.replace('|',''));
  72. if (newBlockDomain === '') {
  73. GM_setValue('menu_blockDomain', []);
  74. registerMenuCommand();
  75. } else if (newBlockDomain != null) {
  76. GM_setValue('menu_blockDomain', newBlockDomain.split('|'));
  77. registerMenuCommand();
  78. }
  79. }
  80.  
  81.  
  82. // 屏蔽指定域名 链接不携来源
  83. function mutationObserver() {
  84. const callback = (mutationsList, observer) => {
  85. for (const mutation of mutationsList) {
  86. for (const target of mutation.addedNodes) {
  87. if (target.nodeType != 1) break
  88. // 屏蔽指定域名
  89. if (target.tagName == 'LI' && target.dataset.layout == 'organic') {
  90. Process(target)
  91. } else if (target.tagName == 'OL' && target.className == 'react-results--main') {
  92. target.childNodes.forEach(li=>{Process(li);})
  93. }
  94. }
  95. }
  96. };
  97. const observer = new MutationObserver(callback);
  98. observer.observe(document, { childList: true, subtree: true });
  99.  
  100. function Process(target) {
  101. const a = target.querySelector('h2>a,a[data-testid=result-title-a]')
  102. if (a) {
  103. if (checkDomain(a.href.split('/')[2])) {
  104. target.hidden = true
  105. } else {
  106. // 链接不携来源
  107. addRel(target);
  108.  
  109. // 添加屏蔽按钮
  110. addBlockDomainBtn(target, a, a.href.split('/')[2]);
  111. }
  112. }
  113. }
  114. }
  115.  
  116.  
  117. // 检查域名是否存在黑名单中
  118. function checkDomain(domain) {
  119. let blockDomain = GM_getValue('menu_blockDomain');
  120. for (let i=0; i<blockDomain.length; i++) {
  121. if (domain === blockDomain[i]) return true
  122. }
  123. return false
  124. }
  125.  
  126.  
  127. // 添加 rel 属性
  128. function addRel(doc) {
  129. doc.querySelectorAll('a').forEach(function(one){one.rel = 'noreferrer noopener nofollow'})
  130. }
  131.  
  132.  
  133. // 添加屏蔽按钮
  134. function addBlockDomainBtn(doc, toElement, domain) {
  135. if (!GM_getValue('menu_blockDomainBtn')) return
  136. if (toElement && !doc.querySelector('button.blockDomainBtn')) {
  137. toElement.insertAdjacentHTML('afterend', `<button class="btn blockDomainBtn" data-domain="${domain}" title="点击在搜索结果中屏蔽 [ ${domain} ] 域名">屏蔽</button>`);
  138. doc.querySelector('button.blockDomainBtn').addEventListener('click', function(e) {
  139. e.stopPropagation();
  140. // 追加屏蔽域名
  141. let blockDomain = GM_getValue('menu_blockDomain');
  142. blockDomain.push(e.target.dataset.domain)
  143. GM_setValue('menu_blockDomain', blockDomain);
  144. // 隐藏该域名的所有搜索结果
  145. document.querySelectorAll(`button[data-domain="${e.target.dataset.domain}"].blockDomainBtn`).forEach(function(one){one.parentElement.parentElement.parentElement.parentElement.remove();})
  146. });
  147. }
  148. }
  149.  
  150.  
  151. // 快捷回到顶部(右键两侧空白处)
  152. function backToTop() {
  153. if (!GM_getValue('menu_backToTop')) return
  154. document.querySelectorAll('#react-layout>div, #react-layout>div>div, section[data-testid=sidebar]').forEach(ele => {
  155. ele.oncontextmenu = function(e) {
  156. if (e.target == this) {
  157. e.preventDefault();
  158. window.scrollTo(0,0);
  159. }
  160. }
  161. })
  162. }
  163. })();