Skip to content

Commit 1bcbe7b

Browse files
committed
feat: settings button rework to be less complex
Hopefully this fixes some issues people were running into with the settings button. This avoids us injecting the settings at all into the webpage.
1 parent c42b870 commit 1bcbe7b

File tree

7 files changed

+80
-112
lines changed

7 files changed

+80
-112
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pocketuniverse",
3-
"version": "0.0.23",
3+
"version": "0.0.24",
44
"description": "A transaction checker that helps you avoid crypto scams.",
55
"scripts": {
66
"build:chrome": "TARGET_BROWSER=chrome node utils/build.js ",

src/lib/request.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@ export class RequestManager {
119119
public request(
120120
args:
121121
| {
122-
chainId: string;
123-
transaction: Transaction;
124-
}
122+
chainId: string;
123+
transaction: Transaction;
124+
}
125125
| {
126-
chainId: string;
127-
domain: any;
128-
message: any;
129-
primaryType: string;
130-
}
126+
chainId: string;
127+
domain: any;
128+
message: any;
129+
primaryType: string;
130+
}
131131
| {
132-
chainId: string;
133-
hash: string;
134-
}
132+
chainId: string;
133+
hash: string;
134+
}
135135
): Promise<Response> {
136136
return new Promise((resolve) => {
137137
let request: RequestArgs;
@@ -200,7 +200,7 @@ const DISPATCH_REQUEST = 'POCKET_UNIVERSE_DISPATCH_REQUEST';
200200
* Listen to request
201201
*/
202202
export const listenToRequest = (callback: (request: RequestArgs) => void) => {
203-
document.addEventListener(DISPATCH_REQUEST, (event: any) => {
203+
document.addEventListener(DISPATCH_REQUEST, async (event: any) => {
204204
callback(event.detail);
205205
});
206206
};

src/lib/settings.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/lib/storage.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ export const updateSimulationState = async (
114114
simulations = simulations.map((x: StoredSimulation) =>
115115
x.id === id
116116
? {
117-
...x,
118-
state,
119-
}
117+
...x,
118+
state,
119+
}
120120
: x
121121
);
122122

@@ -131,10 +131,10 @@ const updateSimulatioWithErrorMsg = async (id: string, error?: string) => {
131131
simulations = simulations.map((x: StoredSimulation) =>
132132
x.id === id
133133
? {
134-
...x,
135-
error,
136-
state: StoredSimulationState.Error,
137-
}
134+
...x,
135+
error,
136+
state: StoredSimulationState.Error,
137+
}
138138
: x
139139
);
140140

@@ -212,7 +212,9 @@ export const clearOldSimulations = async () => {
212212
return browser.storage.local.set({ simulations });
213213
};
214214

215-
export const SETTINGS_KEY = 'settings';
215+
// NOTE: be cautious changing this variable. We need to update the variables in setSettings and getSettings if we do.
216+
// TODO(jqphu): fix this.
217+
export const SETTINGS_KEY = 'pocket_universe_settings';
216218

217219
export interface Settings {
218220
/**
@@ -236,28 +238,28 @@ const updateIcon = (settings: Settings) => {
236238
*/
237239
export const setSettings = async (args: Settings) => {
238240
// Default is enabled.
239-
let { settings = { disable: false } } = await browser.storage.sync.get(
241+
let { pocket_universe_settings = { disable: false } } = await browser.storage.local.get(
240242
SETTINGS_KEY
241243
);
242-
log.info({ settings: settings, msg: 'Updating settings' });
244+
log.info({ settings: pocket_universe_settings, msg: 'Updating settings' });
243245

244-
settings.disable = args.disable;
246+
pocket_universe_settings.disable = args.disable;
245247

246-
updateIcon(settings);
248+
updateIcon(pocket_universe_settings);
247249

248-
return browser.storage.sync.set({ settings });
250+
return browser.storage.local.set({ [SETTINGS_KEY]: pocket_universe_settings });
249251
};
250252

251253
/**
252254
* Get the settings.
253255
*/
254256
export const getSettings = async (): Promise<Settings> => {
255-
const { settings = { disable: false } } = await browser.storage.sync.get(
257+
const { pocket_universe_settings = { disable: false } } = await browser.storage.local.get(
256258
SETTINGS_KEY
257259
);
258-
log.info({ settings: settings, msg: 'Getting settings.' });
260+
log.info({ settings: pocket_universe_settings, msg: 'Getting settings.' });
259261

260-
return settings as Settings;
262+
return pocket_universe_settings as Settings;
261263
};
262264

263265
/**

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"__firefox__manifest_version": 2,
44

55
"name": "Pocket Universe",
6-
"version": "0.0.23",
6+
"version": "0.0.24",
77
"description": "A transaction checker that helps you avoid crypto scams.",
88
"background": {
99
"__chrome__service_worker": "background.bundle.js",

src/pages/Content/index.ts

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
REQUEST_COMMAND,
77
Response,
88
} from '../../lib/request';
9-
import { dispatchSettings } from '../../lib/settings';
109
import type { StoredSimulation } from '../../lib/storage';
11-
import { removeSimulation, StoredSimulationState } from '../../lib/storage';
10+
import { Settings, getSettings, removeSimulation, StoredSimulationState } from '../../lib/storage';
1211

1312
import browser from 'webextension-polyfill';
1413

@@ -39,50 +38,58 @@ const maybeRemoveId = (id: string) => {
3938
}
4039
};
4140

42-
browser.storage.onChanged.addListener((changes, area) => {
43-
if (area === 'sync' && changes.settings?.newValue) {
44-
dispatchSettings(changes.settings.newValue);
45-
}
46-
});
47-
48-
listenToRequest((request: RequestArgs) => {
41+
listenToRequest(async (request: RequestArgs) => {
4942
log.info({ request }, 'Request');
5043
ids.push(request.id);
5144

52-
// Page has sent an event, start listening to storage changes.
53-
// This ensures we don't listen to storage changes on every singel webpage.
54-
browser.storage.onChanged.addListener((changes, area) => {
55-
if (area === 'local' && changes.simulations?.newValue) {
56-
const newSimulations = changes.simulations.newValue;
57-
log.info(newSimulations, 'Dispatching new values for simulation');
45+
getSettings().then((args: Settings) => {
46+
if (args.disable) {
5847

59-
// Note: this will dispatch to **all** content pages.
60-
// To address this later we can generate a random id for each page. Append it to the request.
61-
// Either way, this should be pretty cheap. It's just DOM communication.
62-
// TODO(jqphu): measure & generate random id's so we don't dispatch so many events.
63-
newSimulations.forEach((simulation: StoredSimulation) => {
64-
// Either dispatch the corresponding event, or push the item to new simulations.
65-
if (simulation.state === StoredSimulationState.Confirmed) {
66-
log.debug('Dispatch confirmed', simulation.id);
67-
dispatchResponse({
68-
id: simulation.id,
69-
type: Response.Continue,
70-
});
71-
maybeRemoveId(simulation.id);
72-
} else if (simulation.state === StoredSimulationState.Rejected) {
73-
log.debug('Dispatch rejected', simulation.id);
74-
dispatchResponse({
75-
id: simulation.id,
76-
type: Response.Reject,
77-
});
78-
maybeRemoveId(simulation.id);
79-
}
48+
// Immediately respond continue.
49+
dispatchResponse({
50+
id: request.id,
51+
type: Response.Continue,
8052
});
53+
54+
return;
8155
}
82-
});
8356

84-
browser.runtime.sendMessage({
85-
command: REQUEST_COMMAND,
86-
data: request,
87-
});
57+
// Page has sent an event, start listening to storage changes.
58+
// This ensures we don't listen to storage changes on every singel webpage.
59+
browser.storage.onChanged.addListener((changes, area) => {
60+
if (area === 'local' && changes.simulations?.newValue) {
61+
const newSimulations = changes.simulations.newValue;
62+
log.info(newSimulations, 'Dispatching new values for simulation');
63+
64+
// Note: this will dispatch to **all** content pages.
65+
// To address this later we can generate a random id for each page. Append it to the request.
66+
// Either way, this should be pretty cheap. It's just DOM communication.
67+
// TODO(jqphu): measure & generate random id's so we don't dispatch so many events.
68+
newSimulations.forEach((simulation: StoredSimulation) => {
69+
// Either dispatch the corresponding event, or push the item to new simulations.
70+
if (simulation.state === StoredSimulationState.Confirmed) {
71+
log.debug('Dispatch confirmed', simulation.id);
72+
dispatchResponse({
73+
id: simulation.id,
74+
type: Response.Continue,
75+
});
76+
maybeRemoveId(simulation.id);
77+
} else if (simulation.state === StoredSimulationState.Rejected) {
78+
log.debug('Dispatch rejected', simulation.id);
79+
dispatchResponse({
80+
id: simulation.id,
81+
type: Response.Reject,
82+
});
83+
maybeRemoveId(simulation.id);
84+
}
85+
});
86+
}
87+
});
88+
89+
browser.runtime.sendMessage({
90+
command: REQUEST_COMMAND,
91+
data: request,
92+
});
93+
94+
})
8895
});

src/pages/Injected/index.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ let POCKET = `
2828

2929
import logger from '../../lib/logger';
3030
import { RequestManager, Response } from '../../lib/request';
31-
import { settings, listenForSettingsUpdates } from '../../lib/settings';
3231
import { ethErrors } from 'eth-rpc-errors';
3332

3433
declare global {
@@ -42,7 +41,6 @@ log.debug({ msg: 'Injected script loaded.' });
4241

4342
/// Handling all the request communication.
4443
const REQUEST_MANAGER = new RequestManager();
45-
listenForSettingsUpdates();
4644

4745
// Heavily taken from RevokeCash to ensure consistency. Thanks Rosco :)!
4846
//
@@ -76,13 +74,6 @@ const sendHandler = {
7674

7775
const requestHandler = {
7876
apply: async (target: any, thisArg: any, args: any[]) => {
79-
/*
80-
* User has disabled PU, just reflect.
81-
*/
82-
if (settings.disable) {
83-
return Reflect.apply(target, thisArg, args);
84-
}
85-
8677
const [request] = args;
8778
if (!request) {
8879
return Reflect.apply(target, thisArg, args);
@@ -166,13 +157,6 @@ const requestHandler = {
166157

167158
const sendAsyncHandler = {
168159
apply: async (target: any, thisArg: any, args: any[]) => {
169-
/*
170-
* User has disabled PU, just reflect.
171-
*/
172-
if (settings.disable) {
173-
return Reflect.apply(target, thisArg, args);
174-
}
175-
176160
const [request, callback] = args;
177161
if (!request) {
178162
return Reflect.apply(target, thisArg, args);

0 commit comments

Comments
 (0)