【发布时间】:2022-10-07 00:49:08
【问题描述】:
我需要登录一个强制使用 IE 的网页。
该页面类似于以下代码。
<Script Language=\"javascript\">
alert(\"Please use IE to login!\");
window.opener = null;
window.close();
</Script>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<body>
...
</body>
</html>
我想使用 Chrome 登录,所以我必须阻止这个脚本关闭窗口。 我曾尝试使用 Tampermonkey 脚本,但我的脚本总是比页面脚本执行得晚。
元数据// @run-at document-start 似乎不起作用。
下面是我尝试过的代码。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://localhost:8080/666/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=undefined.localhost
// @grant none
// @run-at document-start
// ==/UserScript==
// reference: https://github.com/jspenguin2017/Snippets/blob/master/onbeforescriptexecute.html
(function() {
\'use strict\';
const Event = class {
constructor(script, target) {
this.script = script;
this.target = target;
this._cancel = false;
this._replace = null;
this._stop = false;
}
preventDefault() {
this._cancel = true;
}
stopPropagation() {
this._stop = true;
}
replacePayload(payload) {
this._replace = payload;
}
};
let callbacks = [];
window.addBeforeScriptExecuteListener = (f) => {
if (typeof f !== \"function\") {
throw new Error(\"Event handler must be a function.\");
}
callbacks.push(f);
};
window.removeBeforeScriptExecuteListener = (f) => {
let i = callbacks.length;
while (i--) {
if (callbacks[i] === f) {
callbacks.splice(i, 1);
}
}
};
const dispatch = (script, target) => {
if (script.tagName !== \"SCRIPT\") {
return;
}
const e = new Event(script, target);
if (typeof window.onbeforescriptexecute === \"function\") {
try {
window.onbeforescriptexecute(e);
} catch (err) {
console.error(err);
}
}else{
console.log(\"window.onbeforescriptexecute no defined\");
}
for (const func of callbacks) {
if (e._stop) {
break;
}
try {
func(e);
} catch (err) {
console.error(err);
}
}
if (e._cancel) {
script.textContent = \"\";
script.remove();
} else if (typeof e._replace === \"string\") {
script.textContent = e._replace;
}
};
const observer = new MutationObserver((mutations) => {
window.close = ()=>{return;};
for (const m of mutations) {
for (const n of m.addedNodes) {
dispatch(n, m.target);
}
}
});
observer.observe(document, {
childList: true,
subtree: true,
});
//example
(() => {
\"use strict\";
window.onbeforescriptexecute = (e) => {
// You should check if textContent exists as this property is
// buggy sometimes
if (!e.script.textContent) {
return;
}
// Prevent execution of a script
if (e.script.textContent.includes(\"window.close()\")) {
e.preventDefault();
//e.stopPropagation();
}
// Change the code that runs
if (e.script.textContent.includes(\"console.log\")) {
// Original payload is e.script.textContent, you can
// manipulate it however you want, just pass the final
// payload to e.replacePayload when you are done
e.replacePayload(\"console.log(2);\");
// Later event handlers can override your payload, you
// can call e.stopPropagation to make sure the current
// payload is applied
}
};
})();
})();
标签: javascript google-chrome tampermonkey