Porting Chrome extension to Safari error: `The service_worker script failed to load due to an error`

360 Views Asked by At

I have a Chrome extension that works as expected. When I port it to Safari I get an error that can't be debugged.

Steps taken (from the dev docs):

  1. Run xcrun safari-web-extension-converter <PATH_TO_CHROME_EXTENSION>

This throws a warning that essentially doesn't accept any keys from the manifest.

Warning: The following keys in your manifest.json are not supported by your current version of Safari. If these are critical to your extension, you should review your code to see if you need to make changes to support Safari:
    run_at
    all_frames
    scripting
    version
    js
    name
    matches
    manifest_version
    icons
    commands
    <all_urls>
    description
    activeTab
    page
    browser_action
    web_accessible_resources
    contextMenus
    bookmarks
  1. Build and run Safari Web Extension
  2. Check Safari > Preferences > Extensions where I get this error: The service_worker script failed to load due to an error.

manifest.json

{
  "name": "FILL_NAME",
  "description": "FILL_DESCRIPTION",
  "version": "0.0.1",
  "manifest_version": 3,
  "icons": {
    "16": "./imgs/icon-16.png",
    "48": "./imgs/icon-48.png",
    "128": "./imgs/icon-128.png"
  },
  "permissions": [
    "activeTab",
    "contextMenus",
    "bookmarks"
  ],
  "background": {
    "service_worker": "background.js",
    "type": "module",
    "persistent": false
  },
  "externally_connectable": {
    "matches": [
      "https://*.MY_URL_HERE.com/*",
      "http://localhost/*"
    ]
  },
  "action": {
    "default_icon": "./imgs/icon-16.png",
    "default_title": "DEFAULT_TITLE"
  },
  "commands": {
    "save-page": {
      "suggested_key": {
        "default": "Ctrl+Shift+S",
        "mac": "Command+Shift+S"
      },
      "description": "COMMAND_DESCRIPTION"
    }
  },
  "content_security_policy": {
    "extension_pages": "script-src 'self'; object-src 'self'",
    "sanbox": "sandbox allow-scripts; script-src 'self' https://apis.google.com https://www.gstatic.com https://www.googleapis.com https://securetoken.googleapis.com; object-src 'self'"
  },
  "host_permissions": [
    "<all_urls>"
  ],
  "web_accessible_resources": [
    {
      "resources": [
        "imgs/*.png",
        "overlay.html"
      ],
      "matches": [
        "<all_urls>"
      ]
    }
  ],
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "/js/content.js"
      ],
      "run_at": "document_end",
      "all_frames": false
    }
  ]
}

Any ideas on how to debug this?

1

There are 1 best solutions below

0
On

Manifest v3 does not support the persistent key. Just use this for background:

...
  "background": {
    "service_worker": "background.js",
    "type": "module"
  },
...