Greasy Fork is available in English.
记住网页上访问过的链接,并将其颜色设置为浅蓝色。脚本菜单支持单独分域名启用/禁用脚本。
当前为
// ==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();
}
})();