-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
223 lines (192 loc) · 6.62 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
class YoBuddyApp {
constructor() {
this.initElements();
this.setupEventListeners();
this.setupAuthStateChanged();
}
initElements() {
this.authScreen = document.getElementById('auth-screen');
this.mainScreen = document.getElementById('main-screen');
this.roomControls = document.getElementById('room-controls');
this.activeRoom = document.getElementById('active-room');
this.signInButton = document.getElementById('sign-in');
this.signOutButton = document.getElementById('sign-out');
this.createRoomButton = document.getElementById('create-room');
this.joinRoomButton = document.getElementById('join-room');
this.leaveRoomButton = document.getElementById('leave-room');
this.copyCodeButton = document.getElementById('copy-code');
this.roomCodeInput = document.getElementById('room-code');
this.roomCodeDisplay = document.getElementById('room-code-display');
this.participantList = document.getElementById('participant-list');
this.userAvatar = document.getElementById('user-avatar');
this.userName = document.getElementById('user-name');
this.status = document.getElementById('status');
}
setupEventListeners() {
this.signInButton.addEventListener('click', () => this.signIn());
this.signOutButton.addEventListener('click', () => this.signOut());
this.createRoomButton.addEventListener('click', () => this.createRoom());
this.joinRoomButton.addEventListener('click', () => this.joinRoom());
this.leaveRoomButton.addEventListener('click', () => this.leaveRoom());
this.copyCodeButton.addEventListener('click', () => this.copyRoomCode());
}
setupAuthStateChanged() {
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.currentUser = user;
this.showMainScreen();
this.checkExistingRoom();
} else {
this.showAuthScreen();
this.currentUser = null;
this.currentRoom = null;
}
});
}
async signIn() {
try {
const provider = new firebase.auth.GoogleAuthProvider();
await firebase.auth().signInWithPopup(provider);
} catch (error) {
this.showStatus('Sign in failed: ' + error.message, 'error');
}
}
async signOut() {
try {
if (this.currentRoom) {
await this.leaveRoom();
}
await firebase.auth().signOut();
} catch (error) {
this.showStatus('Sign out failed: ' + error.message, 'error');
}
}
async createRoom() {
const roomCode = this.generateRoomCode();
const roomRef = firebase.database().ref(`rooms/${roomCode}`);
try {
await roomRef.set({
creator: this.currentUser.uid,
createdAt: Date.now(),
currentUrl: null
});
await this.joinRoomById(roomCode);
this.showStatus('Room created!', 'success');
} catch (error) {
this.showStatus('Failed to create room: ' + error.message, 'error');
}
}
async joinRoom() {
const roomCode = this.roomCodeInput.value.toUpperCase();
if (roomCode.length !== 6) {
this.showStatus('Invalid room code', 'error');
return;
}
await this.joinRoomById(roomCode);
}
async joinRoomById(roomCode) {
const roomRef = firebase.database().ref(`rooms/${roomCode}`);
try {
roomRef.once('value', (snapshot) => {
if (!snapshot.exists()) {
this.showStatus('Room not found', 'error');
return;
}
this.currentRoom = roomCode;
firebase.database().ref(`rooms/${roomCode}/participants/${this.currentUser.uid}`).set({
name: this.currentUser.displayName,
avatar: this.currentUser.photoURL,
joinedAt: Date.now()
});
this.setupRoomListeners(roomCode);
this.showActiveRoom(roomCode);
this.showStatus('Joined room!', 'success');
chrome.storage.local.set({ roomCode });
});
} catch (error) {
this.showStatus('Failed to join room: ' + error.message, 'error');
}
}
setupRoomListeners(roomCode) {
this.roomRef = firebase.database().ref(`rooms/${roomCode}`);
// Listen for URL updates
firebase.database().ref(`rooms/${roomCode}/currentUrl`).on('value', (snapshot) => {
const url = snapshot.val();
if (url) {
chrome.runtime.sendMessage({ type: 'URL_UPDATE', url });
}
});
// Listen for participant updates
firebase.database().ref(`rooms/${roomCode}/participants`).on('value', (snapshot) => {
this.updateParticipantList(snapshot.val());
});
}
removeRoomListeners() {
if (this.roomRef) {
this.roomRef.off();
}
}
updateParticipantList(participants) {
this.participantList.innerHTML = '';
if (participants) {
Object.values(participants).forEach(participant => {
const li = document.createElement('li');
li.innerHTML = `
<img src="${participant.avatar}" alt="${participant.name}" class="avatar-small">
<span>${participant.name}</span>
`;
this.participantList.appendChild(li);
});
}
}
showMainScreen() {
this.authScreen.classList.add('hidden');
this.mainScreen.classList.remove('hidden');
this.userAvatar.src = this.currentUser.photoURL;
this.userName.textContent = this.currentUser.displayName;
}
showAuthScreen() {
this.authScreen.classList.remove('hidden');
this.mainScreen.classList.add('hidden');
}
showActiveRoom(roomCode) {
this.roomControls.classList.add('hidden');
this.activeRoom.classList.remove('hidden');
this.roomCodeDisplay.textContent = roomCode;
}
showRoomControls() {
this.roomControls.classList.remove('hidden');
this.activeRoom.classList.add('hidden');
}
showStatus(message, type = 'info') {
this.status.textContent = message;
this.status.className = `status ${type}`;
this.status.classList.remove('hidden');
setTimeout(() => {
this.status.classList.add('hidden');
}, 3000);
}
generateRoomCode() {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let code = '';
for (let i = 0; i < 6; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
return code;
}
copyRoomCode() {
navigator.clipboard.writeText(this.currentRoom)
.then(() => this.showStatus('Room code copied!', 'success'))
.catch(() => this.showStatus('Failed to copy code', 'error'));
}
async checkExistingRoom() {
const { roomCode } = await chrome.storage.local.get('roomCode');
if (roomCode) {
this.joinRoomById(roomCode);
}
}
}
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new YoBuddyApp();
});