|

// ==UserScript==
// @name GitHub镜像自动跳转
// @Namespace http://tampermonkey.net/
// @Version 1.0
// @description 自动将github.com网址跳转到bgithub.xyz镜像站
// @author GitHub镜像
// @match *://github.com/*
// @match *://www.github.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const targetHostname = "github.com";
const mirrorHostname = "bgithub.xyz";
// 立即重定向当前页面
if (window.location.hostname === targetHostname) {
const newUrl = window.location.href.replace(
new RegExp(`^(https?://)${targetHostname.replace(/\./g, '\\.')}`, 'i'),
`$1${mirrorHostname}`
);
// 避免无限循环
if (newUrl !== window.location.href) {
window.stop(); // 停止当前页面加载
window.location.replace(newUrl);
}
return;
}
// 修改页面中的GitHub链接(防止点击时再跳转)
function modifyPageLinks() {
document.querySelectorAll('a').forEach(link => {
try {
const url = new URL(link.href);
if (url.hostname === targetHostname) {
url.hostname = mirrorHostname;
link.href = url.toString();
}
} catch(e) {
// 忽略无效URL
}
});
}
// 监听DOM变化以处理动态加载的链接
const observer = new MutationObserver(modifyPageLinks);
observer.observe(document, {
childList: true,
subtree: true
});
// 初始修改
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', modifyPageLinks);
} else {
modifyPageLinks();
}
})();
|
|