自定义cdn,国内/国外可以换成合适的cdn
当前为
// ==UserScript==
// @name Custom CDN URL
// @namespace https://greasyfork.org/en/scripts/473962-custom-cdn-url
// @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();
}
return url;
} 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);
}