我已经找到了一个解决方案,可以在加载此页面后立即删除网页内容。
最简单的表现是:
{ "manifest_version": 2, "name": "Remove WebPage", "description": "Remove WebPage", "version": "0.2", "content_scripts": [ { "matches": ["http://www.webdomain.net/*"], "js": ["myscript.js"] } ], "permissions": [ "tabs", "http://*/", "https://*/" ] }和myscript.js文件是
document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false); function fireContentLoadedEvent () { document.body.textContent = "No data anymore"; }它看起来很简单,但这不起作用。
I've looked for a solution to remove the content of a web page as soon as this page is loaded.
The simpliest manifest is:
{ "manifest_version": 2, "name": "Remove WebPage", "description": "Remove WebPage", "version": "0.2", "content_scripts": [ { "matches": ["http://www.webdomain.net/*"], "js": ["myscript.js"] } ], "permissions": [ "tabs", "http://*/", "https://*/" ] }and the myscript.js file is
document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false); function fireContentLoadedEvent () { document.body.textContent = "No data anymore"; }It looks simple, but that does not work.
最满意答案
正如Xan已经指出的那样,你的代码运行得太晚了。
您案例中的实际解决方案应该是:
1)将"run_at": "document_end"到清单中,如下所示:
"content_scripts": [ { "matches": ..., "js": ..., "run_at": "document_end" } ]2)删除document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false); 并使用fireContentLoadedEvent()直接调用您的函数,或者将您的代码完全移出函数。
As Xan has already pointed out, your code is running too late.
The actual solution in your case should be:
1) Add "run_at": "document_end" to your manifest like this:
"content_scripts": [ { "matches": ..., "js": ..., "run_at": "document_end" } ]2) Remove document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false); and directly call your function using fireContentLoadedEvent() or move your code out of the function altogether.
更多推荐
发布评论