简体   繁体   English

Safari 扩展后台页面无法获取 web 可访问资源

[英]Safari extension background page unable to fetch web accessible resource

I have a chrome extension that was converted to Safari extension using xcode safari-web-extension-converter utility.我有一个 chrome 扩展,它使用 xcode safari-web-extension-converter 实用程序转换为 Safari 扩展。 With that, everything except one thing seems to be fine.这样一来,除了一件事之外的一切似乎都很好。 In the background script, I am loading some data from a web accessible resource like so:在后台脚本中,我从 web 可访问资源中加载一些数据,如下所示:

export const fetchConfig = async () => {
   let dataURL = '';
   if (!checkBrowser(BROWSERS.Chrome, true)) {
       dataURL = chrome.runtime.getURL('config.json');
   } else {
       dataURL = browser.runtime.getURL('config.json');
   }
   const res = await fetch(dataURL);
   return await res.json();
}

This works for all browsers except Safari.这适用于除 Safari 之外的所有浏览器。

I am getting the runtime URL as:我得到运行时 URL 为:

safari-web-extension://E522689D-94A6-4561-90F3-BF22C7848965/config.json safari-web-extension://E522689D-94A6-4561-90F3-BF22C7848965/config.json

but the fetch call fails with the error:但 fetch 调用失败并出现错误:

Failed to load resource: unsupported URL加载资源失败:不支持 URL

I have not been able to find anything on this in the documentation or the developer support, but can we not access the web accessible resources in background in Safari?我无法在文档或开发人员支持中找到任何相关内容,但我们能否在 Safari 的后台访问 web 可访问资源?

My manifest file looks like this:我的清单文件如下所示:

{
"manifest_version": 3,
"name": "Ext Dev",
"author": "Test",
"description": "Test",
"version": "1.1.0",
"icons": {
    "16": "assets/icon/icon16.png",
    "32": "assets/icon/icon32.png",
    "48": "assets/icon/icon48.png",
    "128": "assets/icon/icon128.png"
},
"host_permissions": [
  ...
   host addresses here
  ...
],
"permissions": ["storage", "tabs"],
"content_security_policy": {
  "extension_pages": "script-src 'self'; object-src 'self';"
},
"web_accessible_resources": [{
  "resources": ["config.json"],
  "matches": ["<all_urls>"]
}],
"content_scripts": [
    {
        "matches": ["<all_urls>"],
        "js": ["content.js"],
        "css": ["commonstyles.css"],
        "run_at": "document_end",
        "all_frames": true
    }
],
"background": { "service_worker": "background.js" },
"action": {
  "default_title": "Ext Client",
  "default_icon": {
    "20": "assets/icon/icon20.png",
    "40": "assets/icon/icon40.png"
  }
}

} }

I had exactly the same problem as this and discovered in the release notes for Safari 16.0 that they included 'Fixed background service worker to load bundle resources.'我遇到了与此完全相同的问题,并在 Safari 16.0 的发行说明中发现它们包含“固定后台服务工作者以加载捆绑资源”。 Upgrading macOS to 12.6 which comes with safari 16.0 solved this issue for me.将 macOS 升级到 safari 16.0 随附的 12.6 为我解决了这个问题。 Hopefully this helps!希望这会有所帮助!

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

相关问题 Chrome扩展程序:资源不可用,即使将其添加到web_accessible_resources中 - Chrome Extension: Resource not available even adding it in web_accessible_resources Firefox Web扩展程序后台页面中的Websocket - Websockets in a Firefox web extension background page 无法通过网络错误获取资源 - Unable to fetch resource with fetch, NetworkError 浏览器扩展程序的后台脚本(在 Chrome 中或以其他方式)是否可以访问? - Is the background script of a browser extension (in Chrome or otherwise) accessible? Safari web 扩展的 importScripts 失败 - importScripts fails for Safari web extension Chrome扩展程序:确定后台网页iframe的网络请求? - Chrome extension: identify web requests of background-page iframe? Chrome扩展程序有音频web_accessible_resource吗? - Is there an audio web_accessible_resource for Chrome extensions? 列出Chrome扩展程序中的所有Web可访问资源 - List all web accessible resources in a Chrome Extension Chrome扩展程序:让扩展程序编辑其web_accessible_resources - Chrome Extension: have extension edit its web_accessible_resources 无法使用 JS Fetch 向 Google Web 应用程序发出跨域资源共享 (CORS) 请求 - Unable to Make Cross Origin Resource Sharing (CORS) request to Google Web App Using JS Fetch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM