I have a very simple greasemonkey script that I want to call an already existing javascript function on the page. I've read the documentation and nothing seems to work
我有一个非常简单的greasemonkey脚本,我想在页面上调用已经存在的javascript函数。我已阅读文档,似乎没有任何工作
window.setTimeout(function() {
alert('test') // This alert works, but nothing after it does
myFunction() // undefined
window.myFunction() // undefined
document.myFunction() // undefined
}, 1000);
3 个解决方案
#1
25
Try using: unsafeWindow.myFunction();
尝试使用:unsafeWindow.myFunction();
More details and info - http://wiki.greasespot.net/UnsafeWindow
更多细节和信息 - http://wiki.greasespot.net/UnsafeWindow
#2
16
One way to call a function in the original page is like this:
在原始页面中调用函数的一种方法是这样的:
location.href = "javascript:void(myFunction());";
It is a bit ugly. There is also the unsafeWindow provided by GreaseMonkey too, but the authors advise against using it.
这有点难看。 GreaseMonkey也提供了不安全的窗口,但作者建议不要使用它。
unsafeWindow.myFunction();
Looks neater but make sure you understand the ramifications. From the manual:
看起来更整洁,但要确保你了解其后果。从手册:
unsafeWindow bypasses Greasemonkey's XPCNativeWrapper-based security model, which exists to make sure that malicious web pages cannot alter objects in such a way as to make greasemonkey scripts (which execute with more privileges than ordinary Javascript running in a web page) do things that their authors or users did not intend. User scripts should therefore avoid calling or in any other way depending on any properties on unsafeWindow - especally if if they are executed for arbitrary web pages, such as those with @include *, where the page authors may have subverted the environment in this way.
unsafeWindow绕过Greasemonkey的基于XPCNativeWrapper的安全模型,该模型的存在是为了确保恶意网页不会改变对象,从而使paintmonkey脚本(以比网页中运行的普通Javascript更多的权限执行)执行其作者的操作或用户不打算。因此,用户脚本应该避免调用或以任何其他方式取决于unsafeWindow上的任何属性 - 特别是如果它们是针对任意网页执行的,例如@include *,其中页面作者可能以这种方式颠覆了环境。
In other words, your script elevates the privileges available to the original page script if you use unsafeWindow.
换句话说,如果使用unsafeWindow,则脚本会提升原始页面脚本的可用权限。
#3
7
You could try using javascript event listeners.
您可以尝试使用javascript事件侦听器。
These execute code on response to object events occurring (such as page load)
这些执行代码响应发生的对象事件(例如页面加载)
For example, to execute code on page load:
例如,要在页面加载时执行代码:
window.addEventListener('load', function ()
{
/* code goes here */
}
#1
25
Try using: unsafeWindow.myFunction();
尝试使用:unsafeWindow.myFunction();
More details and info - http://wiki.greasespot.net/UnsafeWindow
更多细节和信息 - http://wiki.greasespot.net/UnsafeWindow
#2
16
One way to call a function in the original page is like this:
在原始页面中调用函数的一种方法是这样的:
location.href = "javascript:void(myFunction());";
It is a bit ugly. There is also the unsafeWindow provided by GreaseMonkey too, but the authors advise against using it.
这有点难看。 GreaseMonkey也提供了不安全的窗口,但作者建议不要使用它。
unsafeWindow.myFunction();
Looks neater but make sure you understand the ramifications. From the manual:
看起来更整洁,但要确保你了解其后果。从手册:
unsafeWindow bypasses Greasemonkey's XPCNativeWrapper-based security model, which exists to make sure that malicious web pages cannot alter objects in such a way as to make greasemonkey scripts (which execute with more privileges than ordinary Javascript running in a web page) do things that their authors or users did not intend. User scripts should therefore avoid calling or in any other way depending on any properties on unsafeWindow - especally if if they are executed for arbitrary web pages, such as those with @include *, where the page authors may have subverted the environment in this way.
unsafeWindow绕过Greasemonkey的基于XPCNativeWrapper的安全模型,该模型的存在是为了确保恶意网页不会改变对象,从而使paintmonkey脚本(以比网页中运行的普通Javascript更多的权限执行)执行其作者的操作或用户不打算。因此,用户脚本应该避免调用或以任何其他方式取决于unsafeWindow上的任何属性 - 特别是如果它们是针对任意网页执行的,例如@include *,其中页面作者可能以这种方式颠覆了环境。
In other words, your script elevates the privileges available to the original page script if you use unsafeWindow.
换句话说,如果使用unsafeWindow,则脚本会提升原始页面脚本的可用权限。
#3
7
You could try using javascript event listeners.
您可以尝试使用javascript事件侦听器。
These execute code on response to object events occurring (such as page load)
这些执行代码响应发生的对象事件(例如页面加载)
For example, to execute code on page load:
例如,要在页面加载时执行代码:
window.addEventListener('load', function ()
{
/* code goes here */
}