<script>
(function() {
console.log("Domready");
// ========== 配置项:替换成你的X链接(支持模糊匹配/精准匹配) ==========
const targetScriptUrl = "//s1.hdslb.com/bfs/seed/log/meta/reporter.js";
const isExactMatch = false; // true=精准匹配整个URL;false=模糊匹配(包含即可)
// 轮询配置
const POLL_INTERVAL = 300; // 每次轮询间隔(毫秒)
const MAX_POLL_TIMES = 5; // 最大轮询次数(5×500ms=2.5秒上限)
/**
* 检测指定X链接的script标签是否存在
* @returns {boolean} true=存在;false=不存在(即模块出错)
*/
function checkScriptTagExists() {
// 1. 获取页面中所有script标签
const allScripts = document.getElementsByTagName('script');
// 2. 遍历判断是否匹配目标URL
for (let i = 0; i < allScripts.length; i++) {
const scriptSrc = allScripts[i].src || ; // 兼容无src的内联脚本
if (scriptSrc) {
// 精准匹配 / 模糊匹配
const isMatch = isExactMatch ? (scriptSrc === targetScriptUrl) : (scriptSrc.includes(targetScriptUrl));
if (isMatch) {
return true;
}
}
}
return false; // 遍历完未找到,返回不存在
}
/**
* 带轮询的脚本检测(核心新增逻辑)
* @param {number} currentTimes 当前已轮询次数
* @returns {Promise<boolean>} 最终检测结果
*/
function checkScriptTagWithPolling(currentTimes = 0) {
return new Promise((resolve) => {
// 先执行一次检测
const isExists = checkScriptTagExists();
if (isExists || currentTimes >= MAX_POLL_TIMES) {
// 找到目标脚本 或 达到最大轮询次数 → 返回最终结果
resolve(isExists);
return;
}
// 未找到且未到上限 → 延迟后继续轮询
setTimeout(() => {
checkScriptTagWithPolling(currentTimes + 1).then(resolve);
}, POLL_INTERVAL);
});
}
/**
* 执行检测并处理结果(核心逻辑)
*/
function runScriptCheck() {
// 兜底:等待DOM完全就绪(避免检测时DOM未解析完)
if (document.readyState !== 'complete') {
setTimeout(runScriptCheck, 500); // 延迟500ms重试
return;
}
// 执行带轮询的检测
checkScriptTagWithPolling().then((isScriptExists) => {
if (!isScriptExists) {
// ====== 核心判断:script标签不存在 → 判定appendChild错误发生 ======
console.warn(`检测到X链接的script标签缺失(疑似appendChild错误):${targetScriptUrl}`);
// 可选:触发修复逻辑(重新插入该script标签)
reloadMissingScript();
} else {
console.log(`X链接的script标签存在,页面加载正常:${targetScriptUrl}`);
}
});
}
/**
* 修复逻辑:重新插入缺失的script标签
*/
function reloadMissingScript() {
// 创建新的script标签
const script = document.createElement('script');
script.src = 'https://wiki.biligame.com/starengine/load.php?lang=zh-cn&modules=startup&only=scripts&raw=1&skin=vector';
// 加载成功/失败回调
script.onload = () => console.log('缺失的script标签已重新加载成功');
script.onerror = (err) => console.error('重新加载script标签失败:', err);
// 插入body(此时body已就绪,不会触发appendChild错误)
document.body.appendChild(script);
}
// 启动检测(页面加载完成后执行,避免时序问题)
window.addEventListener('load', runScriptCheck);
})();
</script>