问题描述
在 background.js 文件中使用以下代码获取文件内容时:
function readFile(filename) {
let response = fetch(`file:///${filename}`);
console.log(response);
}
我收到以下错误:
Security Error: Content at moz-extension://86d386b7-2904-441f-8dbe-47f0af9b6bfb/_generated_background_page.html may not load or link to file:///C:/Users/rando/Downloads/jest-26.6.1.zip.
TypeError: NetworkError when attempting to fetch resource.
我的清单文件如下所示:
"permissions": [
"webNavigation","webRequest","webRequestBlocking","cookies","tabs","http://*/*","https://*/*","ws://*/*","wss://*/*","storage","downloads","sessions","file://*/*"
],"background": {
"scripts": [
"app/scripts/background.js"
],"persistent": true
},
如何解决此错误并使用 background.js 文件获取文件内容?
解决方法
出于安全原因,从 Firefox 57 起,扩展程序对本地文件系统 file:///
的访问已被阻止。
扩展程序可以使用 input type="file" 在用户点击时启动文件选择器。
扩展还可以:
- 读取扩展中打包的文件
- 通过 Native messaging 与本地计算机上的软件通信
- 通过 HTTP/s 获取文件,如果本地主机有一个正在运行的服务器