javascript 如何使用用户脚本覆盖函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21271997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:32:13  来源:igfitidea点击:

How to overwrite a function using a userscript?

javascriptuserscripts

提问by Reactormonk

I want to change parts of the webpage display, is it possible to overwrite a function with a userscript?

我想更改部分网页显示,是否可以使用用户脚本覆盖功能?

Below is the function I would like overwritten (also found Here):

下面是我想要覆盖的函数(也可以在这里找到):

function StartDrawing() {
    if ((typeof (systemsJSON) != "undefined") && (systemsJSON != null)) {
        var stellarSystemsLength = systemsJSON.length;
        $('#mapDiv').empty();
        if (stellarSystemsLength > 0) {
            InitializeRaphael();

            var i = 0;
            for (i = 0; i < stellarSystemsLength; i++){
                var stellarSystem = systemsJSON[i];
                DrawSystem(stellarSystem)
            }
        }
    }
}

This would allow me to run my own drawing algorithms.

这将允许我运行我自己的绘图算法。

How do I do this with a userscript in chromium (or Firefox)?

如何使用 Chrome(或 Firefox)中的用户脚本执行此操作?

回答by Brock Adams

See "Accessing Variables (and Functions) from Greasemonkey to Page & vice versa.

请参阅“从 Greasemonkey 到 Page访问变量(和函数),反之亦然”

Userscripts are separated from the target page, so you can't just overwrite a function or variable without understanding the context...

用户脚本与目标页面分离,因此您不能在不了解上下文的情况下仅覆盖函数或变量...

IFthe function is global(looks like it probably is in this case), you can injectyour new function definition:

如果函数是全局的(在这种情况下看起来可能是这样),您可以注入新的函数定义:

function StartDrawing () {
    // WHATEVER YOU WANT FOR THE NEW CODE GOES HERE.
}

addJS_Node (StartDrawing);

function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}


This works in almost every browser that supports userscripts.


这适用于几乎所有支持用户脚本的浏览器。

You may need to delay the load in some cases, by firing on the window loadevent and/or checking to see if the function already exists.

在某些情况下,您可能需要通过触发窗口load事件和/或检查该函数是否已存在来延迟加载。

Depending on your browser, you may have additional options (see the linked Q&A, above):

根据您的浏览器,您可能还有其他选项(请参阅上面的链接问答):

  • In Firefox+Greasemonkey, you can use @grant nonemode or unsafeWindow.
  • In Chrome+Tampermonkey, you can usually use unsafeWindow. @grant nonemode probably works too, but I haven't tested it myself.
  • Firefox+Greasemonkey 中,您可以使用@grant nonemode 或unsafeWindow.
  • Chrome+Tampermonkey 中,通常可以使用unsafeWindow. @grant none模式可能也有效,但我自己没有测试过。







If the function is NOT global:

如果函数不是全局的:

Then in Firefoxyou must overwrite the JS source as it comes in. See "Stop execution of javascript function (client side) or tweak it".

然后在Firefox 中,您必须覆盖进来的 JS 源代码。请参阅“停止执行 javascript 函数(客户端)或调整它”

In Chrome you're mostly out of luck (last verified six-ish months ago).

在 Chrome 中,你大多不走运(最后一次验证是在六个月前)。

回答by caoglish

javascript allow redefine the function if function existed. if redefine, the function will be overwritten without any warning.

如果函数存在,javascript 允许重新定义函数。如果重新定义,该函数将在没有任何警告的情况下被覆盖。

if you want to old function work as well, you can do the following way.

如果你想让旧功能也能工作,你可以按照以下方式进行。

var _oldStartDrawing =StartDrawing;
function StartDrawing() {
     _oldStartDrawing();//if you need previous function
     //extend code here;
}