google_search_results_categorize

highlight Sponsored content from google search results in light red

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         google_search_results_categorize
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  highlight Sponsored content from google search results in light red
// @author       Manyu Lakhotia
// @match        https://www.google.com/search?*
// @icon         s
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  function main() {
    const domEles = Array.from(document.querySelectorAll('div')).filter(div => {
      const containsSponsoredSpan = (element, depth = 0) => {
        if (depth > 3) return false;
        if (element.tagName === 'SPAN' && element.textContent?.trim() === 'Sponsored') {
          return true;
        }
        for (const child of element.children) {
          if (containsSponsoredSpan(child, depth + 1)) {
            return true;
          }
        }
        return false;
      };
      return containsSponsoredSpan(div);
    });

    domEles.forEach(domEle => {
      domEle.style.backgroundColor = 'rgba(255, 170, 170, 0.1)';
    });
  }

  const targetNode = document.getElementsByTagName('html')[0];
  if (targetNode) {
    const observerConfig = {attributes: true, childList: true};
    const observer = new MutationObserver(main);
    observer.observe(targetNode, observerConfig);
  }
})();