-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
193 lines (169 loc) · 4.36 KB
/
Copy pathbackground.js
File metadata and controls
193 lines (169 loc) · 4.36 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const STORAGE_KEYS = {
serverUrl: "degoogFplayServerUrl",
password: "degoogFplayPassword",
};
let ws;
let reconnectTimer = null;
let connectGeneration = 0;
let config = { serverUrl: "", password: "" };
function normalizeWebSocketUrl(input) {
const trimmed = input.trim();
if (!trimmed) return "";
let u = trimmed;
if (/^https:\/\//i.test(u)) u = u.replace(/^https:/i, "wss:");
else if (/^http:\/\//i.test(u)) u = u.replace(/^http:/i, "ws:");
else if (!/^wss?:\/\//i.test(u)) u = `ws://${u}`;
try {
new URL(u);
return u;
} catch {
return "";
}
}
async function loadConfig() {
const data = await chrome.storage.local.get([
STORAGE_KEYS.serverUrl,
STORAGE_KEYS.password,
]);
config = {
serverUrl:
typeof data[STORAGE_KEYS.serverUrl] === "string"
? data[STORAGE_KEYS.serverUrl]
: "",
password:
typeof data[STORAGE_KEYS.password] === "string"
? data[STORAGE_KEYS.password]
: "",
};
}
function clearReconnectTimer() {
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
}
function scheduleReconnect() {
clearReconnectTimer();
reconnectTimer = setTimeout(() => {
reconnectTimer = null;
connect();
}, 3000);
}
function detachWebSocket(socket) {
if (!socket) return;
socket.onclose = null;
socket.onerror = null;
socket.onmessage = null;
socket.onopen = null;
try {
socket.close();
} catch {}
}
function connect() {
clearReconnectTimer();
const gen = ++connectGeneration;
const url = normalizeWebSocketUrl(config.serverUrl);
if (!url) {
if (ws) {
const old = ws;
ws = undefined;
detachWebSocket(old);
}
return;
}
if (ws) {
const old = ws;
ws = undefined;
detachWebSocket(old);
}
let socket;
try {
socket = new WebSocket(url);
} catch {
scheduleReconnect();
return;
}
ws = socket;
socket.onopen = () => {
if (gen !== connectGeneration) return;
if (config.password) {
socket.send(JSON.stringify({ type: "auth", password: config.password }));
}
};
socket.onmessage = async (event) => {
if (gen !== connectGeneration) return;
let msg;
try {
msg = JSON.parse(event.data);
} catch {
return;
}
if (msg.type === "auth_ok") {
return;
}
if (msg.type === "ping") {
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ type: "pong" }));
}
return;
}
if (msg.type === "get_session") {
try {
const tab = await chrome.tabs.create({ url: msg.url, active: false });
await new Promise((resolve) => {
chrome.tabs.onUpdated.addListener(function listener(tabId, info) {
if (tabId === tab.id && info.status === "complete") {
chrome.tabs.onUpdated.removeListener(listener);
resolve();
}
});
});
const cookies = await chrome.cookies.getAll({ url: msg.url });
await chrome.tabs.remove(tab.id);
if (gen === connectGeneration && socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ type: "session", id: msg.id, cookies }));
}
} catch (e) {
if (gen === connectGeneration && socket.readyState === WebSocket.OPEN) {
socket.send(
JSON.stringify({
type: "error",
id: msg.id,
error: e.message,
}),
);
}
}
}
};
socket.onclose = () => {
if (gen !== connectGeneration) return;
if (ws === socket) ws = undefined;
scheduleReconnect();
};
socket.onerror = () => {
if (gen !== connectGeneration) return;
socket.close();
};
}
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "local") return;
if (
Object.prototype.hasOwnProperty.call(changes, STORAGE_KEYS.serverUrl) ||
Object.prototype.hasOwnProperty.call(changes, STORAGE_KEYS.password)
) {
loadConfig().then(() => connect());
}
});
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg.type === "getStatus") {
const normalized = normalizeWebSocketUrl(config.serverUrl);
sendResponse({
ready: ws !== undefined && ws.readyState === WebSocket.OPEN,
hasUrl: Boolean(normalized),
});
return true;
}
return false;
});
loadConfig().then(() => connect());