Duck Hide Sites

Hide specific sites from DuckDuckGo search results

  1. // ==UserScript==
  2. // @name Duck Hide Sites
  3. // @namespace IzzySoft
  4. // @description Hide specific sites from DuckDuckGo search results
  5. // @include https://duckduckgo.com/*
  6. // @include http://duckduckgo.com/*
  7. // @version 1
  8. // @grant none
  9. // ==/UserScript==
  10. // For our eventListener, FF complains "Use of Mutation Events is deprecated. Use MutationObserver instead." So alternatively:
  11. // For Ajax reloads (paging), see solution at [Fire Greasemonkey script on AJAX request](http://stackoverflow.com/q/8281441/2533433)
  12. // and [waitForKeyElements](https://greasyfork.org/en/scripts/6250-waitforkeyelements) (JQuery required)
  13.  
  14. var unwanted = ['experts-exchange.com'];
  15. var unwantedWild = [];
  16. var debug = false;
  17.  
  18. function getElementsByXpath(path, doc=document, ptype=XPathResult.ORDERED_NODE_SNAPSHOT_TYPE) {
  19. return document.evaluate(path,doc,null,ptype,null)
  20. }
  21. function dlog(msg) {
  22. if (debug) console.log(msg);
  23. }
  24.  
  25. function hideResults() {
  26. var res = getElementsByXpath('//div[contains(@class,"results_links_deep")]',document);
  27. if (res.snapshotLength > 0) for ( i=0; i < res.snapshotLength; i++ ) {
  28. var link = getElementsByXpath('//a[@class="result__a"]',res.snapshotItem(i));
  29. if (link.snapshotLength>0) {
  30. if ( unwanted.indexOf(link.snapshotItem(i).hostname) > -1 ) {
  31. dlog('Removed result for: '+link.snapshotItem(i).hostname + ' ('+link.snapshotItem(i).getAttribute('href')+')');
  32. res.snapshotItem(i).style.display = 'none';
  33. }
  34. if (unwantedWild.length>0) for (var wild in unwantedWild) {
  35. var reg = new RegExp(unwantedWild[wild],'i');
  36. if ( link.snapshotItem(i).hostname.match(reg) ) {
  37. dlog('Removed wild result ('+unwantedWild[wild]+') for: '+link.snapshotItem(i).hostname + ' ('+link.snapshotItem(i).getAttribute('href')+')');
  38. res.snapshotItem(i).style.display = 'none';
  39. }
  40. }
  41. }
  42. }
  43. }
  44.  
  45. //window.addEventListener('load', hideResults, false); // only run when page is loaded completely
  46. document.addEventListener("DOMNodeInserted", hideResults, false); // Works on initial and additional (paged) results, as both are loaded via Ajax