我如何使用铬扩展内容脚本在启动时将html注入页面 [英] how can i use a chrome extension content script to inject html into a page at startup

查看:156
本文介绍了我如何使用铬扩展内容脚本在启动时将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?

推荐答案

这里要说清楚,加载意味着开始加载,对吗?当你说你熟悉manifest.json时,你的意思是你熟悉必要的权限,以及如何指定要运行哪些页面和脚本。因为我认为你要找的是你的manifest.json中的 content_scripts run_at 属性:

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#注册

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

这会强制代码在DOM加载到每个页面之前执行。请注意,这可能很复杂,因为您的扩展可能无法创建HTML,因为当然,DOM尚未加载。如果你希望你的脚本比它的默认document_idle早一点点触发,使用document_end相反。

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.

这篇关于我如何使用铬扩展内容脚本在启动时将html注入页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆