停止使用Chrome扩展程序执行功能 [英] Stop a function from execute with Chrome extension

查看:100
本文介绍了停止使用Chrome扩展程序执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的页面:

 <!DOCTYPE HTML> 
< html lang =en>
< head>
< meta charset =UTF-8>
< title>测试页面< /标题>
< script type =text / javascript>
函数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_start document_end ,但他们都没有为我工作。

解决方案

在开发别跟踪我Google 用户脚本/扩展程序。


重要提示



Chrome内容中的窗口对象无法直接访问,
任何方式。

我测试了很多方法,而 only 可靠的方法是通过动态创建的脚本标记注入代码。查看此答案或我的扩展程序的源代码以获取更多信息。


我使用 Object.defineProperty 。使用这种方法,您可以定义一个属性,并指定关于getter,setter和property描述符的信息。在你的情况下:

$ p $ Object.defineProperty(window,'foo',{
value:function(){/ *这个函数不能被重写* /}
});

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

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




Chrome 17 错误#115452 Fixed!



在Chrome 17中,使用V8 3.7.12.12(但不是在Chrome 16中,使用V8 3.6.6.19),函数声明覆盖属性描述符

请参阅 http://jsfiddle.net/bHUag/ >
请注意,当函数声明和属性描述符方法位于同一个块中时,该错误似乎不适用。虽然这是错误的。效果不可见,因为函数声明总是在代码块之前计算。因此,首先对函数foo(){} 进行评估,然后再对其余的代码进行评估。

 <脚本> 
Object.defineProperty(window,'foo',{value:function(){return 5;}});
< / script>< script>
函数foo(){return 1};
alert(foo()); //在所有浏览器中显示5,除了Chrome v17
< / script>


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..

解决方案

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 Fixed!

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>

这篇关于停止使用Chrome扩展程序执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆