目标站点上的 tampermonkey 禁用功能

tampermonkey disable function on targeted site

如何在使用 tampermonkey 运行 之前禁用 javascript 功能,这里是一些示例:

<!doctype html>
<html>
<head>
<script>
+function() {
    function test1() {
        alert('test1');
    }
    function test2() {
        alert('test2');
    }
    function test3() {
        alert('test3');
    }
    test1();
    test2(); // how to disable this function to execute?
    test3();
}();
</script>
</head>
<body>
</body>
</html>

什么 javascript 代码可以禁用它?

Tampermonkey 用户脚本不能直接影响嵌入的 <script> 元素。

您可以尝试欺骗该脚本使用的内置 javascript 函数。但是 Tampermonkey 需要一些时间来注入用户脚本,因此 HEAD 嵌入的页面脚本可能会首先执行。

// ==UserScript==
// @name    Spoof alert()
// @run-at  document-start
// @grant   unsafeWindow
// ==/UserScript==

unsafeWindow.alert = function(){};