// ==UserScript== // @name Universal Link Behavior Controller // @namespace http://tampermonkey.net/ // @version 1.2 // @description 提供优雅的图形界面控制所有网站链接的行为,并持久化保存设置 // @author Grey333 // @match *://*/* // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; const currentDomain = window.location.hostname; const getConfig = () => GM_getValue(currentDomain, 'default'); const setConfig = (value) => GM_setValue(currentDomain, value); function createGUI() { if (document.getElementById('linkControlPanel')) return; const panel = document.createElement('div'); panel.id = 'linkControlPanel'; panel.style.cssText = ` position: fixed; top: 20px; right: 20px; background: #f9f9f9; border: 1px solid #ddd; border-radius: 8px; padding: 15px; z-index: 10000; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); font-family: Arial, sans-serif; transition: opacity 0.3s ease-in-out; opacity: 0; max-width: 300px; min-width: 200px; `; const title = document.createElement('h3'); title.textContent = `链接控制 - ${currentDomain}`; title.style.margin = '0 0 10px'; panel.appendChild(title); const options = [ { label: '默认(不干预)', value: 'default' }, { label: '强制新标签页', value: 'forceNewTab' }, { label: '禁止新标签页', value: 'forceSameTab' } ]; options.forEach(opt => { const label = document.createElement('label'); label.style.display = 'block'; label.style.margin = '5px 0'; const radio = document.createElement('input'); radio.type = 'radio'; radio.name = 'linkBehavior'; radio.value = opt.value; if (getConfig() === opt.value) radio.checked = true; radio.addEventListener('change', () => { setConfig(opt.value); alert(`已为 ${currentDomain} 设置: ${opt.label}`); }); label.appendChild(radio); label.appendChild(document.createTextNode(` ${opt.label}`)); panel.appendChild(label); }); const closeBtn = document.createElement('button'); closeBtn.textContent = '关闭'; closeBtn.style.cssText = ` margin-top: 15px; padding: 5px 10px; background: #007bff; color: #fff; border: none; border-radius: 4px; cursor: pointer; `; closeBtn.addEventListener('click', () => { panel.style.opacity = '0'; setTimeout(() => panel.remove(), 300); }); panel.appendChild(closeBtn); document.body.appendChild(panel); setTimeout(() => panel.style.opacity = '1', 10); } GM_registerMenuCommand('设置链接行为', createGUI); function applyLinkBehavior() { const config = getConfig(); if (config === 'default') return; document.addEventListener('click', (e) => { const link = e.target.closest('a'); if (!link || !link.href) return; if (config === 'forceNewTab') { e.preventDefault(); window.open(link.href, '_blank'); } else if (config === 'forceSameTab') { e.preventDefault(); window.location.href = link.href; } }); } applyLinkBehavior(); })();