5

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

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

The code inside this file is:

// 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(){}

with no result! (got no errors in the console)

is there a way to kill this function?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
bernte
  • 1,184
  • 2
  • 19
  • 34

1 Answers1

8

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.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • I LOVE YOU BROCK ADAMS :D unsafeWindow.document.onkeypress = function(){}; works perfect :D included this part to the old script and works perfect! i hope this part is definitely finished :D i will test this next week on the terminal and let you know. than we can finish this part and drink a beer.. or two :D THANKS – bernte Mar 07 '13 at 23:42
  • can't use checkForBadJavascripts because the terminal has no internet-connection! – bernte Mar 07 '13 at 23:45
  • 1
    You're welcome! Remember, Stack Overflow questions are not about a "part" of whatever project you are working on. They are about a single, specific, programming problem. – Brock Adams Mar 07 '13 at 23:46
  • 1
    It sounds like you don't need `checkForBadJavascripts` in this case. If you did, and without an internet connection, you would just copy and paste the entire utility code into your script. It's fairly short. – Brock Adams Mar 07 '13 at 23:48
  • Sorry! I know. Believe me... this Script drives me crazy and gave me some nightmares :D – bernte Mar 07 '13 at 23:52