javascript - 在我无法控制的 HTML 页面上更改 Javascript

标签 javascript greasemonkey

<分区>

我正在使用我无法控制的 HTML 页面。它在内联中定义了一个 Javascript 函数 <script>标记并在 <body onload="..."> 中调用它:

<html>
...
<body onload="init()">
<script type="text/javascript" language="javascript">
    function init() {
        ...
    }
</script>
...

如何在函数被调用之前更改它?我试过使用 Greasemonkey 修改脚本或在它之后插入另一个脚本来覆盖函数,但它似乎没有任何效果。

最佳答案

Greasemonkey 现在通常可以通过利用 the beforescriptexecute event 来做这种事情和 @run-at document-start .请注意,似乎只有 Firefox 支持该事件,因此这在 Chrome 上不起作用。参见 herehere更乏味的方法。

改变init()在调用函数之前,利用 checkForBadJavascripts()下面定义的函数。

你可以这样调用它:

//--- New "init" function to replace the bad one.
function init () {
    //... Do what you want here...
}

checkForBadJavascripts ( [
    [false, /function\s+init(/, function () {addJS_Node (init);} ]
] );

在哪里function\s+init(对于 <script> 必须是唯一的您定位的标签。 (注意 addJS_Node() 也在下面定义。)


例如访问this page at jsBin .你会看到 3 行文本,其中两行是 JS 添加的。

现在,安装以下脚本并重新访问页面。你会看到 GM 脚本删除了一个坏的 <script>标记并用我们的“好”JS 替换另一个。

// ==UserScript==
// @name        _Replace evil Javascript
// @include     http://output.jsbin.com/tezoni*
// @run-at      document-start
// @grant       none
// ==/UserScript==

/****** New "init" function that we will use
    instead of the old, bad "init" function.
*/
function init () {
    var newParagraph            = document.createElement ('p');
    newParagraph.textContent    = "I was added by the new, good init() function!";
    document.body.appendChild (newParagraph);
}

/*--- Check for bad scripts to intercept and specify any actions to take.
*/
checkForBadJavascripts ( [
    [false, /old, evil init()/, function () {addJS_Node (init);} ],
    [true,  /evilExternalJS/i,  null ]
] );

function checkForBadJavascripts (controlArray) {
    /*--- Note that this is a self-initializing function.  The controlArray
        parameter is only active for the FIRST call.  After that, it is an
        event listener.

        The control array row is  defines like so:
        [bSearchSrcAttr, identifyingRegex, callbackFunction]
        Where:
            bSearchSrcAttr      True to search the SRC attribute of a script tag
                                false to search the TEXT content of a script tag.
            identifyingRegex    A valid regular expression that should be unique
                                to that particular script tag.
            callbackFunction    An optional function to execute when the script is
                                found.  Use null if not needed.
    */
    if ( ! controlArray.length) return null;

    checkForBadJavascripts      = function (zEvent) {

        for (var J = controlArray.length - 1;  J >= 0;  --J) {
            var bSearchSrcAttr      = controlArray[J][0];
            var identifyingRegex    = controlArray[J][1];

            if (bSearchSrcAttr) {
                if (identifyingRegex.test (zEvent.target.src) ) {
                    stopBadJavascript (J);
                    return false;
                }
            }
            else {
                if (identifyingRegex.test (zEvent.target.textContent) ) {
                    stopBadJavascript (J);
                    return false;
                }
            }
        }

        function stopBadJavascript (controlIndex) {
            zEvent.stopPropagation ();
            zEvent.preventDefault ();

            var callbackFunction    = controlArray[J][2];
            if (typeof callbackFunction == "function")
                callbackFunction ();

            //--- Remove the node just to clear clutter from Firebug inspection.
            zEvent.target.parentNode.removeChild (zEvent.target);

            //--- Script is intercepted, remove it from the list.
            controlArray.splice (J, 1);
            if ( ! controlArray.length) {
                //--- All done, remove the listener.
                window.removeEventListener (
                    'beforescriptexecute', checkForBadJavascripts, true
                );
            }
        }
    }

    /*--- Use the "beforescriptexecute" event to monitor scipts as they are loaded.
        See https://developer.mozilla.org/en/DOM/element.onbeforescriptexecute
        Note that it does not work on acripts that are dynamically created.
    */
    window.addEventListener ('beforescriptexecute', checkForBadJavascripts, true);

    return checkForBadJavascripts;
}

function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    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;
    //--- Don't error check here. if DOM not available, should throw error.
    targ.appendChild (scriptNode);
}

关于javascript - 在我无法控制的 HTML 页面上更改 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472569/

相关文章:

javascript - 使用 Tampermonkey 更改网站文本

javascript - 网站如何区分是通过Web浏览器还是App进入网站?

javascript - 将鼠标悬停效果添加到样式中

javascript - Github 从注销重定向到登录页面而不是主页

jquery - @require-ing jQuery 覆盖页面的 $ 变量

javascript - 我正在尝试向 js 对象添加一个元素

javascript - Framework7 在页面之间将对象作为参数传递

javascript - 为什么此脚本不适用于连续的页面点击?

javascript - xpath - 限制搜索到节点不工作?

javascript - 如何用 GreaseMonkey 替换 onclick 事件?