[已关闭主题记录]油猴脚本有没有可能做到使html中这段内嵌js失效
https://www.52pojie.cn/thread-1687961-1-1.html @alexPencil 对于这位大佬当时发的代码// ==UserScript==
// @name replaceJS
// @namespace http://tampermonkey.net/
// @version 0.1
// @AuThor alexPencil
// @match
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const dispatch = (script, target) => {
if(script.tagName !== "SCRIPT") {
return;
}
window.onbeforescriptexecute(script);
}
const observer = new MutationObserver(mutations => {
for(const m of mutations) {
m.addedNodes.forEach(n => {
dispatch(n, m.target);
})
}
})
observer.observe(document, {
childList: true,
subtree: true,
});
window.onbeforescriptexecute = (e) => {
if(!e.textContent) {return;}
replaceTxt(e, `window.location.href = 'x.asp';`, "console.log('alexPencil1')");
replaceTxt(e, `alert("no");`, "console.log('alexPencil2')");
replaceTxt(e, `window.stop();`, "console.log('alexPencil3')");
}
function replaceTxt(script, target, change) {
let textContent = script.textContent;
if (textContent.includes(target)) {
script.textContent = textContent.replace(target,change);
}
}})(); 本帖最后由 hrdom 于 2024-4-29 21:45 编辑
onbeforescriptexecute在chrome系的浏览器中是不支持的,仅在Firefox中支持
参考https://stackoverflow.com/questions/3972038/stop-execution-of-javascript-function-client-side-or-tweak-it 在插件的background.js里面可以做到
tab标签 loading时就注入改写方法的代码 run-at document-start 试试? 写浏览器插件吧,这个好实现点儿。
页:
[1]