Google Block Words

Block words from Google Search

  1. // ==UserScript==
  2. // @name Google Block Words
  3. // @version 1.1
  4. // @description Block words from Google Search
  5. // @author Drazen Bjelovuk
  6. // @include *://*.google.tld/*
  7. // @grant none
  8. // @noframes
  9. // @namespace https://greatest.deepsurf.us/users/11679
  10. // @contributionURL https://goo.gl/dYIygm
  11. // ==/UserScript==
  12.  
  13. var bwWindow, keywords;
  14.  
  15. var oldSubmit = document.getElementById('tsf').onsubmit;
  16. document.getElementById('tsf').onsubmit = function() {
  17. var keywords = JSON.parse(localStorage.getItem('blockedWords'));
  18. if (keywords) {
  19. var blockString = ' ';
  20. for (var i = 0; i < keywords.length; i++)
  21. blockString += '-"' + keywords[i] + '" ';
  22. document.getElementById('lst-ib').value += blockString;
  23. }
  24. oldSubmit();
  25. };
  26.  
  27. // Create and add window
  28. bwWindow = document.createElement('div');
  29. bwWindow.id = 'bwWindow';
  30. bwWindow.setAttribute('style', 'display:none; position:fixed; left:50%; top:70px; width:400px; max-height:400px; overflow-y:auto; overflow-x:hidden; margin-left:-200px; text-align:center; background-color:white; z-index:99999; border:solid 1px; padding-bottom:30px');
  31.  
  32. var innerHTML = '<h2 style="margin-bottom:25px">Blocked Words</h2>';
  33. keywords = JSON.parse(localStorage.getItem('blockedWords'));
  34. if (keywords) {
  35. for (var i = 0; i < keywords.length; i++)
  36. innerHTML += '<div style="margin-bottom:5px"><input type="text" style="width:250px; margin-right:10px" value="'+keywords[i]+'" disabled><button class="removeButton" data-index="'+i+'" style="width:65px">Remove</button></div>';
  37. }
  38. innerHTML += '<div><input id="addWord" type="text" style="width:250px; margin-right:10px"><button id="addButton" style="width:65px">Add</button></div>';
  39. bwWindow.innerHTML = innerHTML;
  40. bwWindow.onclick = function(event) { event.stopPropagation(); };
  41. document.body.appendChild(bwWindow);
  42.  
  43. document.addEventListener('click', function() {
  44. bwWindow.style.display = 'none';
  45. });
  46.  
  47. // Create and add buttons for opening window
  48. if (window.location.href.indexOf('search') > -1) {
  49. if (keywords) {
  50. var srchBox = document.getElementById('lst-ib');
  51. var blockedIndex = srchBox.value.indexOf('-"'+keywords[0]);
  52. if (blockedIndex > -1)
  53. srchBox.value = srchBox.value.slice(0, blockedIndex - 1);
  54. }
  55. var keywordBtn = document.createElement('a');
  56. keywordBtn.className = 'ab_button';
  57. keywordBtn.textContent = 'Blocked Words';
  58. keywordBtn.setAttribute('style', 'position:absolute; right:-140px; bottom:0; cursor:pointer; text-decoration:none; padding: 7px 17px;');
  59. keywordBtn.addEventListener('click', openBWWindow);
  60. document.getElementById('tsf').appendChild(keywordBtn);
  61. }
  62. else {
  63. var keywordBtn = document.createElement('input');
  64. keywordBtn.id = 'homeBWButton';
  65. keywordBtn.type = 'button';
  66. keywordBtn.className = 'gbqfba';
  67. keywordBtn.value = 'Blocked Words';
  68. var css = document.createElement("style");
  69. css.type = "text/css";
  70. css.innerHTML = "#homeBWButton { color:#757575 !important; height:36px; font-size:13px; padding:0 16px; border:1px solid #f2f2f2; } " +
  71. "#homeBWButton:hover { color:#222 !important; border:1px solid #c6c6c6 } " +
  72. "#homeBWButton:focus { border:1px solid #4d90fe };";
  73. document.head.appendChild(css);
  74. keywordBtn.addEventListener('click', openBWWindow);
  75. document.getElementsByClassName('jsb')[0].firstChild.appendChild(keywordBtn);
  76. }
  77.  
  78. // Events
  79. bwWindow.addEventListener('click', function(event) {
  80. if (event.target.id === 'addButton')
  81. addBW(event.target);
  82. else if (event.target.className === 'removeButton')
  83. removeBW(event.target);
  84. });
  85.  
  86. bwWindow.addEventListener('keyup', function(event) {
  87. if (event.keyCode === 13 && event.target.id === 'addWord')
  88. document.getElementById('addButton').click();
  89. });
  90.  
  91. function addBW(addButton) {
  92. var addInput = document.getElementById('addWord');
  93. if (addInput.value) {
  94. keywords = JSON.parse(localStorage.getItem('blockedWords'));
  95. if (keywords)
  96. keywords.push(addInput.value);
  97. else
  98. keywords = [addInput.value];
  99. localStorage.setItem('blockedWords', JSON.stringify(keywords));
  100. var newItem = document.createElement('div');
  101. newItem.style.marginBottom = '5px';
  102. newItem.innerHTML = '<input type="text" style="width:250px; margin-right:10px" value="'+ addInput.value +'" disabled><button class="removeButton" data-index="'+ (keywords.length - 1) +'" style="width:65px">Remove</button>';
  103. bwWindow.insertBefore(newItem, addButton.parentNode);
  104. addInput.value = "";
  105. }
  106. }
  107.  
  108. function removeBW(removeButton) {
  109. keywords = JSON.parse(localStorage.getItem('blockedWords'));
  110. var currIndex = removeButton.dataset.index;
  111. keywords.splice(currIndex, 1);
  112. var nextSibling = removeButton.parentNode.nextSibling;
  113. while (nextSibling) {
  114. nextSibling.childNodes[1].dataset.index = currIndex;
  115. nextSibling = nextSibling.nextSibling;
  116. currIndex++;
  117. }
  118. localStorage.setItem('blockedWords', JSON.stringify(keywords));
  119. removeButton.parentNode.remove();
  120. }
  121.  
  122. function openBWWindow(event) {
  123. event.stopPropagation();
  124. bwWindow.style.display = 'block';
  125. }