-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathutils.ts
45 lines (39 loc) · 1.14 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Env, MessageSender } from "router/types";
import { LocalStorage } from "storage";
import browser from "webextension-polyfill";
const NO_TAB_ID = -2;
export const getNamadaRouterId = async (
store: LocalStorage
): Promise<number> => {
const storedId = await store.getRouterId();
if (!storedId) {
const id = Math.floor(Math.random() * 1000000);
await store.setRouterId(id);
return id;
}
return storedId;
};
export const initNamadaLatestSyncBlock = async (
store: LocalStorage
): Promise<void> => {
const latestBlock = await store.getLatestSyncBlock();
if (!latestBlock) {
await store.setLatestSyncBlock({ lastestSyncBlock: 0 });
}
};
// Determine if content-scripts can be executed in this environment
export class ContentScriptEnv {
static readonly produceEnv = (sender: MessageSender): Env => {
const isInternalMsg = sender.id === browser.runtime.id;
const senderTabId = sender.tab?.id || NO_TAB_ID;
return {
isInternalMsg,
senderTabId,
requestInteraction: () => {
throw new Error(
"ContentScriptEnv doesn't support `requestInteraction`"
);
},
};
};
}