我如何使用 chrome 扩展内容脚本在启动时将 html 注入到页面中

发布于 2024-10-29 02:24:24 字数 139 浏览 6 评论 0原文

我想创建一个扩展,以便在加载后立即将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-11-05 02:24:24

在这里需要明确的是,“加载”意味着“开始加载”,对吧?当您说您熟悉manifest.json 时,您的意思是您熟悉必要的权限以及如何指定要运行的页面和脚本?因为我认为您要寻找的是manifest.json中content_scriptsrun_at属性:

http://code.google.com/chrome/extensions/content_scripts.html#registration

{
  // other stuff
  "content_scripts": [{
    "matches": ["http://*/*"],
    "js": ["content.js"],
    "run_at": "document_start"
  }],
  // other stuff
}

这会强制您的代码在 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 of content_scripts in your manifest.json:

http://code.google.com/chrome/extensions/content_scripts.html#registration

{
  // other stuff
  "content_scripts": [{
    "matches": ["http://*/*"],
    "js": ["content.js"],
    "run_at": "document_start"
  }],
  // other stuff
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文