用Greasemonkey删除一个javascript函数 [英] Remove a javascript function with Greasemonkey

查看:159
本文介绍了用Greasemonkey删除一个javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我访问了一个HTML文件头部的JavaScript文件网站

 < script language =javascriptsrc = javscript.js >< /脚本> 

此文件中的代码为:

<$ (document.layers)document.captureEvents(Event.KEYPRESS)
函数更新(e){
if(document)。所有){//资源管理器
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;

我想用Greasemonkey禁用/删除/替换这个函数。



我试过它用

  unsafeWindow.update = function(){} 

code>

没有结果! (在控制台中没有错误)



有没有办法杀死这个函数?

解决方案

目前还不清楚 update 是一个全局函数。如果不是,那么这种方法将无法正常工作。



但是您可以用下面的方式覆盖按键处理程序:

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











对于一种通用的,高性能的方式来选择性地阻止或替换任何JS(在Firefox上),使用 @ run-at document-start

  // == UserScript = = 
// @name _替换在页面上选择javascript
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require https://gist.github。 com / raw / 2620135 / checkForBadJavascripts.js
// @ run-at document-start
// @grant GM_addStyle
// == / UserScript ==
/ * - 需要使用@grant指令来解决GM 1.0中引入的设计更改
。它恢复沙箱。
* /

checkForBadJavascripts([
[false,
/document\.onkeypress\s*=\s*update/,
函数(){
addJS_Node(myKeypressFunction.toString());
addJS_Node('document.onkeypress = myKeypressFunction;');
}
]
]);


函数myKeypressFunction(evt){
/ *在此处不要使用GREASEMONKEY函数,而使用
函数。
* /
console.log(Keypress function fired。);
}






查看此答案,获取有关 checkForBadJavascripts 的更多信息。


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?

解决方案

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.

这篇关于用Greasemonkey删除一个javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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