我如何使用 chrome 扩展内容脚本在启动时将 html 注入到页面中
我想创建一个扩展,以便在加载后立即将 html 注入到每个页面中。我熟悉manifest.json 的规则以及如何运行内容脚本。我目前遇到的问题是内容脚本在网页加载后注入 html,这有点破坏性。我想在窗口打开时立即加载它,以便注入它,然后也加载网页。你能帮忙吗?
i want to create an extension to inject html into every page as soon as it loads up. I am familiar with teh manifest.json rules for this as well as how to run a content script. The problem I'm currently having is the content script injects the html after the web page has loaded which is a bit disruptive. I would like to load it as soon as the window is open so it is injected and then the webpage loads as well. Can you help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里需要明确的是,“加载”意味着“开始加载”,对吧?当您说您熟悉manifest.json 时,您的意思是您熟悉必要的权限以及如何指定要运行的页面和脚本?因为我认为您要寻找的是manifest.json中
content_scripts
的run_at
属性:http://code.google.com/chrome/extensions/content_scripts.html#registration
这会强制您的代码在 DOM 加载之前执行每一页。请注意,这可能很复杂,因为您的扩展可能无法创建 HTML,当然,DOM 尚未加载。如果您希望脚本比其默认的
“document_idle”
早一点触发,请改用“document_end”
。Just to be clear here, "loads up" means "starts loading," right? And when you say you're familiar with the manifest.json, you mean you're familiar with the permissions necessary and how to specify which pages and which script to run? Because I think what you're looking for is the
run_at
property ofcontent_scripts
in your manifest.json:http://code.google.com/chrome/extensions/content_scripts.html#registration
That forces your code to execute before the DOM loads in every page. Note that this can be complex because your extension may fail to create HTML since, of course, the DOM hasn't yet loaded. If you'd like your script to fire just a bit earlier than its default
"document_idle"
, use"document_end"
instead.