You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展Manifest V2配置后content scripts未注入页面不生效如何解决?

调整方案
  • 刷新对应页面:扩展安装成功后,已经打开的https://www.google.com/页面不会自动注入新增扩展的content scripts,手动刷新该页面即可触发注入。
  • 统一站点匹配规则:当前permissions中配置的站点权限为https://google.com/*(无www前缀),和content scripts的匹配规则https://www.google.com/*(有www前缀)不一致,建议统一调整为匹配所有Google主站场景,避免域名差异导致注入失败:
    1. 将content scripts的matches字段修改为["*://*.google.com/*"]
    2. 将permissions中的Google站点权限修改为"*://*.google.com/*"
  • 修正脚本配置错误:background.js是后台脚本,不应该放在content scripts的js列表中,需要单独在manifest根节点配置"background": {"scripts": ["background.js"], "persistent": false}即可,避免不必要的逻辑冲突。
  • 确认文件路径正确性:保证test.js、test.css和manifest.json在同一级目录,如果存放在子目录,需要补全完整相对路径,比如存放在js文件夹下就要写成"js/test.js"。
  • 可选配置注入时机:如果你的脚本需要在DOM加载完成前执行,可以在content_scripts的配置项中新增"run_at": "document_start",默认注入时机为页面空闲时的document_idle,避免因运行时机不对导致功能看起来未生效。

修正后完整manifest.json参考

{
    "manifest_version": 2,
    "name": "Sample Name",
    "version": "1.0.0",
    "description": "This is a sample description",
    "short_name": "Short Sample Name",
    "permissions": ["tabs", "*://*.google.com/*", "activeTab", "declarativeContent", "storage"],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "content_scripts": [
        {
        "matches": ["*://*.google.com/*"],
        "js": ["test.js"],
        "css": ["test.css"]
        }
    ],
    "browser_action": {
        "default_title": "This is a sample title",
        "default_popup": "popup.html",
        "default_icon": {
            "16": "icons/icon16.png",
            "32": "icons/icon32.png"
        }
    }
}

内容的提问来源于stack exchange,提问作者Boris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 10:54:04