Remove a javascript function with Greasemonkey

前端 未结 1 1983
日久生厌
日久生厌 2020-12-06 07:41

I visit a website with a javascript file in the head of the HTML


相关标签:
1条回答
  • 2020-12-06 08:34

    It's not clear that update is a global function. If it isn't then that approach won't work.

    But you can override the keypress handler with:

    unsafeWindow.document.onkeypress = function(){};
    



    For a general, high-powered way to selectively block, or replace, any JS (on Firefox), use @run-at document-start and the checkForBadJavascripts function, like so:

    // ==UserScript==
    // @name        _Replace select javascript on a page
    // @include     http://YOUR_SERVER.COM/YOUR_PATH/*
    // @require     https://gist.github.com/raw/2620135/checkForBadJavascripts.js
    // @run-at      document-start
    // @grant       GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    checkForBadJavascripts ( [
        [   false,
            /document\.onkeypress\s*=\s*update/,
            function () {
                addJS_Node (myKeypressFunction.toString() );
                addJS_Node ('document.onkeypress = myKeypressFunction;');
            }
        ]
    ] );
    
    
    function myKeypressFunction (evt) {
        /*  DO WHATEVER HERE BUT USE NO GREASEMONKEY FUNCTIONS INSIDE
            THIS FUNCTION.
        */
        console.log ("Keypress function fired.");
    }
    

    See this answer, for more information on checkForBadJavascripts.

    0 讨论(0)
提交回复
热议问题