使用 unsafeWindow 通过 Greasemonkey 插入多个 javascript 函数时出现问题

发布于 2024-11-08 19:35:24 字数 457 浏览 0 评论 0原文

问题是这些功能似乎无法互相看到。

例如:

unsafeWindow.helloworld = function() {

    alert('Hello world!');

    helloworld2();//fails with error saying it does not exist
}

unsafeWindow.helloworld2 = function() {

    alert('Hello world!2');

    helloworld();//fails with error saying it does not exist
}

insert("helloworld();"); //appends a script element to the body

这两个函数都可以使用 insert 或 firebug 控制台调用,但它们在内部并不了解彼此?这里有什么问题呢?

The problem is that the functions do not seem to be able to see each other.

For Example:

unsafeWindow.helloworld = function() {

    alert('Hello world!');

    helloworld2();//fails with error saying it does not exist
}

unsafeWindow.helloworld2 = function() {

    alert('Hello world!2');

    helloworld();//fails with error saying it does not exist
}

insert("helloworld();"); //appends a script element to the body

Both functions can be called using insert or firebug console but they do not know about each other internally? What is the problem here?

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

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

发布评论

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

评论(2

抱猫软卧 2024-11-15 19:35:25

如果您格式化代码,这将会有很大帮助:

> unsafeWindow.helloworld = function() {
> 
>     alert('Hello world!');
> 
>     // fails with error saying it does not exist
>     helloworld2(); 
> }
> 
> unsafeWindow.helloworld2 = function() {
> 
>     alert('Hello world!2');
> 
>     //fails with error saying it does not exist
>     helloworld();
> 
> }

您有无限递归函数 - helloworld 调用 helloworld2 ,它调用 helloworld ,所以 无限循环。

但无论如何,您正在设置 unsafeWindow 的属性:

unsafeWindow.helloworld = function() {

但随后尝试使用在作用域链上解析的非限定标识符来调用它:

[... later, in helloword2 ...]

      helloworld();

因此标识符 helloworld 被解析为调用 unsafeWindow.helloworld2 时创建的执行/变量对象的范围链。

因此 helloworld 被设置为 unsafeWindow属性。当函数被调用时,标识符 helloworld2 将使用函数的范围链来解析。

在浏览器中,窗口/全局对象位于作用域链上,因此可以在那里找到变量名称(即,如果在作用域链中没有更快找到变量,则变量可能会解析为全局对象的属性),但我怀疑当 unsafeWindow .helloworld 被调用,其作用域链以文档的全局对象结束,而不是 unsafeWindow。否则,对文档中函数的调用也会在其作用域链上包含 unsafeWindow ,这对我来说似乎不正确。

或者我的想法可能完全错误。 :-)

It would help a lot if you format your code:

> unsafeWindow.helloworld = function() {
> 
>     alert('Hello world!');
> 
>     // fails with error saying it does not exist
>     helloworld2(); 
> }
> 
> unsafeWindow.helloworld2 = function() {
> 
>     alert('Hello world!2');
> 
>     //fails with error saying it does not exist
>     helloworld();
> 
> }

You have in infinitely recursive funciton - helloworld calls helloworld2 which calls helloworld and so ad infinitum.

But anyway, you are setting a property of unsafeWindow:

unsafeWindow.helloworld = function() {

but then attempting to call it using an unqualified identifier that is resolved on the scope chain:

[... later, in helloword2 ...]

      helloworld();

So the identifier helloworld is resolved on the scope chain of the execution/variable object created when unsafeWindow.helloworld2 is called.

So helloworld is set as a property of unsafeWindow. When the function is called, the identifier helloworld2 will be resolved using the scope chain of the function.

In browsers, the window/global object is on the scope chain so variable names may be found there (i.e. variables may resolve to properties of the global object if not found sooner in the scope chain), but I suspect that when unsafeWindow.helloworld is called, that its scope chain ends with the document's global object, not unsafeWindow. Otherwise calls to functions in the document would also have unsafeWindow on their scope chain, which doesn't seem right to me.

Or I might be completely wrong about that. :-)

永不分离 2024-11-15 19:35:25

存在多个问题。

  1. 由于这些函数是在 unsafeWindow 范围内定义的,因此必须在 Greasemonkey 脚本中以这种方式调用它们。
    就像这样:

    unsafeWindow.helloworld = function () {
    
        警报('你好世界!');
        unsafeWindow.helloworld2();
    }
    
    unsafeWindow.helloworld2 = 函数 () {
    
        警报('你好世界!2');
        unsafeWindow.helloworld();
    }
    

  2. insert() 从哪里来?!要调用这些函数,您可以使用:

    // 警告这将启动无限循环!
    unsafeWindow.helloworld2();
    

  3. 不要这样做!以这种方式定义函数的唯一原因是覆盖/覆盖目标页面上已有的函数。在极少数情况下,当您确实想要覆盖网站的 JavaScript 时,最佳方法取决于详细信息。

    您可以使用大量的unsafeWindow转义(如图所示),或者集体重写函数,但最好的方法通常是删除页面的功能:
    (unsafeWindow.badFunc = function () {})
    并在 GM 脚本范围内替换/添加更改的功能。

  4. 但是对于当前发布的问题,您不需要执行任何操作。根据所显示的内容,脚本将是:

    helloworld = 函数 () {
    
        警报('你好世界!');
        你好世界2();
    }
    
    你好世界2=函数(){
    
        警报('你好世界!2');
        你好世界 ();
    }
    
    // 警告这将启动无限循环!
    你好世界2();
    

There are multiple problems.

  1. Since the functions are being defined in the unsafeWindow scope, they must be called that way, in the Greasemonkey script.
    Like so:

    unsafeWindow.helloworld = function () {
    
        alert ('Hello world!');
        unsafeWindow.helloworld2 ();
    }
    
    unsafeWindow.helloworld2 = function () {
    
        alert ('Hello world!2');
        unsafeWindow.helloworld ();
    }
    

  2. Where is insert() coming from?! To call these functions you would use:

    // WARNING this will launch an infinite loop!!
    unsafeWindow.helloworld2 ();
    

  3. Don't do this! The only reason to define a function that way is to override/overwrite a function that was already on the target page. In the rare cases when you DO want to override a website's JavaScript, the best method depends on the details.

    You can use lot's of unsafeWindow escaping (as shown), or rewrite functions en masse, but the best approach is often just to delete the page's function:
    (unsafeWindow.badFunc = function () {})
    and replace/add the changed features in the GM script scope.

  4. But for the question as it's currently posted, you don't need to do any of that. Based on what's shown, the script would just be:

    helloworld = function () {
    
        alert ('Hello world!');
        helloworld2 ();
    }
    
    helloworld2 = function () {
    
        alert ('Hello world!2');
        helloworld ();
    }
    
    // WARNING this will launch an infinite loop!!
    helloworld2 ();
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文