google_search_results_cleanup

remove Sponsored content from google search results

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         google_search_results_cleanup
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  remove Sponsored content from google search results
// @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.remove();
    });
  }

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