简体   繁体   English

如何在加载后使用greasemonkey调用页面上的函数

[英]How can I get greasemonkey to call a function on a page after it loads

I have a very simple greasemonkey script that I want to call an already existing javascript function on the page. 我有一个非常简单的greasemonkey脚本,我想在页面上调用已经存在的javascript函数。 I've read the documentation and nothing seems to work 我已阅读文档,似乎没有任何工作

window.setTimeout(function() { 
    alert('test') // This alert works, but nothing after it does
    myFunction() // undefined
    window.myFunction() // undefined
    document.myFunction() // undefined
}, 1000);

Try using: unsafeWindow.myFunction(); 尝试使用:unsafeWindow.myFunction();

More details and info - http://wiki.greasespot.net/UnsafeWindow 更多细节和信息 - http://wiki.greasespot.net/UnsafeWindow

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. 也有unsafeWindow通过的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. unsafeWindow绕过Greasemonkey的基于XPCNativeWrapper的安全模型,该模型的存在是为了确保恶意网页不会改变对象,从而使paintmonkey脚本(以比网页中运行的普通Javascript更多的权限执行)执行其作者的操作或用户不打算。 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上的任何属性 - 特别是如果它们是针对任意网页执行的,例如具有@include *的页面,其中页面作者可能以这种方式颠覆了环境。

In other words, your script elevates the privileges available to the original page script if you use unsafeWindow. 换句话说,如果使用unsafeWindow,则脚本会提升原始页面脚本的可用权限。

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 */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM