简体   繁体   中英

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. 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:

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.

I am getting the runtime URL as:

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

but the fetch call fails with the error:

Failed to load resource: unsupported 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?

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.' Upgrading macOS to 12.6 which comes with safari 16.0 solved this issue for me. Hopefully this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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