解除谷歌“手气不错”、知乎链接的重定向(使用Deepseek 优化)

谷歌手气不错以及知乎会阻止跳转网站,此脚本用于解决此问题。

// ==UserScript==
// @name         解除谷歌“手气不错”、知乎链接的重定向(使用Deepseek 优化)
// @name:zh-CN   解除谷歌“手气不错”、知乎链接的重定向(使用Deepseek 优化)
// @name:en-US   Solve Google I'm Feeling Lucky Redirect Problem/ Zhihu Redirect Problem
// @namespace    SolveRedirect
// @version      0.6
// @author       Bilibili Up 漫游挨踢
// @include      *google.*/*
// @include      *link.zhihu.com/*
// @grant        none
// @description:zh-cn  谷歌手气不错以及知乎会阻止跳转网站,此脚本用于解决此问题。
// @description:en-US  Google 's I'm feeling Lucky and Zhihu will cause redirect problem.This script will solve that.
// @description 谷歌手气不错以及知乎会阻止跳转网站,此脚本用于解决此问题。
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 通用URI多重解码函数
    function fullyDecodeURIComponent(str) {
        let decoded = str;
        try {
            while (true) {
                const current = decodeURIComponent(decoded);
                if (current === decoded) break;
                decoded = current;
            }
        } catch (e) {
            // 解码出错时返回当前结果
        }
        return decoded;
    }

    // 处理谷歌重定向
    if (location.hostname.includes('google.com') && location.pathname === '/url') {
        const urlParams = new URLSearchParams(location.search);
        const targetUrl = urlParams.get('q');
        if (targetUrl && (targetUrl.startsWith('http://') || targetUrl.startsWith('https://'))) {
            location.href = fullyDecodeURIComponent(targetUrl);
        }
    }

    // 处理知乎重定向
    if (location.hostname === 'link.zhihu.com') {
        const urlParams = new URLSearchParams(location.search);
        const targetUrl = urlParams.get('target');
        if (targetUrl) {
            location.href = fullyDecodeURIComponent(targetUrl);
        }
    }
})();