我有一个Chrome扩展,可以(如果您允许访问文件URL)获取您的本地pdf文件,您已经打开的铬,并将它发送到我们的API进行处理。这是通过从背景脚本中获取
XMLHttpRequest
到
file:///Users/user/whatever/testfile.pdf
的pdf来完成的。
当迁移到针对Chrome扩展的显式v3时,后台脚本将成为一个服务工作者。在服务工作者中,只有
fetch
可用,而不是
XMLHttpRequest
。问题是,fetch只支持http和https,而不支持file:// urls。那么,如何使Chrome扩展获取/获取本地文件的功能相同呢?
编辑:我也尝试过:
net:ERR_UNKNOWN_URL_SCHEME
。
Access to XMLHttpRequest at 'file:///.../testfile1.docx.pdf' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
据我所知,从大量的研究中可以了解到,访问
file://
的通常是被阻止的,而Chrome扩展后台脚本过去也是一个例外。在我看来,它从来不允许从内容脚本或动作弹出。
。
我的manifest.json供参考:
{
"manifest_version": 3,
"name": "..",
"version": "0.1",
"icons": {
"16": "assets/icon-16x16.png",
"48": "assets/icon-48x48.png",
"128": "assets/icon-128x128.png"
"action": {
"default_title": ".."
"background": {
"service_worker": "background.js"
"permissions": [
"webRequest",
"activeTab",
"scripting",
"storage",
"unlimitedStorage",
"identity",
"pageCapture"
"host_permissions": [
"<all_urls>"
"web_accessible_resources": [{
"resources": ["iframe.html"],