☰

JanitorAI Nag Eater

Hides JanitorAI Plus subscription promotional popups.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         JanitorAI Nag Eater
// @namespace    https://github.com/transientclover-ui/janitorai-nag-eater
// @version      0.2.1
// @description  Hides JanitorAI Plus subscription promotional popups.
// @author       JanitorAI Nag Eater contributors
// @license      MIT
// @match        https://janitorai.com/*
// @run-at       document-start
// @grant        GM_registerMenuCommand
// ==/UserScript==

(() => {
  'use strict';

  const PLUS_SURFACE_SELECTOR =
    '[data-modal-open="true"][class*="_plusSurface_"]';
  const MODAL_OVERLAY_SELECTOR = '[class*="_modalOverlay_"]';
  const SUPPRESSED_ATTRIBUTE = 'data-jne-plus-promotion';
  const suppressedSurfaces = new WeakSet();
  let suppressedCount = 0;

  const style = document.createElement('style');
  style.textContent = `
    ${PLUS_SURFACE_SELECTOR},
    ${MODAL_OVERLAY_SELECTOR}:has(${PLUS_SURFACE_SELECTOR}),
    [${SUPPRESSED_ATTRIBUTE}] {
      display: none !important;
    }
  `;
  (document.head || document.documentElement).append(style);

  function suppressSurface(surface) {
    if (!suppressedSurfaces.has(surface)) {
      suppressedSurfaces.add(surface);
      suppressedCount += 1;
    }

    const overlay = surface.closest(MODAL_OVERLAY_SELECTOR);
    if (!overlay) {
      return;
    }

    overlay.setAttribute(SUPPRESSED_ATTRIBUTE, '');
    overlay.setAttribute('aria-hidden', 'true');
    overlay.inert = true;
  }

  function scan(root) {
    if (!(root instanceof Element)) {
      return;
    }

    if (root.matches(PLUS_SURFACE_SELECTOR)) {
      suppressSurface(root);
    }

    for (const surface of root.querySelectorAll(PLUS_SURFACE_SELECTOR)) {
      suppressSurface(surface);
    }
  }

  GM_registerMenuCommand('Show suppressed promotion count', () => {
    const noun = suppressedCount === 1 ? 'element' : 'elements';
    alert(
      `JanitorAI Nag Eater suppressed ${suppressedCount} promotional ${noun} on this page.`,
    );
  });

  scan(document.documentElement);

  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      for (const node of mutation.addedNodes) {
        scan(node);
      }
    }
  });

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