最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - addEventListener("beforeload") missing events - Stack Overflow

programmeradmin2浏览0评论

I'm trying to make a simple Chrome Addon to remove every event from domain x. I've looked into it and found out about the "beforeload" event listener, this is apparently what things like adblock use to block ads pletely from loading. I've implemented this into the addon and added things like console.log("addon loaded") to be sure it's actually loading the javascript, but the problem is, it only catches a handful of the events, for instance on a page of 50 events, it catches only maybe 1-2. It misses obvious other things.

twitch.js

document.addEventListener("beforeload", function(event) {
    event.preventDefault();
    $(event.target).remove();
}, true);

manifest.json

...
"content_scripts": [ {
    "js": [ "js/jquery.js", "js/twitch.js" ],
    "css": [ "css/twitch.css"],
    "matches": ["*://*/*"], 
    "run_at": "document_start",
    "all_frames" : true
}],
...

Does anyone have any ideas?

I'm trying to make a simple Chrome Addon to remove every event from domain x. I've looked into it and found out about the "beforeload" event listener, this is apparently what things like adblock use to block ads pletely from loading. I've implemented this into the addon and added things like console.log("addon loaded") to be sure it's actually loading the javascript, but the problem is, it only catches a handful of the events, for instance on a page of 50 events, it catches only maybe 1-2. It misses obvious other things.

twitch.js

document.addEventListener("beforeload", function(event) {
    event.preventDefault();
    $(event.target).remove();
}, true);

manifest.json

...
"content_scripts": [ {
    "js": [ "js/jquery.js", "js/twitch.js" ],
    "css": [ "css/twitch.css"],
    "matches": ["*://*/*"], 
    "run_at": "document_start",
    "all_frames" : true
}],
...

Does anyone have any ideas?

Share Improve this question edited Jul 8, 2012 at 20:57 Wladimir Palant 57.7k12 gold badges99 silver badges127 bronze badges asked Jul 8, 2012 at 20:04 clone1018clone1018 1,2672 gold badges11 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

beforeload is called once for each script, iframe, image and stylesheet on the page, not for every event (do you mean element?). Were you expecting other elements to raise this event?

This document, Blocking Unwanted Content, mentions a requirement:

To block content, your script must be run as a Start Script, so that it executes before the content is displayed.

So check that. Also, you don't need the .remove() line, just calling event.preventDefault() will stop the creation of the resource.

document.addEventListener( 'beforeload', function( event ) {
    event.preventDefault();
}, true );
发布评论

评论列表(0)

  1. 暂无评论