From a934f2ced466f99ea92ac54c82c88099763b61cf Mon Sep 17 00:00:00 2001 From: Yufan You Date: Tue, 12 Nov 2024 00:59:20 +0800 Subject: [PATCH] Request host permissions for custom API servers on Firefox `content_scripts.matches` implies permissions to send cross-origin requests on Chrome, but not on Firefox with Manifest V2. We need to request permissions for custom API servers. --- src/manifests/firefox.json | 3 ++- src/utils/settings.ts | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/manifests/firefox.json b/src/manifests/firefox.json index 0fb2721..40db0e9 100644 --- a/src/manifests/firefox.json +++ b/src/manifests/firefox.json @@ -38,6 +38,7 @@ "chrome_style": false, "page": "options.html" }, - "permissions": ["alarms", "tabs", "storage", "activeTab"], + "permissions": ["alarms", "tabs", "storage", "activeTab", "https://api.wakatime.com/*", "https://wakatime.com/*"], + "optional_permissions": [""], "version": "4.0.9" } diff --git a/src/utils/settings.ts b/src/utils/settings.ts index 9e8633d..dffb9f0 100644 --- a/src/utils/settings.ts +++ b/src/utils/settings.ts @@ -81,5 +81,10 @@ export const getSettings = async (): Promise => { }; export const saveSettings = async (settings: Settings): Promise => { - return browser.storage.sync.set(settings); + // permissions.request must be the first await, not after the browser.storage.sync.set + // See https://stackoverflow.com/a/47729896/12601364 + await browser.permissions.request({ + origins: [`${settings.apiUrl}/*`], + }); + await browser.storage.sync.set(settings); };