如何使用用户脚本覆盖函数? [英] How to overwrite a function using a userscript?

查看:49
本文介绍了如何使用用户脚本覆盖函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

请参见访问变量(和函数>)从Greasemonkey到Page,反之亦然.

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

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

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

IF 该函数是全局的(看起来可能是这种情况),您可以

IF the function is global (looks like it probably is in this case), you can inject your 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.

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

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

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

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

  • Firefox + Greasemonkey 中,您可以使用 @grant none 模式或 unsafeWindow .
  • Chrome + Tampermonkey 中,通常可以使用 unsafeWindow . @grant none 模式可能也可以使用,但我自己尚未对其进行测试.
  • In Firefox+Greasemonkey, you can use @grant none mode or unsafeWindow.
  • In Chrome+Tampermonkey, you can usually use unsafeWindow. @grant none mode probably works too, but I haven't tested it myself.


然后在 Firefox 中,您必须覆盖JS源代码.请参见停止执行javascript功能(客户端)或对其进行调整"..

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

在Chrome浏览器中,您大多运气不佳(最近验证是在六个月前).

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

这篇关于如何使用用户脚本覆盖函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆