繁体   English   中英

如何从 chrome 扩展中的内容脚本中获取另一个文件内容

[英]How to get another file content from content script in chrome extension

我有一个带有如下文件的 chrome 扩展名:

.
├── settings.json
├── manifest.json
└── ContentScript.js

像这样的清单

    "content_scripts": [
        {
            "js": [
                "ContentScript.js"
            ],
            "run_at": "document_end",
            "matches": ["https://example.com"]
        }
    ]

如何从 ContentScript.js 中的settings.json获取数据?

首先将其添加到清单中:

    "web_accessible_resources": [
        "settings.json"
    ]

强制文件可访问。

然后在 ContentScript 中添加

(async () => {
    var text = await fetch(chrome.runtime.getURL("settings.json")).then((responce) => {
        if (responce.ok) {
            return responce.text()// or responce.json() to json-style object
        } else { throw "Error: File was not found or can't be reached" }
    })
}).call()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM