chrome扩展页面修改onload(chrome extension page modification onload)

编程入门 行业动态 更新时间:2024-10-07 13:24:47
chrome扩展页面修改onload(chrome extension page modification onload)

我已经找到了一个解决方案,可以在加载此页面后立即删除网页内容。

最简单的表现是:

{ "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.

更多推荐

本文发布于:2023-07-09 10:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   onload   chrome   modification   page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!