From 93af6062476b4bbd2257072aa74ec3ac4f068c4a Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Fri, 5 Jul 2024 18:46:39 +0300 Subject: [PATCH 1/6] added back scripting permission --- src/manifests/base.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manifests/base.json b/src/manifests/base.json index fa3178e3..21e97eb9 100644 --- a/src/manifests/base.json +++ b/src/manifests/base.json @@ -30,5 +30,5 @@ "page": "options.html", "open_in_tab": true }, - "permissions": ["storage", "activeTab"] + "permissions": ["storage", "activeTab", "scripting"] } From a87916c539f39c23bda63f7b407d771fa914c332 Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Sun, 7 Jul 2024 16:40:11 +0300 Subject: [PATCH 2/6] firefox now runs with manifest v3 --- src/manifests/base.json | 29 ++++++++++++++++++++++++++++- src/manifests/chrome-edge.json | 30 ------------------------------ src/manifests/firefox.json | 31 +------------------------------ 3 files changed, 29 insertions(+), 61 deletions(-) diff --git a/src/manifests/base.json b/src/manifests/base.json index 9c0287df..9bfe7084 100644 --- a/src/manifests/base.json +++ b/src/manifests/base.json @@ -30,5 +30,32 @@ "page": "options.html", "open_in_tab": true }, - "permissions": ["storage", "activeTab", "scripting"] + "permissions": ["storage", "activeTab", "scripting"], + "manifest_version": 3, + "web_accessible_resources": [ + { + "resources": ["*.svg"], + "matches": [ + "*://github.com/*", + "*://bitbucket.org/*", + "*://dev.azure.com/*", + "*://*.visualstudio.com/*", + "*://gitea.com/*", + "*://gitlab.com/*", + "*://gitee.com/*", + "*://sourceforge.net/*", + "*://*/*" + ] + } + ], + "action": { + "default_title": "Material Icons Settings", + "default_popup": "settings-popup.html", + "default_icon": { + "16": "icon-16.png", + "32": "icon-32.png", + "48": "icon-48.png", + "128": "icon-128.png" + } + } } diff --git a/src/manifests/chrome-edge.json b/src/manifests/chrome-edge.json index b5e462f3..bff1dee3 100644 --- a/src/manifests/chrome-edge.json +++ b/src/manifests/chrome-edge.json @@ -1,33 +1,3 @@ { - "manifest_version": 3, - "web_accessible_resources": [ - { - "resources": ["*.svg"], - "matches": [ - "*://github.com/*", - "*://bitbucket.org/*", - "*://dev.azure.com/*", - "*://*.visualstudio.com/*", - "*://gitea.com/*", - "*://gitlab.com/*", - "*://gitee.com/*", - "*://sourceforge.net/*", - "*://*/*" - ] - } - ], - "action": { - "default_title": "Material Icons Settings", - "default_popup": "settings-popup.html", - "default_icon": { - "16": "icon-16.png", - "32": "icon-32.png", - "48": "icon-48.png", - "128": "icon-128.png" - } - }, - "background": { - "service_worker": "./background.js" - }, "optional_host_permissions": ["*://*/*"] } diff --git a/src/manifests/firefox.json b/src/manifests/firefox.json index 5d1c43bd..fad814c2 100644 --- a/src/manifests/firefox.json +++ b/src/manifests/firefox.json @@ -4,34 +4,5 @@ "id": "{eac6e624-97fa-4f28-9d24-c06c9b8aa713}" } }, - "manifest_version": 2, - "web_accessible_resources": ["*.svg"], - "browser_action": { - "default_title": "Material Icons Settings", - "default_popup": "settings-popup.html", - "default_icon": { - "16": "icon-16.png", - "32": "icon-32.png", - "48": "icon-48.png", - "128": "icon-128.png" - } - }, - "content_scripts": [ - { - "matches": [ - "*://github.com/*", - "*://bitbucket.org/*", - "*://dev.azure.com/*", - "*://*.visualstudio.com/*", - "*://gitea.com/*", - "*://gitlab.com/*", - "*://gitee.com/*", - "*://sourceforge.net/*", - "*://*/*" - ], - "js": ["./main.js"], - "css": ["./injected-styles.css"], - "run_at": "document_start" - } - ] + "optional_permissions": ["activeTab", ""] } From 7a34d72aafed7c57d623c948431f70c24c9893aa Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Sun, 7 Jul 2024 16:45:30 +0300 Subject: [PATCH 3/6] firefox now has optional site permissions --- scripts/build-src.ts | 5 +- src/background/background.ts | 66 ------------------------ src/ui/popup/settings-popup.css | 2 + src/ui/popup/settings-popup.ts | 90 +++++++++++++++++++++++++-------- 4 files changed, 72 insertions(+), 91 deletions(-) delete mode 100644 src/background/background.ts diff --git a/scripts/build-src.ts b/scripts/build-src.ts index 3d7d231f..bedc862c 100644 --- a/scripts/build-src.ts +++ b/scripts/build-src.ts @@ -57,13 +57,10 @@ function src( ); const bundleOptionsScript = (): Promise => bundleJS(distPath, path.resolve(srcPath, 'ui', 'options', 'options.ts')); - const bundleBackgroundScript = (): Promise => - bundleJS(distPath, path.resolve(srcPath, 'background', 'background.ts')); const bundleAll: Promise = bundleMainScript() .then(bundlePopupScript) - .then(bundleOptionsScript) - .then(bundleBackgroundScript); + .then(bundleOptionsScript); const copyPopup: Promise = Promise.all( [ diff --git a/src/background/background.ts b/src/background/background.ts deleted file mode 100644 index e627b162..00000000 --- a/src/background/background.ts +++ /dev/null @@ -1,66 +0,0 @@ -import Browser from 'webextension-polyfill'; - -type Message = { - event: string; - data: { - host: string; - tabId: number; - }; -}; - -Browser.runtime.onMessage.addListener((message: Message) => { - if (message.event === 'request-access') { - const perm: Browser.Permissions.Permissions = { - permissions: ['activeTab'], - origins: [`*://${message.data.host}/*`], - }; - - Browser.permissions.request(perm).then(async (granted: boolean) => { - if (!granted) { - return; - } - - // run the script now - Browser.scripting.executeScript({ - files: ['./main.js'], - target: { - tabId: message.data.tabId, - }, - }); - - const scripts = await Browser.scripting.getRegisteredContentScripts({ - ids: ['material-icons'], - }); - - const pattern: string = `*://${message.data.host}/*`; - - if (!scripts.length) { - // register content script for future use - return Browser.scripting.registerContentScripts([ - { - id: 'material-icons', - js: ['./main.js'], - css: ['./injected-styles.css'], - matches: [pattern], - runAt: 'document_start', - }, - ]); - } - - const matches = scripts[0].matches ?? []; - - // if we somehow already registered the script for requested origin, skip it - if (matches.includes(pattern)) { - return; - } - - // add new origin to content script - return Browser.scripting.updateContentScripts([ - { - id: 'material-icons', - matches: [...matches, pattern], - }, - ]); - }); - } -}); diff --git a/src/ui/popup/settings-popup.css b/src/ui/popup/settings-popup.css index 7b225032..9c6682c2 100644 --- a/src/ui/popup/settings-popup.css +++ b/src/ui/popup/settings-popup.css @@ -4,6 +4,8 @@ body { color: #1a202c; font-size: 16px; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; + /** firefox has bigger popup width limit, this is to bring it to parity with chromium **/ + max-width: 300px; } #content { diff --git a/src/ui/popup/settings-popup.ts b/src/ui/popup/settings-popup.ts index d81dbff7..b9e04869 100644 --- a/src/ui/popup/settings-popup.ts +++ b/src/ui/popup/settings-popup.ts @@ -7,9 +7,6 @@ import { providerConfig, } from '../../providers'; -const HOST_IS_NEW = 1; -const HOST_NO_MATCH = 2; - const isPageSupported = (domain: string) => getGitProvider(domain); function getCurrentTab() { @@ -150,7 +147,7 @@ function guessProvider(tab: Browser.Tabs.Tab) { return Browser.tabs.sendMessage(tab.id ?? 0, cmd).then((match) => { if (match === null) { - return HOST_NO_MATCH; + return false; } return match; @@ -174,30 +171,88 @@ function checkAccess(tab: Browser.Tabs.Tab) { origins: [`*://${host}/*`], }; - return Browser.permissions.contains(perm).then((r) => { + return Browser.permissions.contains(perm).then(async (r) => { if (r) { + await ensureContentScriptRegistered(tab); + return tab; } - return HOST_IS_NEW; + return false; }); } function requestAccess(tab: Browser.Tabs.Tab) { const { host } = new URL(tab.url ?? ''); - return Browser.runtime.sendMessage({ - event: 'request-access', - data: { - tabId: tab.id, - host, - }, + const perm: Browser.Permissions.Permissions = { + permissions: ['activeTab'], + origins: [`*://${host}/*`], + }; + + // request the permission + Browser.permissions.request(perm).then(async (granted: boolean) => { + if (!granted) { + return; + } + + // when granted reload the popup to show ui changes + window.location.reload(); + }); + + // close the popup, in firefox it stays open for some reason. + window.close(); +} + +async function ensureContentScriptRegistered(tab: Browser.Tabs.Tab) { + const { host } = new URL(tab.url ?? ''); + + const scripts = await Browser.scripting.getRegisteredContentScripts({ + ids: ['material-icons'], }); + + const pattern: string = `*://${host}/*`; + + if (!scripts.length) { + // run the script now in the current tab to prevent need for reloading + await Browser.scripting.executeScript({ + files: ['./main.js'], + target: { + tabId: tab.id ?? 0, + }, + }); + + // register content script for future use + return Browser.scripting.registerContentScripts([ + { + id: 'material-icons', + js: ['./main.js'], + css: ['./injected-styles.css'], + matches: [pattern], + runAt: 'document_start', + }, + ]); + } + + const matches = scripts[0].matches ?? []; + + // if we somehow already registered the script for requested origin, skip it + if (matches.includes(pattern)) { + return; + } + + // add new origin to content script + return Browser.scripting.updateContentScripts([ + { + id: 'material-icons', + matches: [...matches, pattern], + }, + ]); } function doGuessProvider(tab: Browser.Tabs.Tab, domain: string) { return guessProvider(tab).then((match) => { - if (match !== HOST_NO_MATCH) { + if (match !== false) { registerControls(domain); displayDomainSettings(); @@ -224,15 +279,8 @@ function init(tab: Browser.Tabs.Tab) { return displayPageNotSupported(domain); } - // overwrite for firefox browser, currently does not support - // asking for permissions from background, so it will run - // on all pages. - if (isFirefox()) { - return doGuessProvider(tab, domain); - } - return checkAccess(tab).then((access) => { - if (access === HOST_IS_NEW) { + if (access === false) { return askDomainAccess(tab); } From 847db7fc1f8857f38559959704edb04a943aef26 Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Sun, 7 Jul 2024 16:54:55 +0300 Subject: [PATCH 4/6] surpress firefox quirks warning --- src/ui/options/options.html | 1 + src/ui/popup/settings-popup.css | 4 ++-- src/ui/popup/settings-popup.html | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ui/options/options.html b/src/ui/options/options.html index e5f80ba2..51a6a776 100644 --- a/src/ui/options/options.html +++ b/src/ui/options/options.html @@ -1,3 +1,4 @@ + diff --git a/src/ui/popup/settings-popup.css b/src/ui/popup/settings-popup.css index 9c6682c2..680efa62 100644 --- a/src/ui/popup/settings-popup.css +++ b/src/ui/popup/settings-popup.css @@ -46,8 +46,8 @@ body { #options-btn { margin-left: auto; - width: 24; - height: 24; + width: 24px; + height: 24px; filter: grayscale(1); } diff --git a/src/ui/popup/settings-popup.html b/src/ui/popup/settings-popup.html index 46b4bcd4..b0dc2fd1 100644 --- a/src/ui/popup/settings-popup.html +++ b/src/ui/popup/settings-popup.html @@ -1,3 +1,4 @@ + From a8373989d624732e865049eb8ad6c7fc4b938b2a Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Sun, 7 Jul 2024 16:55:45 +0300 Subject: [PATCH 5/6] biomejs formatting --- src/lib/replace-icon.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/replace-icon.ts b/src/lib/replace-icon.ts index 1f062520..7b1be473 100644 --- a/src/lib/replace-icon.ts +++ b/src/lib/replace-icon.ts @@ -28,7 +28,7 @@ export function replaceIconInRow( ): void { const fileName = itemRow .querySelector(provider.selectors.filename) - ?.textContent// get the last folder for the icon + ?.textContent // get the last folder for the icon ?.split('/') .reverse()[0] .trim() From 81a3a9572b991f02ae290bb33cc7afde0929f9ea Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Mon, 8 Jul 2024 12:02:26 +0300 Subject: [PATCH 6/6] Removed unused function --- src/ui/popup/settings-popup.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/ui/popup/settings-popup.ts b/src/ui/popup/settings-popup.ts index b9e04869..d3956523 100644 --- a/src/ui/popup/settings-popup.ts +++ b/src/ui/popup/settings-popup.ts @@ -263,10 +263,6 @@ function doGuessProvider(tab: Browser.Tabs.Tab, domain: string) { }); } -function isFirefox() { - return navigator.userAgent.toLowerCase().includes('firefox'); -} - function init(tab: Browser.Tabs.Tab) { const domain = new URL(tab.url ?? '').host;