通用中转链接跳转器

支持搜狗/CSDN/知乎等平台的中转链接跳转

// ==UserScript==
// @name         通用中转链接跳转器
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  支持搜狗/CSDN/知乎等平台的中转链接跳转
// @author       Yutes
// @match        *://*.sogou.com/web/*
// @match        *://link.csdn.net/*
// @match        *://link.zhihu.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 站点配置中心(可自由扩展)
    const SITE_CONFIG = {
        // 搜狗搜索
        'm.sogou.com': {
            param: 'url',
            decode: true,
            pattern: /url=(.+?)(&|$)/i
        },
        // CSDN链接
        'link.csdn.net': {
            param: 'target',
            decode: true,
            pattern: /target=(.+?)(&|$)/i
        },
        // 知乎外链
        'link.zhihu.com': {
            param: 'target',
            decode: true,
            pattern: /target=(.+?)(&|$)/i,
            fixEncoding: true
        }
    };

    // 获取当前网站配置
    const currentHost = location.hostname;
    const config = SITE_CONFIG[currentHost];

    if (!config) return;

    // 解析查询参数
    const queryParams = new URLSearchParams(location.search);

    if (queryParams.has(config.param)) {
        let targetUrl = queryParams.get(config.param);

        // 执行URL解码
        if (config.decode) {
            targetUrl = decodeURIComponent(targetUrl);
        }

        // 处理知乎的特殊编码(将"//"前的":"编码修正)
        if (config.fixEncoding) {
            targetUrl = targetUrl.replace(/(https?):\/\//, (m, p1) => {
                return p1 + '://';
            });
        }

        // 立即跳转(替换历史记录)
        window.location.replace(targetUrl);
    }
})();