From 2f84b04616869e7671e8cbe8594715190d0776aa Mon Sep 17 00:00:00 2001 From: escapedcat Date: Fri, 8 Jul 2022 15:05:32 +0800 Subject: [PATCH] refactor!: manifest v3 support #1051 --- .../actions/setup/setIcon.js | 27 ++++++ src/extension/background-script/index.ts | 25 +++++- src/extension/content-script/injectScript.js | 2 +- src/manifest.json | 30 ++++--- src/manifest.json.development.sample | 90 ------------------- 5 files changed, 69 insertions(+), 105 deletions(-) create mode 100644 src/extension/background-script/actions/setup/setIcon.js delete mode 100644 src/manifest.json.development.sample diff --git a/src/extension/background-script/actions/setup/setIcon.js b/src/extension/background-script/actions/setup/setIcon.js new file mode 100644 index 0000000000..fe1a2f553c --- /dev/null +++ b/src/extension/background-script/actions/setup/setIcon.js @@ -0,0 +1,27 @@ +import browser from "webextension-polyfill"; + +const setIcon = async (message, sender) => { + // TODO: refactor names / rename files? + const names = { + active: "alby_icon_yellow", + off: "alby_icon_sleeping", + }; + const name = names[message.args.icon]; + return browser.action + .setIcon({ + path: { + 16: `assets/icons/${name}_16x16.png`, + 32: `assets/icons/${name}_32x32.png`, + 48: `assets/icons/${name}_48x48.png`, + 128: `assets/icons/${name}_128x128.png`, + }, + tabId: sender.tab.id, + }) + .then(() => { + return { + data: true, + }; + }); +}; + +export default setIcon; diff --git a/src/extension/background-script/index.ts b/src/extension/background-script/index.ts index 4fb5871a2c..adc91e1efc 100644 --- a/src/extension/background-script/index.ts +++ b/src/extension/background-script/index.ts @@ -32,6 +32,7 @@ const extractLightningData = ( // Adding a short delay because I've seen cases where this call has happened too fast // before the receiving side in the content-script was connected/listening setTimeout(() => { + // double check: https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/#alarms browser.tabs.sendMessage(tabId, { action: "extractLightningData", }); @@ -55,10 +56,26 @@ const updateIcon = async ( .equalsIgnoreCase(url.host) .first(); - await setIcon( - allowance ? ExtensionIcon.Active : ExtensionIcon.Default, - tabId - ); + // TODO: move to some config file + const names = { + active: "alby_icon_yellow", + off: "alby_icon_sleeping", + }; + let name; + if (allowance) { + name = names.active; + } else { + name = names.off; + } + return browser.action.setIcon({ + path: { + 16: `assets/icons/${name}_16x16.png`, + 32: `assets/icons/${name}_32x32.png`, + 48: `assets/icons/${name}_48x48.png`, + 128: `assets/icons/${name}_128x128.png`, + }, + tabId: tabId, + }); }; const debugLogger = (message: unknown, sender: Runtime.MessageSender) => { diff --git a/src/extension/content-script/injectScript.js b/src/extension/content-script/injectScript.js index 3c19c8ae8b..8b63bc49e8 100644 --- a/src/extension/content-script/injectScript.js +++ b/src/extension/content-script/injectScript.js @@ -13,7 +13,7 @@ export default function injectScript() { scriptEl.setAttribute("type", "text/javascript"); scriptEl.setAttribute( "src", - browser.extension.getURL("js/inpageScript.bundle.js") + browser.extension.getURL("js/inpageScript.bundle.js") // https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#sunset-deprecated-apis ); container.appendChild(scriptEl); } catch (err) { diff --git a/src/manifest.json b/src/manifest.json index 5f2594784f..4459e5bdbf 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "Alby - Bitcoin Lightning Wallet", - "version": "0.0.0", + "version": "1.0.0", "icons": { "16": "assets/icons/alby_icon_yellow_16x16.png", @@ -11,18 +11,29 @@ }, "description": "The Bitcoin Lightning wallet for direct payments across the globe, Bitcoin Lightning applications and passwordless logins.", "homepage_url": "https://getAlby.com/", - "web_accessible_resources": ["js/inpageScript.bundle.js"], + + "COMMENT_web_accessible_resources": "https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#web-accessible-resources", + "web_accessible_resources": [ + { + "resources": ["js/inpageScript.bundle.js"] + } + ], + "permissions": [ "notifications", "activeTab", "tabs", "storage", - "unlimitedStorage", "nativeMessaging", - "*://*/*" + "unlimitedStorage" ], + "host_permissions": ["*://*/*"], - "content_security_policy": "script-src 'self'; object-src 'self'", + "COMMENT_content_security_policy": [ + "https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#content-security-policy", + "https://developer.chrome.com/docs/extensions/mv3/mv3-migration-checklist/#security_checklist" + ], + "content_security_policy.extension_pages": "script-src 'self'; object-src 'self'", "__chrome|firefox__author": "Alby", "__opera__developer": { @@ -35,10 +46,10 @@ } }, - "__chrome__minimum_chrome_version": "49", + "__chrome__minimum_chrome_version": "88", "__opera__minimum_opera_version": "36", - "browser_action": { + "action": { "default_popup": "popup.html", "default_icon": { "16": "assets/icons/alby_icon_yellow_16x16.png", @@ -89,8 +100,7 @@ }, "background": { - "scripts": ["js/background.bundle.js"], - "__chrome|opera__persistent": true + "service_worker": "js/background.bundle.js" }, "content_scripts": [ diff --git a/src/manifest.json.development.sample b/src/manifest.json.development.sample deleted file mode 100644 index 4e7b5a8958..0000000000 --- a/src/manifest.json.development.sample +++ /dev/null @@ -1,90 +0,0 @@ -{ - "manifest_version": 2, - "name": "Lightning", - "version": "1.0.0", - - "icons": { - "16": "assets/icons/favicon-16.png", - "32": "assets/icons/favicon-32.png", - "48": "assets/icons/favicon-48.png", - "128": "assets/icons/favicon-128.png" - }, - "description": "Lightning up the browser", - "homepage_url": "https://github.com/bumi", - "short_name": "Lightning", - "web_accessible_resources": ["js/inpageScript.bundle.js"], - "permissions": [ - "nativeMessaging", - "notifications", - "activeTab", - "storage", - "webRequest", - "webRequestBlocking", - "http://*/*", - "https://*/*" - ], - "optional_permissions": [ - "nativeMessaging" - ], - - "content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'; connect-src ws://localhost:8097 ws://localhost:9090 https://alice-wallet.webln.external.myzel.io:443 https://blockchain.info/ticker", - - "__chrome|firefox__author": "bumi", - "__opera__developer": { - "name": "bumi" - }, - - "__firefox__applications": { - "gecko": { - "id": "{424FB1AD-CC3B-4856-B6A0-7786F8CA9D17}" - } - }, - - "__chrome__minimum_chrome_version": "49", - "__opera__minimum_opera_version": "36", - - "browser_action": { - "default_popup": "popup.html", - "default_icon": { - "16": "assets/icons/favicon-16.png", - "32": "assets/icons/favicon-32.png", - "48": "assets/icons/favicon-48.png", - "128": "assets/icons/favicon-128.png" - }, - "default_title": "Lightning", - "__chrome|opera__chrome_style": false, - "__firefox__browser_style": false - }, - - "commands": { - "_execute_browser_action": { - "suggested_key": { - "default": "Alt+Shift+A" - } - } - }, - - "__chrome|opera__options_page": "options.html", - "options_ui": { - "page": "options.html", - "open_in_tab": true, - "__chrome__chrome_style": false - }, - - "background": { - "scripts": [ - "js/background.bundle.js" - ], - "__chrome|opera__persistent": true - }, - - "content_scripts": [{ - "matches": [ - "http://*/*", - "https://*/*" - ], - "js": [ - "js/contentScript.bundle.js" - ] - }] -}