更好访问gcc

一点小小hook

目前為 2025-07-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         更好访问gcc
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  一点小小hook
// @author       LinXingJun
// @match        *://*.gcc.edu.cn/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    //vpn 503的时候重新刷新
    if(document.body.innerText.includes("Server is unable to process client's requests temporarily due to overload or system maintenance")){
          location.reload();
    }

    // 配置重定向规则(示例格式)
    const redirect_url = {
        'nc.gcc.edu.cn': 'nic.gcc.edu.cn',
        'www-cnki-net.vpn.gcc.edu.cn':'www-cnki-net-s.vpn.gcc.edu.cn'
    };

    // 当前页面URL替换与跳转
    function handlePageRedirect() {
        let newUrl = location.href;
        for (const [oldKey, newKey] of Object.entries(redirect_url)) {
            if (newUrl.includes(oldKey)) {
                newUrl = newUrl.replace(oldKey, newKey);
                location.replace(newUrl);
                break;
            }
        }
    }

    // 替换页面资源引用
    function replaceResourceUrls() {
        // 需要处理的标签属性
        const targets = [
            { selector: 'a', attr: 'href' },
            { selector: 'img', attr: 'src' },
            { selector: 'script', attr: 'src' },
            { selector: 'link', attr: 'href' }
        ];

        targets.forEach(target => {
            document.querySelectorAll(target.selector).forEach(el => {
                const attrValue = el.getAttribute(target.attr);
                if (attrValue) {
                    for (const [oldKey, newKey] of Object.entries(redirect_url)) {
                        if (attrValue.includes(oldKey)) {
                            el.setAttribute(
                                target.attr,
                                attrValue.replace(oldKey, newKey)
                            );
                            break;
                        }
                    }
                }
            });
        });
    }


    // 执行主流程
    handlePageRedirect();
    replaceResourceUrls();

    // 监听DOM变化处理动态内容
    const observer = new MutationObserver(() => {
        replaceResourceUrls();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址