已访问链接样式修改(带预设和自定义)
// ==UserScript==
// @name 已访问链接样式修改(带预设和自定义)
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 让点击过的链接变色并显示下划线,支持预设样式和自定义
// @license MIT
// @author zzzwq
// @match *://*/*
// @grant GM_addStyle
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @icon https://i0.hdslb.com/bfs/face/42998afc31c623fae26bd04ee5d9e11d7b778de7.jpg
// ==/UserScript==
(function() {
'use strict';
// 预设选项
const presets = {
'红色实线': { color: '#FF0000', underline: 'solid' },
'蓝色虚线': { color: '#0000FF', underline: 'dashed' },
'绿色波浪线': { color: '#00FF00', underline: 'wavy' },
'紫色双下划线': { color: '#800080', underline: 'double' },
'橙色点状线': { color: '#FFA500', underline: 'dotted' }
};
// 默认设置
const defaultSettings = {
color: '#FF0000',
underline: 'solid'
};
// 获取当前设置
let settings = {
color: GM_getValue('linkColor', defaultSettings.color),
underline: GM_getValue('underlineType', defaultSettings.underline)
};
// 更新样式
function updateStyles() {
GM_addStyle(`
.visited-link {
color: ${settings.color} !important;
text-decoration: ${settings.underline} underline !important;
}
`);
}
// 注册设置菜单
function registerMenuCommands() {
// 添加预设选项
Object.entries(presets).forEach(([name, style]) => {
GM_registerMenuCommand(`预设:${name}`, () => {
settings.color = style.color;
settings.underline = style.underline;
GM_setValue('linkColor', style.color);
GM_setValue('underlineType', style.underline);
updateStyles();
});
});
// 自定义颜色
GM_registerMenuCommand('自定义颜色', () => {
const newColor = prompt('请输入新的链接颜色(支持颜色名称、十六进制、RGB等):', settings.color);
if (newColor) {
settings.color = newColor;
GM_setValue('linkColor', newColor);
updateStyles();
}
});
// 自定义下划线
GM_registerMenuCommand('自定义下划线', () => {
const newUnderline = prompt('请输入下划线样式(如:solid, dotted, dashed, double, wavy等):', settings.underline);
if (newUnderline) {
settings.underline = newUnderline;
GM_setValue('underlineType', newUnderline);
updateStyles();
}
});
}
// 初始化
function init() {
updateStyles();
registerMenuCommands();
// 使用Set存储已访问链接
let visitedLinks = new Set(JSON.parse(localStorage.getItem('visitedLinks')) || []);
// 初始化页面样式
function applyVisitedStyles() {
document.querySelectorAll('a').forEach(link => {
const href = link.href;
if (visitedLinks.has(href)) {
link.classList.add('visited-link');
}
});
}
// 保存到本地存储
function saveToStorage() {
localStorage.setItem('visitedLinks', JSON.stringify([...visitedLinks]));
}
// 点击事件处理
document.addEventListener('click', function(e) {
const target = e.target.closest('a');
if (target) {
const href = target.href;
if (!visitedLinks.has(href)) {
visitedLinks.add(href);
saveToStorage();
}
target.classList.add('visited-link');
}
});
// 初始应用样式
applyVisitedStyles();
// 监听DOM变化
const observer = new MutationObserver(applyVisitedStyles);
observer.observe(document.body, {
childList: true,
subtree: true
});
}
init();
})();