Files
xyzw_web_helper/tampermonkey-emulator.js
2025-10-17 20:56:50 +08:00

40 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// tampermonkey-emulator.js
// 模拟 GM_addStyle
window.GM_addStyle = function(css) {
const style = document.createElement('style');
style.textContent = css;
document.head.appendChild(style);
};
// 模拟 GM_xmlhttpRequest
window.GM_xmlhttpRequest = function(opts) {
fetch(opts.url, {
method: opts.method || 'GET',
headers: opts.headers || {},
body: opts.data || null,
mode: 'cors',
credentials: 'include'
}).then(res => res.text()).then(text => {
if (opts.onload) opts.onload({ status: 200, responseText: text });
}).catch(err => {
if (opts.onerror) opts.onerror(err);
});
};
// 模拟 unsafeWindow直接指向 window
window.unsafeWindow = window;
// 模拟 @run-at document-end
function runUserScript1() {
// 这里动态加载原始脚本
const script = document.createElement('script');
script.src = '/auto.js'; // 原始脚本,不改
document.head.appendChild(script);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', runUserScript1);
} else {
runUserScript1();
}