使用 Chrome 扩展停止执行函数

发布于 2025-01-07 17:25:03 字数 765 浏览 0 评论 0原文

这是一个简单的页面:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test page</title>
    <script type="text/javascript">
        function foo (num) {
            alert(num);
        }
    </script>
</head>
<body>
    Hello World
    <script type="text/javascript">
        foo(2);
    </script>
</body>
</html>  

我想编写一个 Chrome 扩展程序来阻止执行底部脚本(foo(2))。
我尝试编写一个内容脚本,删除最后一个脚本标签:

document.body.removeChild(document.body.lastChild);  

但它不起作用。

我认为这可能是因为内容脚本在最后一个脚本行执行后运行。然后我尝试将 run_at 设置为 document_startdocument_end,但它们都不适合我。

Here is a simple page:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test page</title>
    <script type="text/javascript">
        function foo (num) {
            alert(num);
        }
    </script>
</head>
<body>
    Hello World
    <script type="text/javascript">
        foo(2);
    </script>
</body>
</html>  

I'd like to write a Chrome extension to prevent the execute of the bottom script(foo(2)).
I tried to write a content script which removes the last script tag with:

document.body.removeChild(document.body.lastChild);  

but it does not work.

I think this may be because the content script runs after the last script line has executed. then I tried to set the run_at to document_start or document_end, but none of them work for me..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2025-01-14 17:25:03

我在开发 Don 的过程中遇到了同样的问题't track me Google 用户脚本/扩展程序。

#重要说明
无法以任何方式直接访问 Chrome 内容脚本中的 window 对象。
我测试了很多方法,唯一可靠的方法是通过动态创建的脚本标签注入代码。看看这个答案,或我的扩展的 源代码了解更多信息。

我通过使用 Object.定义属性。使用此方法,您可以定义属性,并指定有关 getter、setter 和属性描述符的信息。在您的情况下:

Object.defineProperty(window, 'foo', {
    value: function(){/*This function cannot be overridden*/}
});

或者,如果您想捕获变量并稍后使用它:

(function() {
    var originalFoo = function(){/*Default*/};
    Object.defineProperty(window, 'foo', {
        get: function(){
            if (confirm('function logic')) return function(){/*dummy*/};
            else return originalFoo;
        },
        set: function(fn){originalFoo = fn;}
    });
})();

##Bug in Chrome 17 [Bug #115452](http://code.google.com/p/chromium/issues/detail?id=115452) [Fixed!](http://code.google.com/p/chromium/issues/detail?id=115452#hc4)
In Chrome 17, using V8 3.7.12.12 (but not in Chrome 16, using V8 3.6.6.19), **Function declarations override the property descriptors**.
See http://jsfiddle.net/bHUag/
Note that this bug *seems* to not be applied when the function declaration and property descriptor method are in the same block. This is false, though. The effect is not visible, because function declarations are always evaluated before the code block. So, `function foo(){}` is evaluated first, then the rest of the code.

<script>
Object.defineProperty(window, 'foo', {value: function(){return 5;} });
</script><script>
function foo(){return 1};
alert(foo()); // Shows 5 in all browsers except for Chrome v17
</script>

I faced the same problem during development of the Don't track me Google User script / extension.

#Important note
The window object in a Chrome contentscript cannot be accessed directly, in any way.
I have tested many methods, and the only reliable method is injecting the code through a dynamically created script tag. Have a look at this answer, or my extension's source code for more information.

I solved it by using Object.defineProperty. With this method, you can define a property, and specify information about the getter, setter and property descriptors. In your case:

Object.defineProperty(window, 'foo', {
    value: function(){/*This function cannot be overridden*/}
});

Or, if you want to capture the variable, and use it later:

(function() {
    var originalFoo = function(){/*Default*/};
    Object.defineProperty(window, 'foo', {
        get: function(){
            if (confirm('function logic')) return function(){/*dummy*/};
            else return originalFoo;
        },
        set: function(fn){originalFoo = fn;}
    });
})();

##Bug in Chrome 17 [Bug #115452](http://code.google.com/p/chromium/issues/detail?id=115452) [Fixed!](http://code.google.com/p/chromium/issues/detail?id=115452#hc4)
In Chrome 17, using V8 3.7.12.12 (but not in Chrome 16, using V8 3.6.6.19), **Function declarations override the property descriptors**.
See http://jsfiddle.net/bHUag/
Note that this bug *seems* to not be applied when the function declaration and property descriptor method are in the same block. This is false, though. The effect is not visible, because function declarations are always evaluated before the code block. So, `function foo(){}` is evaluated first, then the rest of the code.

<script>
Object.defineProperty(window, 'foo', {value: function(){return 5;} });
</script><script>
function foo(){return 1};
alert(foo()); // Shows 5 in all browsers except for Chrome v17
</script>
无人接听 2025-01-14 17:25:03

我认为值得一提的是相对较新的元数据项 // @unwrap ,它将用户脚本从用户脚本通常运行的沙箱中取出。更多信息:

https://wiki.greasespot.net/Metadata_Block#.40unwrap

I think it's worth mentioning the relatively new metadata item // @unwrap which takes the userscript out of the sandbox that userscripts normally run in. More info:

https://wiki.greasespot.net/Metadata_Block#.40unwrap

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文