-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.ts
71 lines (67 loc) · 1.64 KB
/
background.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
68
69
70
71
const HOST = 'https://poom.live:9091'
const MODE_CLIENT = 'client'
const MODE_HOST = 'host'
const MODE_OFF = 'old_session'
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
var userLink = changeInfo.url
if (userLink === undefined) {
// skip
return
}
chrome.storage.local.get(({ mode, id, host_last_send_link }) => {
if (mode !== MODE_HOST) {
return // skip
}
if (host_last_send_link === userLink) {
// link was not change, skip
return
}
const body = JSON.stringify({
"id": id,
"url": userLink,
})
fetch(`${HOST}/api/v1/meeting`, {
method: 'PUT',
body,
}).then((res) => {
if (res.status === 204) {
chrome.storage.local.set({ host_last_send_link: userLink })
}
if (res.status !== 204) {
chrome.storage.local.remove(['mode', 'id', 'host_last_send_link'])
}
})
})
});
window.setInterval(function () {
chrome.storage.local.get(({ mode, id, client_last_get_link }) => {
if (mode !== MODE_CLIENT) {
return // skip
}
const body = JSON.stringify({
"id": id,
})
fetch(`${HOST}/api/v1/meeting`, {
method: 'POST',
body: body
})
.then((response) => {
if (response.status === 404) {
chrome.storage.local.remove(['mode', 'id', 'client_last_get_link'])
return { url: '' }
} else if (response.status == 200) {
// send json response with URL
return response.json()
} else {
return { url: '' }
}
})
.then(({ url }) => {
if (client_last_get_link !== url) {
// link has changed
chrome.storage.local.set({ client_last_get_link: url })
chrome.tabs.update({ active: true, url: url });
}
})
})
}, 1000 * 2);