简体   繁体   English

覆盖或阻止外部.js文件加载到Chrome / tampermonkey中

[英]Overriding or preventing an external .js file from loading in Chrome/tampermonkey

I have a page with the following script: 我有一个包含以下脚本的页面:

<script type="text/javascript" src="domainstuff/evil.js"></script>

It has the following thing: 它具有以下内容:

var input = ":input[type=text]";
jQuery(function(input) 
{
    jQuery(input).bind('focusout', function(e) 
    {
        //...etc
    }
}

It overrides my js: 它覆盖了我的js:

document.addEventListener("focusout", function (e)
    {
         //etc
    }

I've tried in tampermonkey 我已经尝试过tampermonkey

jQuery(input).unbind("focusout");

and also 并且

jQuery(input).off("focusout");

but I don't think I am able to target the evil.js's function. 但我认为我无法针对evil.js的功能。

I can't find the SO question again, but I read someone suggested to use 我再也找不到SO问题,但我读到有人建议使用

// @run-at document-start

and

// @run-at document-end

but both don't prevent the evil.js file from loading. 但是两者都不能阻止evil.js文件的加载。

To be honest, I've been able to block the script with uBlock Origin, and my script finally works, but I just want to know if it's possible to override a function (event?) in an external .js file. 老实说,我已经能够使用uBlock Origin阻止脚本,并且我的脚本终于可以使用,但是我只想知道是否有可能在外部.js文件中覆盖函数(事件?)。

Input in this case, is a variable, given that you're loading the JavaScript after the initial evil script: 在这种情况下,输入是一个变量,假设您要在初始邪恶脚本之后加载JavaScript:

jQuery(input).off("focusout");

will not work, but: 将不起作用,但是:

jQuery(':input[type=text').off("focusout");

Should work. 应该管用。

Now you'll have to make sure that this applies after the document is loaded and the JavaScript gets executed, so: 现在,您必须确保在加载文档并执行JavaScript之后,此操作才适用,因此:

$(document).ready(function() {
    jQuery(':input[type=text').off("focusout");
}

In case this doesn't work either, the off() is still being called before the initial eventListener gets applied, so: 如果这也不起作用,则在应用初始eventListener之前仍会调用off() ,因此:

$(document).ready(function() {
    setTimeout(function() {
        jQuery(':input[type=text').off("focusout");
    }, 500);
}

Will wait half a second after loading before removing the focusout, after which you can apply the new event listener. 加载后将等待半秒钟,然后再移除焦点,然后可以应用新的事件侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM