使用 unsafeWindow 通过 Greasemonkey 插入多个 javascript 函数时出现问题
问题是这些功能似乎无法互相看到。
例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您格式化代码,这将会有很大帮助:
您有无限递归函数 - helloworld 调用 helloworld2 ,它调用 helloworld ,所以 无限循环。。
但无论如何,您正在设置
unsafeWindow
的属性:但随后尝试使用在作用域链上解析的非限定标识符来调用它:
因此标识符
helloworld
被解析为调用 unsafeWindow.helloworld2 时创建的执行/变量对象的范围链。因此
helloworld
被设置为unsafeWindow
的属性。当函数被调用时,标识符helloworld2
将使用函数的范围链来解析。在浏览器中,窗口/全局对象位于作用域链上,因此可以在那里找到变量名称(即,如果在作用域链中没有更快找到变量,则变量可能会解析为全局对象的属性),但我怀疑当
unsafeWindow .helloworld
被调用,其作用域链以文档的全局对象结束,而不是unsafeWindow
。否则,对文档中函数的调用也会在其作用域链上包含unsafeWindow
,这对我来说似乎不正确。或者我的想法可能完全错误。 :-)
It would help a lot if you format your code:
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
:but then attempting to call it using an unqualified identifier that is resolved on the scope chain:
So the identifier
helloworld
is resolved on the scope chain of the execution/variable object created whenunsafeWindow.helloworld2
is called.So
helloworld
is set as a property ofunsafeWindow
. When the function is called, the identifierhelloworld2
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, notunsafeWindow
. Otherwise calls to functions in the document would also haveunsafeWindow
on their scope chain, which doesn't seem right to me.Or I might be completely wrong about that. :-)
存在多个问题。
由于这些函数是在
unsafeWindow
范围内定义的,因此必须在 Greasemonkey 脚本中以这种方式调用它们。就像这样:
insert()
从哪里来?!要调用这些函数,您可以使用:不要这样做!以这种方式定义函数的唯一原因是覆盖/覆盖目标页面上已有的函数。在极少数情况下,当您确实想要覆盖网站的 JavaScript 时,最佳方法取决于详细信息。
您可以使用大量的
unsafeWindow
转义(如图所示),或者集体重写函数,但最好的方法通常是删除页面的功能:(
unsafeWindow.badFunc = function () {}
)并在 GM 脚本范围内替换/添加更改的功能。
但是对于当前发布的问题,您不需要执行任何操作。根据所显示的内容,脚本将是:
There are multiple problems.
Since the functions are being defined in the
unsafeWindow
scope, they must be called that way, in the Greasemonkey script.Like so:
Where is
insert()
coming from?! To call these functions you would use: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.
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: