forked from samuraiexx/roll_together
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
75 lines (62 loc) · 2.29 KB
/
popup.js
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
const background = chrome.extension.getBackgroundPage();
let connected = false;
const createRoomButton = document.getElementById('createRoom');
const copyIdButton = document.getElementById('copyId');
const disconnectButton = document.getElementById('disconnect');
const idInput = document.getElementById('idInput');
let optionButtons = document.getElementsByClassName('actionButton');
getExtensionColor().then(color => {
for (button of optionButtons) {
log("Color of " + button.id + " is now " + color);
button.style.backgroundColor = color;
}
});
function update() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const tab = tabs[0];
const roomId = background.window.getRoomId(tab.id);
const connected = roomId != null;
log('Updating Popup...', { roomId, connected });
if (connected) {
idInput.value = roomId;
idInput.focus();
idInput.select();
[...document.getElementsByClassName('firstPage')].forEach(el => el.style.display = 'none');
[...document.getElementsByClassName('secondPage')].forEach(el => el.style = {});
} else {
getDefaultRoomId().then(roomId => {
log('Setting roomId field');
idInput.value = roomId;
});
[...document.getElementsByClassName('firstPage')].forEach(el => el.style = {});
[...document.getElementsByClassName('secondPage')].forEach(el => el.style.display = 'none');
}
});
}
window.addEventListener('beforeunload', () => {
background.window.updatePopup = null;
});
/* Room creation is initiated here. createRoom is assigned the sendConnectionRequestToWebpage
* function from background.js */
createRoomButton.onclick = async function () {
log('Clicking CreateRoomButton');
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
background.window.createRoom(tabs[0], idInput.value);
});
};
copyIdButton.onclick = function () {
log('Clicking CopyIdButton');
idInput.focus();
idInput.select();
document.execCommand('copy');
}
disconnectButton.onclick = function () {
log('Clicking DisconnectButton');
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
background.window.disconnectRoom(tabs[0].id);
})
}
idInput.onclick = function () {
}
background.window.updatePopup = update;
update();