我试图为坦帕猴编写一个脚本,以防止执行特定的内联脚本标记。页面的正文如下所示
<body>
<!-- the following script tag should be executed-->
<script type="text/javascript">
alert("I'm executed as normal")
</script>
<!-- the following script tag should NOT be executed-->
<script type="text/javascript">
alert("I should not be executed")
</script>
<!-- the following script tag should be executed-->
<script type="text/javascript">
alert("I'm executed as normal, too")
</script>
</body>我试图使用我的script脚本删除跑到标记,但是如果我使用跑到 document-start或document-body,则script标记还不存在。如果我在document-end或document-idle上运行它,我想要删除的script标记将在执行document-idle脚本之前运行。
如何防止script标记的执行?
注意:是我想阻止执行的实际script标记,包含window.location = 'redirect-url'。因此,在这种情况下也足以防止重新加载。
版本:
发布于 2019-08-05 06:16:56
没有doc.write/XHR的替代版本--
(() => {
'use strict';
let needle = '/window.location/';
if ( needle === '' || needle === '{{1}}' ) {
needle = '.?';
} else if ( needle.slice(0,1) === '/' && needle.slice(-1) === '/' ) {
needle = needle.slice(1,-1);
} else {
needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
needle = new RegExp(needle);
const jsnode = () => {
try {
const jss = document.querySelectorAll('script');
for (const js of jss) {
if (js.outerHTML.match(needle)) {
js.remove();
}
}
} catch { }
};
const observer = new MutationObserver(jsnode);
observer.observe(document.documentElement, { childList: true, subtree: true });
})();发布于 2018-04-25 13:53:46
删除document-start上的脚本标记(按照wOxxOm的建议):
(function() {
'use strict';
window.stop();
const xhr = new XMLHttpRequest();
xhr.open('GET', window.location.href);
xhr.onload = () => {
var html = xhr.responseText
.replace(/<script\b[\s\S]*?<\/script>/g, s => {
// check if script tag should be replaced/deleted
if (s.includes('window.location')) {
return '';
} else {
return s;
}
});
document.open();
document.write(html);
document.close();
};
xhr.send();
})();https://stackoverflow.com/questions/50020880
复制相似问题