Skip to content

Commit

Permalink
🐛 Fix extension icon switch
Browse files Browse the repository at this point in the history
  • Loading branch information
ssakone committed Dec 29, 2023
1 parent dc1c8cc commit b05de96
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
26 changes: 23 additions & 3 deletions src/lib/controllers/browser.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getDuration } from '$lib/utility';
import { getDuration, urlToDomain } from '$lib/utility';
import { web } from '$lib/utility';

import type { Browser } from '$lib/types';
import type { Browser, Profile, WebSite } from '$lib/types';
import { backgroundController } from './background.controller';

const createBrowserController = (): Browser => {
const get = async (key: string): Promise<{ [key: string]: unknown }> => {
Expand Down Expand Up @@ -57,6 +58,23 @@ const createBrowserController = (): Browser => {
});
});
};
const switchIcon = async (activeInfo: { tabId: number }) => {
const tab = await web.tabs.get(activeInfo.tabId);
const user: Profile = await backgroundController().getUserProfile();
const domain = urlToDomain(tab.url || '');
const webSites = user.data?.webSites as { [key: string]: WebSite };
if (webSites !== undefined && domain in webSites) {
web.action.setIcon({
tabId: tab.id,
path: 'assets/logo-on.png'
});
} else {
web.action.setIcon({
tabId: tab.id,
path: 'assets/logo-off.png'
});
}
};
const createWindow = async (url: string): Promise<chrome.windows.Window> => {
return web.windows.create({
url: web.runtime.getURL(url),
Expand All @@ -72,6 +90,7 @@ const createBrowserController = (): Browser => {
url: string | undefined,
requestId: string | undefined
) => {
getCurrentTab().then((tab) => switchIcon({ tabId: tab.id as number }));
return web.runtime.sendMessage({
prompt: true,
response: {
Expand All @@ -97,7 +116,8 @@ const createBrowserController = (): Browser => {
injectJsInTab,
injectJsinAllTabs,
createWindow,
sendAuthorizationResponse
sendAuthorizationResponse,
switchIcon
};
};

Expand Down
3 changes: 3 additions & 0 deletions src/lib/services/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getDuration } from '../utility/utils';
import { userProfile } from '$lib/stores/data';
import { ProfileUtil } from '$lib/utility';
import { profileController } from '$lib/controllers/profile.controller';
import { browserController } from '$lib/controllers';

const accept = async (accept: boolean, domain: string, choice: number = 0) => {
try {
Expand Down Expand Up @@ -32,6 +33,8 @@ const accept = async (accept: boolean, domain: string, choice: number = 0) => {
}
});

browserController.getCurrentTab().then((tab) => browserController.switchIcon({ tabId: tab.id as number }));

profileController.saveProfile(get(userProfile));

return true;
Expand Down
23 changes: 4 additions & 19 deletions src/lib/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,17 @@ import { get } from 'svelte/store';
import { profileController } from '$lib/controllers/profile.controller';
import { sessionController } from '$lib/controllers/session.controller';
import { backgroundController } from '$lib/controllers/background.controller';
import { browserController } from '$lib/controllers';

const background = backgroundController();
const session = sessionController();

const switchIcon = async (activeInfo: { tabId: number }) => {
const tab = await web.tabs.get(activeInfo.tabId);

const user: Profile = await background.getUserProfile();
const domain = urlToDomain(tab.url || '');
const webSites = user.data?.webSites as { [key: string]: WebSite };
if (webSites !== undefined && domain in webSites) {
web.action.setIcon({
tabId: tab.id,
path: 'assets/logo-on.png'
});
} else {
web.action.setIcon({
tabId: tab.id,
path: 'assets/logo-off.png'
});
}
};

web.runtime.onInstalled.addListener(() => BrowserUtil.injectJsinAllTabs('content.js'));
web.runtime.onStartup.addListener(() => BrowserUtil.injectJsinAllTabs('content.js'));
web.tabs.onActivated.addListener(async (activeInfo) => switchIcon(activeInfo));
BrowserUtil.getCurrentTab().then((tab) => switchIcon({ tabId: tab.id as number }));
web.tabs.onActivated.addListener(async (activeInfo) => browserController.switchIcon(activeInfo));
BrowserUtil.getCurrentTab().then((tab) => browserController.switchIcon({ tabId: tab.id as number }));

const responders: Responders = {};
const requestQueue: any[] = [];
Expand Down Expand Up @@ -68,6 +52,7 @@ const makeResponse = async (type: string, data: any) => {
const user = get(userProfile);
const privateKey: string = user.data?.privateKey || '';
let res;

switch (type) {
case 'getPublicKey':
res = getPublicKey(privateKey);
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/profile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ interface Browser {
injectJsInTab: (tab: chrome.tabs.Tab, jsFileName: string) => Promise<void>;
injectJsinAllTabs: (jsFileName: string) => Promise<void>;
createWindow: (url: string) => Promise<chrome.windows.Window>;
switchIcon: (activeInfo: { tabId: number }) => Promise<void>;
sendAuthorizationResponse: (
yes: boolean,
choice: number,
Expand Down

0 comments on commit b05de96

Please sign in to comment.