Greasy Fork

Greasy Fork is available in English.

链接新标签页打开❓

为同域名和跨域名链接分别设置打开行为

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         链接新标签页打开❓
// @namespace    http://tampermonkey.net/
// @version      1.41
// @description  为同域名和跨域名链接分别设置打开行为
// @author       Grey333
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// @grant        GM_registerMenuCommand
// ==/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 getSubDomainBehavior = () => GM_getValue(currentDomain + '_subDomain_behavior', 'default');
    const setSubDomainBehavior = (value) => GM_setValue(currentDomain + '_subDomain_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: 'subDomain', get: getSubDomainBehavior, set: setSubDomainBehavior },
            { 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 subDomainBehavior = getSubDomainBehavior();
        const crossDomainBehavior = getCrossDomainBehavior();

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

            try {
                // 解析完整 URL
                const absoluteURL = new URL(link.href, window.location.href);
                const linkDomain = absoluteURL.hostname;
                let behavior;

                // 判断链接类型
                if (linkDomain === currentDomain) {
                    behavior = sameDomainBehavior; // 同域名
                    console.log(`同域名链接: ${link.href} -> ${behavior}`);
                } else if (linkDomain.endsWith('.' + currentDomain) && linkDomain !== currentDomain) {
                    behavior = subDomainBehavior; // 子域名
                    console.log(`子域名链接: ${link.href} -> ${behavior}`);
                } else {
                    behavior = crossDomainBehavior; // 跨域名
                    console.log(`跨域名链接: ${link.href} -> ${behavior}`);
                }

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

                e.preventDefault(); // 阻止默认行为
                if (behavior === 'forceNewTab') {
                    window.open(absoluteURL.href, '_blank');
                } else if (behavior === 'forceSameTab') {
                    window.location.href = absoluteURL.href;
                }
            } catch (error) {
                console.error('无法解析链接:', error);
            }
        }, true); // 使用捕获阶段,确保优先级高于页面脚本
    }

    applyLinkBehavior();
})();