-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventPage.js
166 lines (158 loc) · 6.35 KB
/
eventPage.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
// jshint jquery: true, undef: true,curly: true, bitwise: true, eqeqeq: true, immed: true, strict: false, unused: vars, devel: true, browser: true, newcap: false, multistr: true
/* global chrome, firebase */
'use strict';
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request);
if (request.updated) {
let user = firebase.auth().currentUser;
if (user) {
firebase.database().ref('users/' + user.uid + '/' + request.updated).set(request.val);
}
} else if (request.authCheck) {
if (firebase.auth().currentUser) {
sendResponse({ authStatus: true });
} else {
sendResponse({ authStatus: false });
startAuth(true);
}
} else if (request.relayGET) {
$.ajax({
url: request.url,
success: (data) => {
sendResponse({success: true, data});
},
error: () => {
sendResponse({error: true});
},
});
//indicates that sendResponse will be called asynchronously.
return true;
}
});
let config = {
apiKey: 'AIzaSyCisxFC0G30N0Yh8meI3eSCl6BZOD5CF-I',
authDomain: 'better-fanfiction.firebaseapp.com',
databaseURL: 'https://better-fanfiction.firebaseio.com',
projectId: 'better-fanfiction',
storageBucket: 'better-fanfiction.appspot.com',
messagingSenderId: '658655686523',
};
firebase.initializeApp(config);
startAuth(false);
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
firebase.database.enableLogging(function(message) {
console.log("[FIREBASE]", message);
});
setUpDBListeners(user.uid);
} else {
}
});
function setUpDBListeners(uid) {
let dbRef = firebase.database().ref('users/' + uid);
dbRef.on('child_added', onDBUpdate);
dbRef.on('child_changed', onDBUpdate);
dbRef.on('child_removed', snap => {
let key = snap.key;
let msgObj = {};
if (key.startsWith('Read:')) {
msgObj.updated = 'Read';
msgObj.id = key.substr(5);
} else if (key.startsWith('shelf:')) {
msgObj.updated = 'Shelf';
msgObj.id = parseInt(key.substr(6), 10);
} else {
msgObj = undefined;
}
chrome.storage.local.remove(key, () => tabUpdate(msgObj));
});
}
function onDBUpdate(snap) {
let key = snap.key;
chrome.storage.local.get(key, x => {
if (!jsonEqual(x[key], snap.val())) {
let storeObj = { [key]: snap.val() };
let msgObj = {};
if (key.startsWith('Read:')) {
msgObj.updated = 'Read';
msgObj.id = key.substr(5);
} else if (key.startsWith('shelf:')) {
msgObj.updated = 'Shelf';
msgObj.id = parseInt(key.substr(6), 10);
} else if (key === 'ReadLater') {
msgObj.updated = 'ReadLater';
} else if (key === 'Liked') {
msgObj.updated = 'Liked';
} else if (key === 'FavoritesLastModified') {
msgObj.updated = 'Favorites';
} else if (key === 'AlertsLastModified') {
msgObj.updated = 'Alerts';
} else if (key === 'SubscriptionsLastModified') {
msgObj.updated = 'Subscriptions'
} else if (key === 'Bookshelves') {
let oldData = x[key] || {};
storeObj[key] = Array.from(snap.val()).filter(el => el != null);
storeObj[key].forEach((shelf, i) => {
if (!jsonEqual(shelf, oldData[i])) {
if (shelf === undefined || shelf === null) {
shelf = oldData[i];
} else if (oldData[i] === undefined || oldData[i] === null) {
msgObj.added = true;
}
msgObj.updated = 'Bookshelves';
msgObj.fandom = shelf.fandom;
msgObj.id = shelf.id;
msgObj.name = shelf.name;
//break
return false;
}
});
} else {
msgObj = undefined;
}
chrome.storage.local.set(storeObj, () => tabUpdate(msgObj));
}
});
}
function tabUpdate(data) {
if (data !== undefined) {
chrome.tabs.query({ url: ['https://www.fanfiction.net/*', 'https://archiveofourown.org/*'] }, function (tabs) {
tabs.forEach(t => {
console.log("Sending message to tab: ", t.title, data);
chrome.tabs.sendMessage(t.id, data)
});
});
}
}
//from Google Firebase Auth Example: https://github.com/firebase/quickstart-js/blob/master/auth/chromextension/credentials.js
/**
* Start the auth flow and authorizes to Firebase.
* @param{boolean} interactive True if the OAuth flow should request with an interactive mode.
*/
function startAuth(interactive) {
// Request an OAuth token from the Chrome Identity API.
chrome.identity.getAuthToken({ interactive: !!interactive }, function (token) {
if (chrome.runtime.lastError && !interactive) {
console.log('It was not possible to get a token programmatically.');
} else if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else if (token) {
// Authorize Firebase with the OAuth Access Token.
var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
firebase.auth().signInWithCredential(credential).catch(function (error) {
// The OAuth token might have been invalidated. Lets' remove it from cache.
if (error.code === 'auth/invalid-credential') {
chrome.identity.removeCachedAuthToken({ token: token }, function () {
startAuth(interactive);
});
}
});
} else {
console.error('The OAuth Token was null');
}
});
}
function jsonEqual(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}