-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
messages.ts
67 lines (57 loc) · 1.21 KB
/
messages.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import browser from 'webextension-polyfill';
import { SignedOutAction } from './pages/Popup/stateMachine';
export enum MessageType {
Autofill,
LogInRequest,
LogInResponse,
GenerateRequest,
GenerateResponse,
ReservationRequest,
ReservationResponse,
ActiveInputElementWrite,
}
export type Message<T> = {
type: MessageType;
data: T;
};
export type ReservationRequestData = {
hme: string;
label: string;
elementId: string;
};
export type GenerationResponseData = {
hme?: string;
elementId: string;
error?: string;
};
export type ActiveInputElementWriteData = {
text: string;
copyToClipboard: boolean;
};
export type ReservationResponseData = GenerationResponseData;
export type LogInRequestData = {
email: string;
password: string;
};
export type LogInResponseData = {
success: boolean;
action?: SignedOutAction;
};
export const sendMessageToTab = async (
type: MessageType,
data: unknown,
tab?: browser.Tabs.Tab
): Promise<void> => {
if (tab === undefined) {
[tab] = await browser.tabs.query({
active: true,
lastFocusedWindow: true,
});
}
if (tab.id !== undefined) {
await browser.tabs.sendMessage(tab.id, {
type,
data,
});
}
};