Google APIs CDN Replacer

Replace ajax.googleapis.com

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google APIs CDN Replacer
// @version      1.0.0
// @description  Replace ajax.googleapis.com
// @author       ShingekiNoRex
// @match        https://ajax.googleapis.com/
// @namespace    https://github.com/justjavac/ReplaceGoogleCDN
// @run-at       document-body
// @grant        unsafeWindow
// @license MIT

// ==/UserScript==
// 判断是否在指定的网址范围内
function isSupportedURL(url) {
    return url.startsWith('https://ajax.googleapis.com/');
}
 
// 获取随机的 CDN 域名
const cdnDomains = [
  "ajax.loli.net"
];
 
const getRandomCdnDomain = () => {
  return cdnDomains[Math.floor(Math.random() * cdnDomains.length)];
};
 
// 替换 P2P URL
const replaceP2PUrl = url => {
    try {
        const urlObj = new URL(url);
        urlObj.host = cdnDomains[0];
        return urlObj.toString();
    } catch(e) {
        return url;
    }
};
 
 
// 递归替换对象中的 URL
const replaceP2PUrlDeep = obj => {
    for (const key in obj) {
        if (key === 'baseUrl' || key === 'base_url') {
            obj[key] = replaceP2PUrl(obj[key]);
        } else if (typeof obj[key] === 'array' || typeof obj[key] === 'object') {
            replaceP2PUrlDeep(obj[key]);
        }
    }
}
 
// 在合适的 URL 范围内进行操作
if (isSupportedURL(location.href)) {
    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);
}