Skip to content

Commit

Permalink
fix: rest of issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nemmtor committed Oct 1, 2024
1 parent c7c36b9 commit 94580fd
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 263 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ module.exports = {
rules: [
{
from: ['runtime'],
allow: ['infrastructure'],
allow: ['infrastructure', 'shared'],
},
{
from: ['host'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ExtensionAddressBookSettingsStorageKeys,
ExtensionGovernanceSettingsStorageKeys,
ExtensionIntegrationSettingsStorageKeys,
ExtensionAddressBookSettingName,
ExtensionGovernanceSettingName,
ExtensionIntegrationSettingName,
ExtensionSettingsStorageKey,
} from 'shared/extension';

import { SettingListItem, SettingListItemsGroup } from './types';

const addressBookSettings: SettingListItem<ExtensionAddressBookSettingsStorageKeys>[] =
const addressBookSettings: SettingListItem<ExtensionAddressBookSettingName>[] =
[
{
label: 'Tipping',
Expand All @@ -19,23 +19,22 @@ const addressBookSettings: SettingListItem<ExtensionAddressBookSettingsStorageKe
},
];

const governanceSettings: SettingListItem<ExtensionGovernanceSettingsStorageKeys>[] =
[
{
label: 'Agora',
storageKey: 'agora-enabled',
},
{
label: 'Snapshot',
storageKey: 'snapshot-enabled',
},
{
label: 'Tally',
storageKey: 'tally-enabled',
},
];
const governanceSettings: SettingListItem<ExtensionGovernanceSettingName>[] = [
{
label: 'Agora',
storageKey: 'agora-enabled',
},
{
label: 'Snapshot',
storageKey: 'snapshot-enabled',
},
{
label: 'Tally',
storageKey: 'tally-enabled',
},
];

const integrationsSettings: SettingListItem<ExtensionIntegrationSettingsStorageKeys>[] =
const integrationsSettings: SettingListItem<ExtensionIntegrationSettingName>[] =
[
{
label: 'Polymarket',
Expand Down
85 changes: 70 additions & 15 deletions src/infrastructure/service-worker/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { WEB3_COMMAND_MAP } from 'shared/web3';
import { GITCOIN_DONATION_COMMAND_MAP } from 'application/gitcoin';
import { POLYMARKET_COMMAND_MAP } from 'application/polymarket';
import { SNAPSHOT_COMMAND_MAP } from 'application/snapshot';
import { EXTENSION_COMMAND_MAP } from 'shared/extension';
import {
DEFAULT_SETTINGS,
EXTENSION_BUTTON_CLICKED,
EXTENSION_COMMAND_MAP,
ExtensionSettingsManager,
} from 'shared/extension';
import {
createObservabilityScope,
OBESRVABILITY_COMMAND_MAP,
Expand Down Expand Up @@ -45,23 +50,24 @@ const COMMAND_MAP = {
export class ServiceWorker {
private observabilityScope: ObservabilityScope =
createObservabilityScope('service-worker');
private constructor(private environment: typeof chrome) {}
private constructor(private environment: typeof chrome) { }

static run(environment: typeof chrome, onInstalled: () => void) {
static run(environment: typeof chrome) {
const serviceWorker = new ServiceWorker(environment);
serviceWorker.subscribeToCommands();

// TODO: double check after refactoring rest of extension
serviceWorker.onInstalled(onInstalled);
// TODO: remove check after refactoring rest of extension
serviceWorker.onRestMessages();
serviceWorker.watchCommands();
serviceWorker.watchStartup();
serviceWorker.watchInstalled();
serviceWorker.watchPopupClick();
serviceWorker.watchLegacyMessages();
serviceWorker.watchWorkerError();
}

self.addEventListener('error', (event) => {
serviceWorker.observabilityScope.captureException(event.error);
});
keepAlive() {
return setInterval(this.environment.runtime.getPlatformInfo, 20e3);
}

subscribeToCommands() {
watchCommands() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.onMessage<SerializedCommand<unknown>>(
COMMAND_BUS_REQUEST_MESSAGE,
Expand Down Expand Up @@ -111,7 +117,7 @@ export class ServiceWorker {
}

// TODO: refactor
onRestMessages() {
watchLegacyMessages() {
this.environment.runtime.onMessage.addListener(
(request, _sender, sendResponse) => {
switch (request.type) {
Expand Down Expand Up @@ -206,7 +212,56 @@ export class ServiceWorker {
});
}

onInstalled(onInstalled: () => void) {
this.environment.runtime.onInstalled.addListener(onInstalled);
watchStartup() {
this.environment.runtime.onStartup.addListener(() => {
return this.keepAlive();
});
}

watchInstalled() {
this.environment.runtime.onInstalled.addListener(() => {
void ExtensionSettingsManager.getAllSettings().then((currentSettings) => {
void ExtensionSettingsManager.setSettings({
...DEFAULT_SETTINGS,
...currentSettings,
});
});
});
}

watchPopupClick() {
this.environment.action.onClicked.addListener(() => {
this.environment.tabs.query(
{ active: true, currentWindow: true },
(tabs) => {
const activeTab = tabs[0];
if (ServiceWorker.isValidTab(activeTab)) {
this.environment.tabs
.sendMessage(activeTab.id, {
type: EXTENSION_BUTTON_CLICKED,
})
.catch(console.error);
}
},
);
});
}

watchWorkerError() {
self.addEventListener('error', (event) => {
this.observabilityScope.captureException(event.error);
});
}

private static isValidTab = (
tab?: chrome.tabs.Tab,
): tab is chrome.tabs.Tab & { id: number } => {
return Boolean(
tab?.id &&
tab.url &&
tab.url?.length > 0 &&
!tab.url?.startsWith('chrome') &&
!tab.url?.startsWith('about'),
);
};
}
85 changes: 1 addition & 84 deletions src/runtime/chromium/service-worker.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,3 @@
import { ServiceWorker } from 'infrastructure/service-worker';
// eslint-disable-next-line boundaries/element-types
import { ACTIVE_TAB_CHANGED, EXTENSION_BUTTON_CLICKED } from 'shared/extension';

const keepAlive = () => {
return setInterval(chrome.runtime.getPlatformInfo, 20e3);
};

const isValidTab = (
tab?: chrome.tabs.Tab,
): tab is chrome.tabs.Tab & { id: number } => {
return Boolean(
tab?.id &&
tab.url &&
tab.url?.length > 0 &&
!tab.url?.startsWith('chrome') &&
!tab.url?.startsWith('about'),
);
};

/* Chromium Wallets
nkbihfbeogaeaoehlefnkodbefgpgknn - MetaMask
hnfanknocfeofbddgcijnmhnfnkdnaad - Coinbase
bfnaelmomeimhlpmgjnjophhpkkoljpa - Phantom
eajafomhmkipbjmfmhebemolkcicgfmd - Tally
bhhhlbepdkbapadjdnnojkbgioiodbic - Solflare
bgpipimickeadkjlklgciifhnalhdjhe - GeroWallet
fhbohimaelbohpjbbldcngcnapndodjp - Binance Wallet
aiifbnbfobpmeekipheeijimdpnlpgpp - TerraStation Wallet
ogcmjchbmdichlfelhmceldndgmgpcem - Bob Extension
hmeobnfnfcmdkdcmlblgagmfpfboieaf - XDEFI Wallet
dlcobpjiigpikoobohmabehhmhfoodbb - Argent X StarkNet Wallet
fnjhmkhhmkbjkkabndcnnogagogbneec - Ronin Wallet
*/
const onInstalled = () => {
chrome.contextMenus.create({
title: 'Open IDriss',
contexts: ['page'],
id: 'idriss-crypto-1',
documentUrlPatterns: [
'chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/*',
'chrome-extension://hnfanknocfeofbddgcijnmhnfnkdnaad/*',
'chrome-extension://bfnaelmomeimhlpmgjnjophhpkkoljpa/*',
'chrome-extension://eajafomhmkipbjmfmhebemolkcicgfmd/*',
'chrome-extension://bhhhlbepdkbapadjdnnojkbgioiodbic/*',
'chrome-extension://bgpipimickeadkjlklgciifhnalhdjhe/*',
'chrome-extension://fhbohimaelbohpjbbldcngcnapndodjp/*',
'chrome-extension://aiifbnbfobpmeekipheeijimdpnlpgpp/*',
'chrome-extension://ogcmjchbmdichlfelhmceldndgmgpcem/*',
'chrome-extension://hmeobnfnfcmdkdcmlblgagmfpfboieaf/*',
'chrome-extension://dlcobpjiigpikoobohmabehhmhfoodbb/*',
'chrome-extension://fnjhmkhhmkbjkkabndcnnogagogbneec/*',
],
});
};

chrome.tabs.onActivated.addListener(() => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];
if (isValidTab(activeTab)) {
chrome.tabs
.sendMessage(activeTab.id, {
type: ACTIVE_TAB_CHANGED,
})
.catch(console.error);
}
});
});

chrome.action.onClicked.addListener(() => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];
if (isValidTab(activeTab)) {
chrome.tabs
.sendMessage(activeTab.id, {
type: EXTENSION_BUTTON_CLICKED,
})
.catch(console.error);
}
});
});

chrome.runtime.onStartup.addListener(keepAlive);
keepAlive();

ServiceWorker.run(chrome, onInstalled);
ServiceWorker.run(chrome);
2 changes: 1 addition & 1 deletion src/runtime/firefox/service-worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ServiceWorker } from 'infrastructure/service-worker';

ServiceWorker.run(browser, () => {});
ServiceWorker.run(browser);
6 changes: 3 additions & 3 deletions src/shared/extension/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ManageExtensionSettingsCommand } from './manage-extension-settings';
import { ChangeExtensionSettingsCommand } from './manage-extension-settings';
import { GetServiceStatusCommand } from './get-service-status';

export const COMMAND_MAP = {
[GetServiceStatusCommand.name]: GetServiceStatusCommand,
[ManageExtensionSettingsCommand.name]: ManageExtensionSettingsCommand,
[ChangeExtensionSettingsCommand.name]: ChangeExtensionSettingsCommand,
};

export { GetServiceStatusCommand } from './get-service-status';
export { ManageExtensionSettingsCommand } from './manage-extension-settings';
export { ChangeExtensionSettingsCommand as ManageExtensionSettingsCommand } from './manage-extension-settings';
21 changes: 5 additions & 16 deletions src/shared/extension/commands/manage-extension-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,24 @@ interface Payload {
settings: Partial<ExtensionSettings>;
}

type Response = ExtensionSettings;
type Response = boolean;

export class ManageExtensionSettingsCommand extends Command<Payload, Response> {
public readonly name = 'ManageExtensionSettingsCommand' as const;
export class ChangeExtensionSettingsCommand extends Command<Payload, Response> {
public readonly name = 'ChangeExtensionSettingsCommand' as const;

constructor(public payload: Payload) {
super();
}

async handle() {
chrome.storage.local
.set({ cacheInvalidate: Date.now() })
.catch(console.error);
const allStorage = await chrome.storage.local.get(null);
for (const x of Object.keys(allStorage).filter((x) => {
return x.startsWith('cache[');
})) {
await chrome.storage.local.remove(x);
continue;
}
let allSettings = await ExtensionSettingsManager.getAllSettings();
const allSettings = await ExtensionSettingsManager.getAllSettings();

// as payload.settings can be just partial of ExtensionSettings
// we merge it with the previous state to not lose any setting
await ExtensionSettingsManager.setSettings({
...allSettings,
...this.payload.settings,
});
allSettings = await ExtensionSettingsManager.getAllSettings();
return new OkResult(allSettings);
return new OkResult(true);
}
}
36 changes: 13 additions & 23 deletions src/shared/extension/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExtensionSettings } from './types';

export const GET_EXTENSION_SETTINGS_REQUEST = 'GET_EXTENSION_SETTINGS_REQUEST';
export const GET_EXTENSION_SETTINGS_RESPONSE =
'GET_EXTENSION_SETTINGS_RESPONSE';
Expand All @@ -8,29 +10,17 @@ export const EXTENSION_POPUP_ROUTE = {
SETTINGS_CUSTOMIZATION: '/settings/customization',
};

export const extensionAddressBookSettingsStorageKeys = [
'idriss-send-enabled',
'wallet-lookup-enabled',
] as const;

export const extensionGovernanceSettingsStorageKeys = [
'snapshot-enabled',
'tally-enabled',
'agora-enabled',
] as const;

export const extensionIntegrationSettingsStorageKeys = [
'polymarket-enabled',
'gitcoin-enabled',
] as const;

export const extensionSettingsStorageKeys = [
'entire-extension-enabled',
...extensionAddressBookSettingsStorageKeys,
...extensionGovernanceSettingsStorageKeys,
...extensionIntegrationSettingsStorageKeys,
] as const;

export const SETTINGS_STORAGE_KEY = 'EXTENSION_SETTINGS';
export const EXTENSION_BUTTON_CLICKED = 'EXTENSION_BUTTON_CLICKED';
export const ACTIVE_TAB_CHANGED = 'ACTIVE_TAB_CHANGED';

export const DEFAULT_EXTENSION_SETTINGS: ExtensionSettings = {
'agora-enabled': true,
'tally-enabled': true,
'gitcoin-enabled': true,
'snapshot-enabled': true,
'polymarket-enabled': true,
'idriss-send-enabled': true,
'wallet-lookup-enabled': true,
'entire-extension-enabled': true,
};
Loading

0 comments on commit 94580fd

Please sign in to comment.