简体   繁体   English

在调用函数之前,在Greasemonkey脚本中执行代码

[英]Execute code, in a Greasemonkey script, before a function is called

I'm working on a Greasemonkey script. 我正在研究Greasemonkey脚本。 What I need to do is execute a script before a function is called, or execute the script at the beginning of the function. 我需要做的是在调用函数之前执行脚本,或者在函数开头执行脚本。

The problem is that the function is located in the document and not inside the Greasemonkey file. 问题是该函数位于文档中,而不是Greasemonkey文件中。 It will be like overwriting the function, but without overwriting it because it has to be executed after the script finished. 这就像覆盖函数一样,但不会覆盖它,因为它必须在脚本完成后执行。

Here's my full Greasemonkey code, I don't know what I'm missing: 这是我的完整Greasemonkey代码,我不知道我错过了什么:

<pre>// ==UserScript==
// @name           appname
// @version        1.0.0
// @author         me
// @description    blah
// @include        http://www.runhere.net/*
// @exclude        http://www.notinhere.com/*
// @run-at         document-end
// ==/UserScript==

function addJQuery(callback) {
    var script = document.createElement("script");
    script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
    script.addEventListener('load', function() {
        var script = document.createElement("script");
        script.textContent = "(" + callback.toString() + ")();";
        document.body.appendChild(script);
    }, false);
    document.body.appendChild(script);
}

function main() {
    var originalFunction = unsafeWindow.add_comment;
    unsafeWindow.add_comment = function(go_last_page) {
        alert("if it is shown, then works!");
        originalFunction.apply(unsafeWindow, new Array(true));
    }
}

//Load JQuery and execute function
addJQuery(main);​</pre>


The function I need to call is located in a page and is called add_comment . 我需要调用的函数位于页面中,称为add_comment It has a single parameter that is a boolean type. 它有一个布尔类型的参数。 I'm not familiar with javascript, but I need to do this simple extension. 我不熟悉javascript,但我需要做这个简单的扩展。

I'll really appreciate your help. 我真的很感谢你的帮助。

Replace the function with a wrapper function that calls your function and then the original function. 使用调用函数的包装函数替换函数,然后调用原始函数。

var originalFunction = someObject.someFunction;

someObject.someFunction = function() {

    executeMyScript();
    return originalFunction.apply(someObject, arguments);

}

You can save the function to a variable, then overwrite the function. 您可以将函数保存到变量,然后覆盖该函数。

Example: 例:

var _func = functionIWant;
functionIWant = function(){
   // Do whatever
   _func(); // Call original function
}

That code is injecting main() into the target page via the addJQuery() method. 该代码通过addJQuery()方法将main()注入目标页面。 This means that is improper to use unsafeWindow -- which will be undefined. 这意味着使用unsafeWindow是不合适的 - 这将是未定义的。

Also, you probably do not need to use .apply() in this case. 此外,在这种情况下,您可能不需要使用.apply() Lastly, the code is using a variable, go_last_page , that does not seem to be defined anywhere. 最后,代码使用的变量go_last_page似乎没有在任何地方定义。

So the code would be: 所以代码是:

function main () {
    var originalFunction    = add_comment;
    add_comment             = function (/*go_last_page*/) {
        alert ("if it is shown, then works!");
        /* This next line is probably not needed.  "this" and "arguments" are
            special JS variables.
        originalFunction.apply (this, arguments);
        */
        originalFunction (true);  //-- Simplified call is probably sufficient.
    }
}

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

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