Google Block Website Results

Adds the option to hide search results from the google search. A new block icon is visible next to the search result. When clicked you are promted to confirm the blocking of the domain. The amount of search results will not be adjusted, so if you block all websites on page 1, then page 1 will be empty. You can remove websites right now only by editing the script storage,

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

  1. // ==UserScript==
  2. // @name Google Block Website Results
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.1
  5. // @description Adds the option to hide search results from the google search. A new block icon is visible next to the search result. When clicked you are promted to confirm the blocking of the domain. The amount of search results will not be adjusted, so if you block all websites on page 1, then page 1 will be empty. You can remove websites right now only by editing the script storage,
  6. // @author LostFelix
  7. // @license MIT
  8. // @match https://www.google.com/search*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. let domainsToBlock = [];
  17. init();
  18. function init(){
  19. domainsToBlock = GM_getValue('domainsToBlock',[]);
  20. nuke();
  21. setInterval(nuke,1000);
  22. }
  23. function nuke(force = false){
  24. let search = document.querySelector('#search');
  25. if(!search){ return; }
  26.  
  27. if(force){
  28. search.querySelectorAll('.MjjYud .A6K0A .Ww4FFb a[jsname="UWckNb"][data-nuked]').forEach((el) => {
  29. delete el.dataset.nuked;
  30. });
  31. }
  32. search.querySelectorAll('.MjjYud .A6K0A .Ww4FFb a[jsname="UWckNb"]:not([data-nuked])').forEach((el) => {
  33. if(el.classList.length > 0){
  34. return;
  35. }
  36. domainsToBlock.forEach( (domain) => {
  37. if(el.href.includes(domain)){
  38. let parent = getListElParentTop(el);
  39. parent.style.opacity = 0.5;
  40. parent.style.transform = "scale(0.75)";
  41. parent.style.transformOrigin = "top left";
  42. parent.style.height = parent.getBoundingClientRect().height +"px";
  43. parent.style.display = "none";
  44. }
  45. });
  46. addBlockButton(el);
  47. el.dataset.nuked = "1";
  48. });
  49. }
  50. function addBlockButton(link){
  51. const el = getListElParentTop(link).querySelector('.D6lY4c');
  52. if(el.querySelector('.nuke-button')){
  53. return;
  54. };
  55. el.style.position = "relative";
  56. let btn = document.createElement('button');
  57. btn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" style="fill: red; height: 14px; width: 14px; margin: auto;" viewBox="0 0 512 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M367.2 412.5L99.5 144.8C77.1 176.1 64 214.5 64 256c0 106 86 192 192 192c41.5 0 79.9-13.1 111.2-35.5zm45.3-45.3C434.9 335.9 448 297.5 448 256c0-106-86-192-192-192c-41.5 0-79.9 13.1-111.2 35.5L412.5 367.2zM0 256a256 256 0 1 1 512 0A256 256 0 1 1 0 256z"/></svg>';
  58. btn.classList.add('nuke-button');
  59. btn.style.background = "transparent";
  60. btn.style.border = 0;
  61. btn.style.cursor = "pointer";
  62. btn.style.height = "100%";
  63. btn.style.padding = 0;
  64. btn.style.margin = "0 3px";
  65. btn.style.display = "inline-block";
  66. btn.addEventListener('click',(e) => {
  67. e.stopPropagation(); e.preventDefault();
  68. let href = link.href;
  69. let domain = href.replace(/^http.*\/\//,'').replace(/\/.*/,'');
  70. if(!domainsToBlock.includes(domain)){
  71. if(window.confirm("Do you want to block: "+domain)){
  72. domainsToBlock.push(domain);
  73. GM_setValue('domainsToBlock',domainsToBlock);
  74. nuke(true)
  75. }
  76. }
  77. });
  78. el.appendChild(btn);
  79. console.log(el);
  80. }
  81. function getListElParentTop(el){
  82. let parent = el.parentElement;
  83. let target = null
  84. while(parent){
  85. if(parent.classList.contains('MjjYud')){
  86. if(parent.parentElement.classList.contains('hlcw0c')){
  87. target = parent.parentElement;
  88. }else{
  89. target = parent;
  90. }
  91. parent = null;
  92. }else{
  93. parent = parent.parentElement;
  94. }
  95. }
  96. return target;
  97. }
  98.  
  99. })();