Remove javascript function with Greasemonkey

I visit a website with javascript file in HTML header

<script language="javascript" src="javscript.js"></script>

Code inside this file:

// keypress management 
if (document.layers) document.captureEvents(Event.KEYPRESS)
function update(e) {        
    if (document.all) {             // Explorer
        if (event.keyCode==13) document.forms[0].submit();  // 13 = ENTER
        else if (event.keyCode==26) runHelp(hplk);          // 26 = CTRL+Z
        return;
    } else {                                                // mozilla
        if (e.which==13) document.forms[0].submit();        // 13 = ENTER
        else if (e.which==26) runHelp(hplk);                // 122 = CTRL+Z     
        return;         
    }
}
document.onkeypress=update;

I want to disable / remove / replace this function with Greasemonkey.

I tried it with

unsafeWindow.update = function(){}

no result! (there were no errors in the console)

Is there any way to kill this feature?

+5
source share
1 answer

It is not clear what updateis a global function. If this is not the case, this approach will not work.

But you can override the keystroke handler:

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



For common, powerful method for selectively blocking or replacing any JS (in Firefox) use @run-at document-startand function checkForBadJavascripts , for example:

// ==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.");
}

. , checkForBadJavascripts.

+8

All Articles