-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
132 lines (109 loc) · 4.22 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
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
let config = {
settings: {
opened: false,
}
}
const gearButton = document.querySelector("#popup-button");
const modal = document.querySelector("#modal")
gearButton.addEventListener("click", () => {
if (config.settings.opened){
modal.style.display = "hidden";
modal.style.zIndex = -1;
modal.style.opacity = 0;
modal.style.transform = "scale(1.1)";
config.settings.opened = false;
}
else {
modal.style.zIndex = 10;
modal.style.display = "block";
modal.style.opacity = 1.0;
modal.style.transform = "scale(1.0)";
config.settings.opened = true;
}
})
class Member {
constructor(username, uuid, badge){
this.username = username;
this.uiid = uuid;
this.badge = badge;
}
}
class Lobby {
constructor(){
this.host = new Member;
this.hostNicknameElement = document.querySelector(".host-nickname");
this.hostBadgeElement = document.querySelector(".badge");
this.members = [];
this.htmlList = document.querySelector(".lobby-list");
this.rowTemplate = document.querySelector(".template-lobby-row");
}
loadSession(){
let host = JSON.parse(window.localStorage.getItem("host"))
if(host === null){ this.endSession(); return;};
let memberlist = JSON.parse(window.localStorage.getItem("member-list")) || [];
this.addHost(host.username, host.uuid, host.badge);
memberlist.forEach((member) => {
this.addUser(member);
})
}
addHost(username,uuid,badge){
this.host = new Member(username, uuid, badge);
this.hostNicknameElement.innerHTML = `Host: <b><i>${username}</i></b>`;
this.hostBadgeElement.style.display = "block";
this.hostBadgeElement.setAttribute("class", "badge")
this.hostBadgeElement.innerHTML = badge;
window.localStorage.setItem("host", JSON.stringify(this.host))
}
endSession(){
chrome.storage.local.clear();
}
kickUserByUiid(uuid){
document.getElementById(`${uuid}`).remove();
let objIndex = this.members.findIndex((member) => {
return member.uuid === uuid;
})
if (!objIndex){
console.log(`No user with ${uuid} was found `);
return;
}
this.members.slice(objIndex, 1);
}
addUser(username, uuid, badge){
this.members.push(new Member(username, uuid, badge));
// HTML //
let userElement = this.rowTemplate.content.cloneNode(true);
let usernameSpan = userElement.querySelector(".lobby-username");
usernameSpan.textContent = username;
let userUuid = userElement.querySelector(".lobby-row");
userUuid.id = uuid;
this.htmlList.prepend(userElement);
window.localStorage.setItem("member-list", JSON.stringify(this.members));
}
}
const lobby = new Lobby();
lobby.htmlList.addEventListener("click", (e) => {
if(!e.target.classList.contains("kick-user")) return;
let rowElement = e.target.closest(".lobby-row");
let uuid = rowElement.id;
lobby.kickUserByUiid(uuid);
})
lobby.htmlList.addEventListener("mouseover", (e) => {
if(!(e.target.classList.contains("lobby-row") ||
e.target.classList.contains("lobby-user-actions") ||
e.target.classList.contains("kick-user"))) return;
let rowElement = e.target.closest(".lobby-row");
let userActionseElement = rowElement.querySelector(".lobby-user-actions");
userActionseElement.style.display = "block";
})
lobby.htmlList.addEventListener("mouseout", (e) => {
if(!(e.target.classList.contains("lobby-row") ||
e.target.classList.contains("lobby-user-actions") ||
e.target.classList.contains("kick-user"))) return;
let rowElement = e.target.closest(".lobby-row");
let userActionseElement = rowElement.querySelector(".lobby-user-actions");
userActionseElement.style.display = "none";
})
window.addEventListener('beforeunload', function (e) {
lobby.endSession();
});
lobby.loadSession();