No More Search Links in Reddit Comments

Strips Reddit-injected comment search links and spyglass icons, but leaves user-added links untouched.

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         No More Search Links in Reddit Comments
// @namespace    https://greasyfork.org/en/users/971411-xcloudx01
// @version      1.1
// @description  Strips Reddit-injected comment search links and spyglass icons, but leaves user-added links untouched.
// @author       xcloudx01
// @match        https://www.reddit.com/*
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(() => {
  /**
   * Remove a Reddit-injected auto-search link, preserving text only.
   * @param {HTMLAnchorElement} node
   */
  function stripSearchInjection(node) {
    if (!node || node.tagName !== 'A') return;

    const textOnly = Array.from(node.childNodes)
      .filter(n => n.nodeType === Node.TEXT_NODE)
      .map(n => n.textContent)
      .join(' ')
      .replace(/\s+/g, ' ')
      .trim();

    node.replaceWith(document.createTextNode(textOnly));
    console.log('[NoMoreSearchLinks] Removed:', textOnly);
  }

  /**
   * Scan for Reddit auto-injected comment links marked by the spyglass icon.
   * @param {ParentNode} root
   */
  function cleanse(root) {
    const injectedSearchLinks = root.querySelectorAll(
      'a[href^="/search/"] svg[icon-name="search-outline"]'
    );

    injectedSearchLinks.forEach(svg => {
      const anchor = svg.closest('a[href^="/search/"]');
      if (anchor) stripSearchInjection(anchor);
    });
  }

  function init() {
    cleanse(document);

    const observer = new MutationObserver(mutations => {
      for (const m of mutations) {
        for (const node of m.addedNodes) {
          if (node.nodeType === Node.ELEMENT_NODE) {
            cleanse(node);
          }
        }
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true,
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();