238 lines
7.2 KiB
JavaScript
238 lines
7.2 KiB
JavaScript
/************************************************************************
|
|
Copyright (C) Ayushya Amitabh - All Rights Reserved
|
|
Unauthorized copying of this file, via any medium is strictly prohibited
|
|
Proprietary and confidential
|
|
Written by Ayushya Amitabh <admin@chromewatchparty.com>, 2020
|
|
https://www.chromewatchparty.com/copyright
|
|
=========================================================================
|
|
ALL CONTENT ON WATCH PARTY'S WEBSITE OR IN WATCH PARTY'S CHROME EXTENSION APP, UNLESS OTHERWISE
|
|
CITED ARE THE SOLE INTELECTUAL PROPERTY OF AYUSHYA AMITABH, AND MAY NOT BE
|
|
COPIED, EDITED, OR REPRODUCED WITHOUT PERMISSION.
|
|
************************************************************************/
|
|
/* global chrome */
|
|
/* global firebase */
|
|
|
|
const APP_VERSION = '1.6.4';
|
|
const APP_VERSION_STRING = APP_VERSION.replace(/\./g, '');
|
|
|
|
const firebaseConfig = {
|
|
apiKey: 'AIzaSyCJLuYpzXLDRmu2ttzFuSGmYcpFxqToWFU',
|
|
authDomain: 'watch-party-crx.firebaseapp.com',
|
|
databaseURL: 'https://watch-party-crx.firebaseio.com',
|
|
projectId: 'watch-party-crx',
|
|
storageBucket: 'watch-party-crx.appspot.com',
|
|
messagingSenderId: '712242707190',
|
|
appId: '1:712242707190:web:65fa7f772e30da364b0ad7',
|
|
measurementId: 'G-EZS4PEVG4T',
|
|
};
|
|
|
|
firebase.initializeApp(firebaseConfig);
|
|
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
|
|
|
|
const isAuthed = () => true && firebase.auth().currentUser;
|
|
const doAuth = () => firebase.auth().signInAnonymously();
|
|
|
|
const fetcher = async (request, sendResponse) => {
|
|
if (!isAuthed()) await doAuth();
|
|
const { url, method } = request;
|
|
delete request.type;
|
|
delete request.url;
|
|
delete request.method;
|
|
const idToken = await firebase.auth().currentUser.getIdToken();
|
|
let resStatus = {};
|
|
fetch(url, {
|
|
method,
|
|
headers: {
|
|
Authorization: `Token ${idToken}:${firebase.auth().currentUser.uid}`,
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
...request,
|
|
})
|
|
.then((res) => {
|
|
resStatus = {
|
|
status: res.status,
|
|
statusText: res.statusText,
|
|
};
|
|
return res.json();
|
|
})
|
|
.then((res) => sendResponse({ ...res, ...resStatus }))
|
|
.catch((err) =>
|
|
sendResponse({
|
|
...err,
|
|
status: 503,
|
|
statusText: "Couldn't talk to server",
|
|
})
|
|
);
|
|
};
|
|
|
|
const sendAuth = async (sendResponse) => {
|
|
if (!isAuthed()) await doAuth();
|
|
sendResponse(firebase.auth().currentUser);
|
|
};
|
|
|
|
chrome.runtime.onInstalled.addListener((details) => {
|
|
if (details.reason) {
|
|
if (details.reason === 'install') {
|
|
if (window.localStorage.getItem('watchPartyVersion')) {
|
|
if (window.localStorage.getItem('watchPartyVersion') !== APP_VERSION) {
|
|
chrome.tabs.create({
|
|
url: `http://www.chromewatchparty.com/changes/v${APP_VERSION}`,
|
|
active: true,
|
|
});
|
|
window.localStorage.setItem('watchPartyVersion', APP_VERSION);
|
|
}
|
|
} else {
|
|
chrome.tabs.create({
|
|
url: 'https://www.chromewatchparty.com/welcome',
|
|
active: true,
|
|
});
|
|
window.localStorage.setItem('watchPartyVersion', APP_VERSION);
|
|
}
|
|
} else if (details.reason === 'update') {
|
|
if (window.localStorage.getItem('watchPartyVersion')) {
|
|
if (window.localStorage.getItem('watchPartyVersion') !== APP_VERSION) {
|
|
chrome.tabs.create({
|
|
url: `http://www.chromewatchparty.com/changes/v${APP_VERSION}`,
|
|
active: true,
|
|
});
|
|
window.localStorage.setItem('watchPartyVersion', APP_VERSION);
|
|
}
|
|
} else {
|
|
chrome.tabs.create({
|
|
url: `http://www.chromewatchparty.com/changes/v${APP_VERSION}`,
|
|
active: true,
|
|
});
|
|
window.localStorage.setItem('watchPartyVersion', APP_VERSION);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|
const { type } = request;
|
|
switch (type) {
|
|
case 'API_CALL':
|
|
fetcher(request, sendResponse);
|
|
break;
|
|
case 'GET_AUTH':
|
|
sendAuth(sendResponse);
|
|
break;
|
|
case 'GET_PARTIES':
|
|
sendResponse(
|
|
window.localStorage.getItem('parties')
|
|
? JSON.parse(window.localStorage.getItem('parties'))
|
|
: []
|
|
);
|
|
break;
|
|
case 'EMPTY_PARTIES':
|
|
window.localStorage.removeItem('parties');
|
|
break;
|
|
case 'IS_HIDDEN':
|
|
sendResponse(
|
|
window.localStorage.getItem('showWPButton')
|
|
? JSON.parse(window.localStorage.getItem('showWPButton'))
|
|
: true
|
|
);
|
|
break;
|
|
case 'GET_TUTORIAL':
|
|
sendResponse(
|
|
window.localStorage.getItem('tutorialComplete')
|
|
? JSON.parse(window.localStorage.getItem('tutorialComplete'))
|
|
: false
|
|
);
|
|
break;
|
|
case 'SET_TUTORIAL':
|
|
window.localStorage.setItem('tutorialComplete', request.status);
|
|
break;
|
|
default:
|
|
console.warn('Unknown message type: <', type, '>');
|
|
break;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
chrome.runtime.onConnect.addListener((port) => {
|
|
port.onDisconnect.addListener(() => {
|
|
port.roomListener && port.roomListener();
|
|
port.userListener && port.userListener();
|
|
port.messageListener && port.messageListener();
|
|
});
|
|
port.onMessage.addListener((request) => {
|
|
const { type, roomId, roomPin } = request;
|
|
if (type === 'INIT') {
|
|
port.roomListener = firebase
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.onSnapshot(
|
|
(snap) => {
|
|
if (snap) {
|
|
port.postMessage(snap.data());
|
|
}
|
|
},
|
|
(err) => {
|
|
port.postMessage({ isError: true, ...err });
|
|
}
|
|
);
|
|
port.userListener = firebase
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.collection('users')
|
|
.onSnapshot(
|
|
(snap) => {
|
|
if (snap) {
|
|
const isAdmin = snap.docs
|
|
.filter((user) => user.id === firebase.auth().currentUser.uid)[0]
|
|
.data().admin;
|
|
const users = snap.docs.map((userSnap) => userSnap.data().name);
|
|
port.postMessage({ admin: isAdmin, users });
|
|
}
|
|
},
|
|
(err) => {
|
|
port.postMessage({ isError: true, ...err });
|
|
}
|
|
);
|
|
port.messageListener = firebase
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.collection('messages')
|
|
.orderBy('time')
|
|
.onSnapshot(
|
|
(snap) => {
|
|
if (snap) {
|
|
let messages = snap.docs.map((v) => ({ ...v.data(), id: v.id }));
|
|
port.postMessage({
|
|
messages,
|
|
});
|
|
}
|
|
},
|
|
(err) => {
|
|
port.postMessage({ isError: true, ...err });
|
|
}
|
|
);
|
|
if (window.localStorage.getItem('parties')) {
|
|
var existing = JSON.parse(window.localStorage.getItem('parties'));
|
|
if (!(existing.filter((i) => i.name === roomId).length > 0)) {
|
|
existing.push({
|
|
name: roomId,
|
|
pin: roomPin,
|
|
});
|
|
window.localStorage.setItem('parties', JSON.stringify(existing));
|
|
}
|
|
} else {
|
|
window.localStorage.setItem(
|
|
'parties',
|
|
JSON.stringify([
|
|
{
|
|
name: roomId,
|
|
pin: roomPin,
|
|
},
|
|
])
|
|
);
|
|
}
|
|
}
|
|
});
|
|
});
|