【问题标题】:how can i use a chrome extension content script to inject html into a page at startup如何在启动时使用 chrome 扩展内容脚本将 html 注入页面
【发布时间】:2011-04-04 08:15:46
【问题描述】:

我想创建一个扩展,以便在加载后立即将 html 注入每个页面。我熟悉 manifest.json 规则以及如何运行内容脚本。我目前遇到的问题是内容脚本在网页加载后注入 html,这有点破坏性。我想在窗口打开后立即加载它,以便注入它,然后网页也加载。你能帮忙吗?

【问题讨论】:

    标签: google-chrome inject


    【解决方案1】:

    这里要明确一点,“加载”的意思是“开始加载”,对吗?当您说您熟悉 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"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 2012-06-17
      相关资源
      最近更新 更多