一种选择是使用userscript:fetch当前页面,将响应文本解析为文档,删除文档中的所有<script>标签,然后使用window.open()打开一个新窗口,并填充它的<head> 和<body> 与清理后的文档的<head> 和<body>:
window.openPageWithoutScripts = async function() {
const resp = await fetch(window.location.href);
const text = await resp.text();
const doc = new DOMParser().parseFromString(text, 'text/html');
doc.querySelectorAll('script').forEach(script => script.remove());
const w = window.open();
w.document.head.innerHTML = doc.head.innerHTML;
w.document.body.innerHTML = doc.body.innerHTML;
};
然后,当您想在没有任何脚本的情况下打开当前页面时,打开控制台并输入openPageWithoutScripts()。
这会去除 <script> 标记,但不会去除内联处理程序,因为内联处理程序更难预测且更难摆脱(但幸运的是,它们是不好的做法,而且通常很少见)。
还要去除内联处理程序,创建一个包含所有可能事件的数组,然后使用这些处理程序遍历它们和 querySelectorAll 元素,然后删除该属性:
window.openPageWithoutScripts = async function() {
const resp = await fetch(window.location.href);
const text = await resp.text();
const doc = new DOMParser().parseFromString(text, 'text/html');
doc.querySelectorAll('script').forEach(script => script.remove());
const eventNames = ['click', 'load', 'error']; // etc
eventNames.forEach((e) => {
const onEventName = 'on' + e;
document.querySelectorAll(`[${onEventName}]`).forEach((elm) => {
elm.removeAttribute(onEventName);
});
});
const w = window.open();
w.document.head.innerHTML = doc.head.innerHTML;
w.document.body.innerHTML = doc.body.innerHTML;
};