Undirect + Open external links in new tab

Removes this tracking and redirection from google search results

  1. // ==UserScript==
  2. // @name Undirect + Open external links in new tab
  3. // @description Removes this tracking and redirection from google search results
  4. // @namespace https://greasyfork.org/users/19952-xant1k-bt
  5. // @include /^https?.\/\/.+google[^\/]*/
  6. // @grant none
  7. // @run_at document_end
  8. // @author Steve Leigh
  9. // @version 1.1.3
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. var googlePagesPattern = /https?.\/\/.+google[^\/]*/gi;
  14. if (!document.location.href.match(googlePagesPattern))
  15. return;
  16.  
  17. var scriptToExecute = (function() {
  18. var expectedRwt = function() { return true; };
  19.  
  20. var replaceRwtFunction = function() {
  21. if (window.rwt && window.rwt != expectedRwt) {
  22. delete window.rwt;
  23. Object.defineProperty(window, 'rwt', {
  24. value: expectedRwt,
  25. writable: false
  26. });
  27. }
  28. };
  29.  
  30. replaceRwtFunction();
  31.  
  32. var timeoutId = 0;
  33. document.body.addEventListener("DOMNodeInserted", function() {
  34. if (timeoutId) clearTimeout(timeoutId);
  35. timeoutId = setTimeout(replaceRwtFunction, 1000);
  36. }, false);
  37. });
  38.  
  39. // Write script to page - since plugins often work in an isolated world, this gives us the
  40. // ability to replace javascript added by the page
  41. var fnContents = scriptToExecute.toString();
  42. var executeFnScript = '(' + fnContents + ')();';
  43.  
  44. var script = document.createElement('script');
  45. script.textContent = executeFnScript;
  46. (document.head || document.documentElement).appendChild(script);
  47. script.parentNode.removeChild(script);
  48. })();
  49. (function() {
  50. var a=0, c=document.getElementsByTagName('a');
  51. for(a; a<c.length; a++) { if (c[a].getAttribute('href') && c[a].hostname !== location.hostname) c[a].target = '_blank'; }
  52. })();