Custom CDN URL

自定义cdn,国内/国外可以换成合适的cdn

目前為 2023-08-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Custom CDN URL 
// @namespace    https://gf.qytechs.cn/en/users/1160175-frerenzoo
// @version      1.5.1
// @description  自定义cdn,国内/国外可以换成合适的cdn
// @author       freenzoo
// @match        https://*.bilibili.com/*
// @run-at       document-body
// @grant        unsafeWindow
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==
if ( location.href.startsWith('https://www.bilibili.com/video/') || location.href.startsWith('https://www.bilibili.com/bangumi/play/') ) {
    const cdnDomains = [
        "upos-sz-mirrorhw.bilivideo.com",
        "upos-sz-mirrorcos.bilivideo.com",
        "upos-sz-mirrorali.bilivideo.com",
        "upos-sz-mirroralib.bilivideo.com"
    ];

    function getRandomCdnDomain() {
        const randomIndex = Math.floor(Math.random() * cdnDomains.length);
        return cdnDomains[randomIndex];
    }
    function replaceP2PUrl(url) {
        try {
            const urlObj = new URL(url);
            const hostName = urlObj.hostname;
            if (urlObj.hostname.endsWith(".bilivideo.com") || urlObj.hostname.endsWith(".akamaized.net") || urlObj.hostname.endsWith(".szbdyd.com")) {
                urlObj.host = getRandomCdnDomain();
                urlObj.port = 443;
                // console.warn(`更换视频源: ${hostName} -> ${urlObj.host}`);
                return urlObj.toString();
            }
        } catch(e) {
            return url;
        }
    }

    function replaceP2PUrlDeep(obj) {
        for (const key in obj) {
            if ( key === 'baseUrl' ) {
                obj[key] = replaceP2PUrl(obj[key]);
            } else if (typeof obj[key] === 'array' || typeof obj[key] === 'object') {
                replaceP2PUrlDeep(obj[key]);
            }
        }
    }
    replaceP2PUrlDeep(unsafeWindow.__playinfo__);

    (function (open) {
        unsafeWindow.XMLHttpRequest.prototype.open = function () {
            try {
                arguments[1] = replaceP2PUrl(arguments[1]);
            } finally {
                return open.apply(this, arguments);
            }
        }
    })(unsafeWindow.XMLHttpRequest.prototype.open);
}

QingJ © 2025

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