Greasy Fork

Greasy Fork is available in English.

链接新标签页打开❓

提供优雅的图形界面控制所有网站链接的行为,并持久化保存设置

当前为 2025-03-03 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         链接新标签页打开❓
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  提供优雅的图形界面控制所有网站链接的行为,并持久化保存设置
// @author       Grey333
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @license      MIT
// ==/UserScript==



(function() {
    'use strict';

    const currentDomain = window.location.hostname;

    // 获取和设置行为
    const getSameDomainBehavior = () => GM_getValue(currentDomain + '_sameDomain_behavior', 'default');
    const setSameDomainBehavior = (value) => GM_setValue(currentDomain + '_sameDomain_behavior', value);
    const getCrossDomainBehavior = () => GM_getValue(currentDomain + '_crossDomain_behavior', 'default');
    const setCrossDomainBehavior = (value) => GM_setValue(currentDomain + '_crossDomain_behavior', 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: 350px; min-width: 250px;
        `;

        const title = document.createElement('h3');
        title.textContent = `链接控制 - ${currentDomain}`;
        title.style.margin = '0 0 10px';
        panel.appendChild(title);

        // 选项组
        const options = [
            { group: '同域名链接', key: 'sameDomain', get: getSameDomainBehavior, set: setSameDomainBehavior },
            { group: '跨域名链接', key: 'crossDomain', get: getCrossDomainBehavior, set: setCrossDomainBehavior }
        ];

        options.forEach(({ group, key, get, set }) => {
            const groupTitle = document.createElement('h4');
            groupTitle.textContent = group;
            groupTitle.style.margin = '10px 0 5px';
            panel.appendChild(groupTitle);

            const behaviorOptions = [
                { label: '默认(不干预)', value: 'default' },
                { label: '强制新标签页', value: 'forceNewTab' },
                { label: '禁止新标签页', value: 'forceSameTab' }
            ];

            behaviorOptions.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 = `${key}_behavior`;
                radio.value = opt.value;
                if (get() === opt.value) radio.checked = true;

                radio.addEventListener('change', () => {
                    set(opt.value);
                    alert(`已为 ${group} 设置: ${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 sameDomainBehavior = getSameDomainBehavior();
        const crossDomainBehavior = getCrossDomainBehavior();

        document.addEventListener('click', (e) => {
            const link = e.target.closest('a');
            if (!link || !link.href) return;

            try {
                const linkDomain = new URL(link.href).hostname;
                const isSameDomain = linkDomain === currentDomain;
                const behavior = isSameDomain ? sameDomainBehavior : crossDomainBehavior;

                if (behavior === 'default') return;

                if (behavior === 'forceNewTab') {
                    e.preventDefault();
                    window.open(link.href, '_blank');
                } else if (behavior === 'forceSameTab') {
                    e.preventDefault();
                    window.location.href = link.href;
                }
            } catch (error) {
                console.error('无法解析链接域名:', error);
            }
        });
    }

    applyLinkBehavior();
})();