"Nonfren Radar"

"Warns you of unfrenly people on the internet"

当前为 2023-11-28 提交的版本,查看 最新版本

// ==UserScript==
// @name          "Nonfren Radar"
// @description   "Warns you of unfrenly people on the internet"
// @include       *
// @grant         GM_getResourceText
// @grant         GM_registerMenuCommand
// @grant         GM_setValue
// @grant         GM_getValue
// @resource      theList https://files.catbox.moe/rvk3cx.json
// @homepageURL   https://frenschan.org
// @version       1488.0.1
// @license       GNU GPLv3

// @namespace https://greasyfork.org/users/1185877
// ==/UserScript==

(function () {
  var theList = JSON.parse(GM_getResourceText("theList"));
  var regexp = new RegExp("\\b(" + theList.join("|") + ")\\b(?!\\)\\))", "gi");

  // Check if the script is disabled for the current site
  var isDisabled = GM_getValue(window.location.hostname, false);

  // Add a menu command to disable the script for the current site
  GM_registerMenuCommand('Disable script for this site', function() {
    if (!isDisabled) {
      GM_setValue(window.location.hostname, true);
      console.log('Script disabled for this site');
      location.reload(); // Reload the page to apply the changes
    } else {
      console.log('Script is already disabled for this site');
    }
  });

  // Add a menu command to enable the script for the current site
  GM_registerMenuCommand('Enable script for this site', function() {
    if (isDisabled) {
      GM_setValue(window.location.hostname, false);
      console.log('Script enabled for this site');
      location.reload(); // Reload the page to apply the changes
    } else {
      console.log('Script is already enabled for this site');
    }
  });

  if (isDisabled) {
    console.log('Script is disabled for this site');
    return;
  }

  function handleText(textNode) {
    var currentNode = textNode;
    while (currentNode) {
      if (currentNode.nodeName.toLowerCase() === 'input' || currentNode.nodeName.toLowerCase() === 'textarea' || currentNode.isContentEditable) {
        return; // Skip processing if the text node is within an interactive element
      }
      currentNode = currentNode.parentNode;
    }
    textNode.nodeValue = textNode.nodeValue.replace(regexp, "((($1)))");
  }

  function processNode(node) {
    if (node.nodeType === 3) {
      handleText(node);
    } else {
      node.childNodes.forEach(processNode);
    }
  }

  // Processes the initial page content
  processNode(document.body);

  // Observes the changes in the DOM to handle dynamic content
  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      mutation.addedNodes.forEach(processNode);
    });
  });

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