Skip to content

Commit

Permalink
Merge pull request #261 from gopasspw/chrome-manifest-v3
Browse files Browse the repository at this point in the history
Migrate Chrome extension to Manifest V3
  • Loading branch information
Pharb authored Oct 12, 2024
2 parents 8e86e53 + e34cb50 commit 18d4d59
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ release: package
$(LOCAL_WEB_EXT) -s $(CURDIR)/firefox-release build --overwrite-dest

clean:
rm -rf chrome firefox chrome-release firefox-release chrome.zip webextension-polyfill
rm -rf chrome firefox chrome-release firefox-release chrome.zip webextension-polyfill web-ext-artifacts

format:
npm run format
Expand Down
34 changes: 19 additions & 15 deletions manifests/chrome-manifest-dev.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"manifest_version": 2,
"manifest_version": 3,
"short_name": "gopassbridge",
"name": "Gopass Bridge",
"version": "1.0.0",
Expand Down Expand Up @@ -27,14 +27,10 @@
}
],

"minimum_chrome_version": "88",

"background": {
"scripts": [
"vendor/browser-polyfill.js",
"generic.js",
"i18n.js",
"background.js"
],
"persistent": true
"service_worker": "service_worker.js"
},

"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0mHC1MVxHG3X9CZ6b6lp1hCW5j2nP9bRES/Mw6sL1Emkp6HyHXU745HRGv+JD8Q+jvo85IYjGnxrcrgq+K9Op8S4ggzjaZMVL9kmDeluQPRE4247uEu2WCLZR7x00A2unZtErvlwNYiYIRcqhDZ5VQDtxKNdg6CsFr56g7ur2iuF4+RwKSDbdfVY0t9yb++Rsj9aAtarDbPnuR4gWDzR/3AL4SmgU21BQYARjUQHLFmmH1M2YWVz2I9Z3w2m3EfL6oNMSm7qFweRTrBMmVKLwQ9jitvz8To7QX5si6y8L3CRKBNtBIwBF3t8Cm4CIB5Tfjh/6RN5cf/9D7Jci0EXOQIDAQAB",
Expand All @@ -45,7 +41,7 @@

"default_locale": "en",

"browser_action": {
"action": {
"browser_style": true,
"default_icon": {
"48": "icons/gopassbridge-48.png",
Expand All @@ -56,9 +52,10 @@
},

"commands": {
"_execute_browser_action": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+U"
"default": "Ctrl+Shift+U",
"mac": "Command+Shift+U"
}
}
},
Expand All @@ -67,10 +64,17 @@
"activeTab",
"storage",
"nativeMessaging",
"http://*/*",
"https://*/*",
"notifications",
"webRequest",
"webRequestBlocking"
]
"webRequestAuthProvider"
],

"host_permissions": [
"http://*/*",
"https://*/*"
],

"content_security_policy": {
"extension_pages": "default-src 'self'; style-src 'unsafe-inline';"
}
}
34 changes: 18 additions & 16 deletions manifests/chrome-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"manifest_version": 2,
"manifest_version": 3,
"short_name": "gopassbridge",
"name": "Gopass Bridge",
"version": "1.0.0",
Expand Down Expand Up @@ -27,16 +27,10 @@
}
],

"minimum_chrome_version": "63",
"minimum_chrome_version": "88",

"background": {
"scripts": [
"vendor/browser-polyfill.js",
"generic.js",
"i18n.js",
"background.js"
],
"persistent": true
"service_worker": "service_worker.js"
},

"options_ui": {
Expand All @@ -45,7 +39,7 @@

"default_locale": "en",

"browser_action": {
"action": {
"browser_style": true,
"default_icon": {
"48": "icons/gopassbridge-48.png",
Expand All @@ -56,9 +50,10 @@
},

"commands": {
"_execute_browser_action": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+U"
"default": "Ctrl+Shift+U",
"mac": "Command+Shift+U"
}
}
},
Expand All @@ -67,10 +62,17 @@
"activeTab",
"storage",
"nativeMessaging",
"http://*/*",
"https://*/*",
"notifications",
"webRequest",
"webRequestBlocking"
]
"webRequestAuthProvider"
],

"host_permissions": [
"http://*/*",
"https://*/*"
],

"content_security_policy": {
"extension_pages": "default-src 'self'; style-src 'unsafe-inline';"
}
}
4 changes: 4 additions & 0 deletions tests/unit/generic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ describe('urlDomain', () => {
test('extracts hostname', () => {
expect(generic.urlDomain('http://www.muh.maeh.de:80/some/path?maeh')).toEqual('www.muh.maeh.de');
});

test('extracts default "localhost" for invalid URL', () => {
expect(generic.urlDomain('invalid')).toEqual('localhost');
});
});

describe('localStorage wrappers', () => {
Expand Down
14 changes: 8 additions & 6 deletions web-extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ function initBackground() {

initBackground();

window.tests = {
background: {
initBackground,
processMessageAndCatch,
},
};
if ('window' in globalThis) {
window.tests = {
background: {
initBackground,
processMessageAndCatch,
},
};
}
42 changes: 23 additions & 19 deletions web-extension/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ function createButtonWithCallback(attributes, callback) {
}

function urlDomain(urlString) {
const a = document.createElement('a');
a.href = urlString;
return a.hostname;
try {
return new URL(urlString).hostname;
} catch (e) {
return 'localhost';
}
}

function setLocalStorageKey(key, value) {
Expand Down Expand Up @@ -132,19 +134,21 @@ function isChrome() {
return browser.runtime.getURL('/').startsWith('chrome');
}

window.tests = {
generic: {
sendNativeAppMessage,
executeOnSetting,
urlDomain,
setLocalStorageKey,
getLocalStorageKey,
createButtonWithCallback,
showNotificationOnSetting,
getPopupUrl,
isChrome,
openURLOnEvent,
makeAbsolute,
checkVersion,
},
};
if ('window' in globalThis) {
window.tests = {
generic: {
sendNativeAppMessage,
executeOnSetting,
urlDomain,
setLocalStorageKey,
getLocalStorageKey,
createButtonWithCallback,
showNotificationOnSetting,
getPopupUrl,
isChrome,
openURLOnEvent,
makeAbsolute,
checkVersion,
},
};
}
5 changes: 5 additions & 0 deletions web-extension/service_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

importScripts('vendor/browser-polyfill.js');
importScripts('generic.js');
importScripts('background.js');

0 comments on commit 18d4d59

Please sign in to comment.