From fbf3fa53b5369b06c3a5dcaf12ecba65ac62ea20 Mon Sep 17 00:00:00 2001 From: nobkd <44443899+nobkd@users.noreply.github.com> Date: Sat, 15 Apr 2023 23:01:09 +0200 Subject: [PATCH] fix: action icon now fits to currently active tab --- package-lock.json | 4 ++-- package.json | 2 +- public/manifest.json | 3 ++- src/bg/action.ts | 22 ++++------------------ src/bg/bg.ts | 15 ++++++++++++++- src/bg/utils/actionIcon.ts | 27 +++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 src/bg/utils/actionIcon.ts diff --git a/package-lock.json b/package-lock.json index 450fda9..024600f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "replace-maps", - "version": "1.1.1", + "version": "1.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "replace-maps", - "version": "1.1.1", + "version": "1.1.2", "hasInstallScript": true, "license": "Unlicense", "dependencies": { diff --git a/package.json b/package.json index 4e2840a..161d696 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "Replace Maps iframes with OSM", "author": "nobkd", "license": "Unlicense", - "version": "1.1.1", + "version": "1.1.2", "type": "module", "homepage": "https://github.com/nobkd/replace-maps#readme", "bugs": { diff --git a/public/manifest.json b/public/manifest.json index ee44022..f628d19 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -2,7 +2,7 @@ "description": "Replace Maps iframes with OSM", "manifest_version": 2, "name": "Replace Maps", - "version": "1.1.1", + "version": "1.1.2", "homepage_url": "https://github.com/nobkd/replace-maps", "icons": { "48": "icons/48.png", @@ -15,6 +15,7 @@ } }, "permissions": [ + "tabs", "storage", "webRequest", "webRequestBlocking", diff --git a/src/bg/action.ts b/src/bg/action.ts index 8457465..35ca808 100644 --- a/src/bg/action.ts +++ b/src/bg/action.ts @@ -1,5 +1,6 @@ -import { runtime, browserAction, tabs, type Tabs } from 'webextension-polyfill'; -import { disabledHosts, getHostname, invertHostState } from './utils/storage'; +import { browserAction, tabs, type Tabs } from 'webextension-polyfill'; +import { getHostname, invertHostState } from './utils/storage'; +import { updateIcon } from './utils/actionIcon'; browserAction.onClicked.addListener(async (tab: Tabs.Tab) => { if (!tab.url) return; @@ -7,22 +8,7 @@ browserAction.onClicked.addListener(async (tab: Tabs.Tab) => { let hostname = getHostname(tab.url); await invertHostState(hostname); - // TODO: having to fix that icon fits to current tab... - if (disabledHosts.includes(hostname)) { - browserAction.setIcon({ - path: { - '48': runtime.getURL('/icons/48-grey.png'), - '96': runtime.getURL('/icons/96-grey.png'), - }, - }); - } else { - browserAction.setIcon({ - path: { - '48': runtime.getURL('/icons/48.png'), - '96': runtime.getURL('/icons/96.png'), - }, - }); - } + updateIcon(hostname); // TODO: reload only frames (that have gmaps url), not full page tabs.reload(tab.id, { bypassCache: true }); diff --git a/src/bg/bg.ts b/src/bg/bg.ts index 2d6dccc..7a5218a 100644 --- a/src/bg/bg.ts +++ b/src/bg/bg.ts @@ -1,5 +1,6 @@ -import { runtime, webRequest, type WebRequest } from 'webextension-polyfill'; +import { runtime, tabs, windows, webRequest, type WebRequest } from 'webextension-polyfill'; import { disabledHosts, getHostname } from './utils/storage'; +import { updateActiveTabIcon } from './utils/actionIcon'; const patterns: string[] = [ 'http://*/maps/embed*?*', @@ -33,3 +34,15 @@ webRequest.onBeforeRequest.addListener( }, ['blocking'] ); + +// listen to tab URL changes +tabs.onUpdated.addListener(updateActiveTabIcon); + +// listen to tab switching +tabs.onActivated.addListener(updateActiveTabIcon); + +// listen for window switching +windows.onFocusChanged.addListener(updateActiveTabIcon); + +// run at startup +updateActiveTabIcon(); diff --git a/src/bg/utils/actionIcon.ts b/src/bg/utils/actionIcon.ts new file mode 100644 index 0000000..aa5a867 --- /dev/null +++ b/src/bg/utils/actionIcon.ts @@ -0,0 +1,27 @@ +import { browserAction, tabs } from 'webextension-polyfill'; +import { disabledHosts, getHostname } from './storage'; + +export function updateIcon(hostname: string) { + let disabled = disabledHosts.includes(hostname); + + browserAction.setIcon({ + path: !disabled + ? { + 48: '/icons/48.png', + 96: '/icons/96.png', + } + : { + 48: '/icons/48-grey.png', + 96: '/icons/96-grey.png', + }, + }); +} + +export async function updateActiveTabIcon() { + let browserTabs = await tabs.query({ active: true, currentWindow: true }); + + let tab = browserTabs[0]; + if (tab && tab.url) { + updateIcon(getHostname(tab.url)); + } +}