Greasy Fork

Greasy Fork is available in English.

记住访问过的链接

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

当前为 2024-09-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         记住访问过的链接
// @version      1.3
// @description  记住网页上访问过的链接,并将其颜色设置为浅蓝色。脚本菜单支持单独分域名启用/禁用脚本。
// @author       ChatGPT
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @run-at       document-end
// @namespace http://greasyfork.icu/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');
        const menuText = isEnabled ? '禁用记住访问过的链接脚本' : '启用记住访问过的链接脚本';
        GM_registerMenuCommand(menuText, toggleScript);
    }

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

    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);

        // 监听 DOM 变化
        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        node.querySelectorAll('a').forEach(updateLinkStatus);
                    }
                });
            });
        });

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

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

    updateMenu();

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