Greasy Fork

Settings Tab Manager (STM)

Provides an API for other userscripts to add tabs to a site's settings menu.

目前为 2025-04-22 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.cloud/scripts/533630/1575656/Settings%20Tab%20Manager%20%28STM%29.js

// ==UserScript==
// @name         Settings Tab Manager (STM)
// @namespace    shared-settings-manager
// @version      1.1.2 // Incremented version
// @description  Provides an API for other userscripts to add tabs to a site's settings menu.
// @author       Your Name (or AI Collaborator)
// @license      MIT
// @match        https://8chan.moe/*
// @match        https://8chan.se/*
// @grant        GM_addStyle
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    // ... (Constants, State, Readiness Promise, Public API Definition, Styling remain the same) ...
    const log = (...args) => console.log(`[${MANAGER_ID}]`, ...args);
    const warn = (...args) => console.warn(`[${MANAGER_ID}]`, ...args);
    const error = (...args) => console.error(`[${MANAGER_ID}]`, ...args);
    const MANAGER_ID = 'SettingsTabManager';
    const SELECTORS = {/* ... */};
    const ACTIVE_CLASSES = {/* ... */};
    const ATTRS = {/* ... */};


    // ... (findSettingsElements, deactivateCurrentTab, activateTab remain the same) ...
    function activateTab(scriptId) { /* ... same code ... */ }
    function deactivateCurrentTab() { /* ... same code ... */ }

    /** Handles clicks within the tab container. */
    function handleTabClick(event) {
        // Find the closest ancestor tab element that was created by STM
        const clickedTab = event.target.closest(`span[${ATTRS.MANAGED}][${ATTRS.SCRIPT_ID}]`);

        if (clickedTab) {
            // REMOVED: event.stopPropagation(); - Let the event bubble up for the site's handlers
            const scriptId = clickedTab.getAttribute(ATTRS.SCRIPT_ID);
            if (scriptId) {
                activateTab(scriptId); // Call the internal activate function
            }
        } else {
             // If a non-STM tab was clicked, make sure our active tab is deactivated
             // Check if the actual clicked target or its ancestor is a site tab NOT managed by STM
             if (event.target.closest(`${SELECTORS.SITE_TAB}:not([${ATTRS.MANAGED}])`)) {
                 deactivateCurrentTab();
             }
        }
    }

    // ... (attachTabClickListener, createSeparator, createTabAndPanel, processPendingRegistrations remain the same) ...
    function createTabAndPanel(config) { /* ... same code ... */ }
    function attachTabClickListener() { /* ... same code ... */ }


    // ... (Initialization, Observer, API Implementation, Global Exposure remain the same) ...
    function initializeManager() { /* ... same code ... */ }
    const observer = new MutationObserver(/* ... */);
    // ... start observer ...
    // ... initializeManager() call ...
    // ... API Implementation functions (registerTabImpl etc.) ...
    // ... Global Exposure logic ...

})();