Files
YuerKIng/tampermonkey-emulator.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2025-10-19 13:09:47 +08:00
// 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();
}