google_search_results_categorize

highlight Sponsored content from google search results in light red

  1. // ==UserScript==
  2. // @name google_search_results_categorize
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description highlight Sponsored content from google search results in light red
  6. // @author Manyu Lakhotia
  7. // @match https://www.google.com/search?*
  8. // @icon s
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. function main() {
  17. const domEles = Array.from(document.querySelectorAll('div')).filter(div => {
  18. const containsSponsoredSpan = (element, depth = 0) => {
  19. if (depth > 3) return false;
  20. if (element.tagName === 'SPAN' && element.textContent?.trim() === 'Sponsored') {
  21. return true;
  22. }
  23. for (const child of element.children) {
  24. if (containsSponsoredSpan(child, depth + 1)) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. };
  30. return containsSponsoredSpan(div);
  31. });
  32.  
  33. domEles.forEach(domEle => {
  34. domEle.style.backgroundColor = 'rgba(255, 170, 170, 0.1)';
  35. });
  36. }
  37.  
  38. const targetNode = document.getElementsByTagName('html')[0];
  39. if (targetNode) {
  40. const observerConfig = {attributes: true, childList: true};
  41. const observer = new MutationObserver(main);
  42. observer.observe(targetNode, observerConfig);
  43. }
  44. })();