记住访问过的链接

记住网页上访问过的链接,并将其颜色设置为浅蓝色。脚本菜单支持单独域名启用/禁用脚本。

// ==UserScript==
// @name         记住访问过的链接
// @version      1.5
// @description  记住网页上访问过的链接,并将其颜色设置为浅蓝色。脚本菜单支持单独域名启用/禁用脚本。
// @author       ChatGPT
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @run-at       document-end
// @namespace https://greasyfork.org/users/452911
// ==/UserScript==

(function() {
    'use strict';

    const domain = window.location.hostname;
    const scriptKey = `scriptEnabled_${domain}`;
    let isEnabled = GM_getValue(scriptKey, true);

    function updateMenu() {
        GM_unregisterMenuCommand('toggleScriptMenuCommand');
        GM_unregisterMenuCommand('clearLinksMenuCommand');

        const toggleText = isEnabled ? '禁用记住访问过的链接脚本' : '启用记住访问过的链接脚本';
        GM_registerMenuCommand(toggleText, toggleScript);

        GM_registerMenuCommand('清除所有记住的链接', clearLinks);
    }

    function toggleScript() {
        isEnabled = !isEnabled;
        GM_setValue(scriptKey, isEnabled);
        updateMenu();
        if (isEnabled) {
            initScript();
        } else {
            removeScript();
        }
    }

    function clearLinks() {
        GM_setValue('visitedLinks', []);
        document.querySelectorAll('a[href]').forEach(link => {
            link.style.color = '';
        });
    }

    function initScript() {
        const visitedLinks = new Set(GM_getValue('visitedLinks', []));

        function updateLinkStatus(link) {
            if (visitedLinks.has(link.href)) {
                link.style.color = '#88C6E5';
            } else {
                link.addEventListener('click', () => {
                    visitedLinks.add(link.href);
                    GM_setValue('visitedLinks', Array.from(visitedLinks));
                    link.style.color = '#88C6E5';
                });
            }
        }

        document.querySelectorAll('a[href]').forEach(updateLinkStatus);

        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        node.querySelectorAll('a[href]').forEach(updateLinkStatus);
                    }
                });
            });
        });

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

    function removeScript() {
        document.querySelectorAll('a[href]').forEach(link => {
            link.style.color = '';
        });
    }

    updateMenu();

    if (isEnabled) {
        initScript();
    }
})();