-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.ts
More file actions
130 lines (117 loc) · 3.58 KB
/
Copy pathbackground.ts
File metadata and controls
130 lines (117 loc) · 3.58 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { log } from "./common/helper/log";
import {
GetLoginStatusMessage,
GetCurrentChromeUserToken,
OpenLoginWindowMessage,
StoreUserTokenMessage,
SettingsSyncMessage,
SettingsObject,
MESSAGE_TYPE,
} from "./common/types/messaging";
export {};
function getOAuthToken() {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({ interactive: true }, function (token) {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(token);
}
});
});
}
function getScreenSize() {
return new Promise<{ width: number; height: number }>((resolve, reject) => {
chrome.windows.getCurrent((window) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve({ width: window.width || 0, height: window.height || 0 });
}
});
});
}
function broadcastSettings(settings: SettingsObject) {
chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) {
if (!tab?.id) continue;
// Tabs without our content script (chrome://, web store, freshly
// installed pre-extension tabs, etc.) reject with "Receiving end does
// not exist". That's expected for a fire-and-forget broadcast.
// Cast: @types/chrome@0.0.193 still types tabs.sendMessage as void;
// in MV3 the no-callback overload returns a Promise.
(
chrome.tabs.sendMessage(tab.id, {
type: MESSAGE_TYPE.SYNC_SETTINGS,
settings,
}) as unknown as Promise<unknown>
).catch(() => {});
}
});
}
chrome.runtime.onMessage.addListener(function (request, _sender, sendResponse) {
log("background: onMessage", request);
// Get login status
if (GetLoginStatusMessage.is(request)) {
chrome.storage.sync.get("token").then(({ token }) => {
sendResponse({ status: !!token, token: token || null });
});
}
// Store user token
else if (StoreUserTokenMessage.is(request)) {
if (request.token === null) {
chrome.storage.sync.remove("token").then(() => {
sendResponse({ status: true });
});
} else {
chrome.storage.sync.set({ token: request.token }).then(() => {
sendResponse({ status: true });
});
}
}
// Get current chrome user token
if (GetCurrentChromeUserToken.is(request)) {
getOAuthToken()
.then((token) => {
sendResponse({ status: !!token, token: token || null });
})
.catch((err) => {
sendResponse({ status: false, token: null });
});
}
// Open Login popup
else if (OpenLoginWindowMessage.is(request)) {
getScreenSize().then((screen) => {
const width = 500;
const height = 600;
const left = screen.width / 2 - width / 2;
const top = screen.height / 2 - height / 2;
chrome.windows.create({
url: chrome.runtime.getURL("popup.html") + "#/login",
type: "popup",
width: width,
height: height,
focused: true,
left: Math.round(left),
top: Math.round(top),
});
});
}
// Handle SYNC_SETTINGS
else if (SettingsSyncMessage.is(request)) {
if (request.settings) {
// Save settings and broadcast
chrome.storage.local.set({ settings: request.settings }).then(() => {
if (request.settings) broadcastSettings(request.settings);
sendResponse({ status: true });
});
} else {
// Return current settings
chrome.storage.local.get("settings").then((data) => {
const settings = new SettingsSyncMessage(data.settings);
sendResponse(settings);
});
}
}
return true;
});