-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.html
271 lines (243 loc) · 5.86 KB
/
background.html
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<html>
<head>
<script>
//Questions contains the union of the questions, routs, and hrefs that the pages currently have, hashed by rout
var routs;
var hrefs;
var quest;
//Tabs are the quora tabs if true, others if false
var logging = true;
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if(request.method == "marquora_hidesPlease") {
sendResponse({method: "marquora_hidesWelcome", routs: findHides(request.routs)});
}
});
function sendQuoraMessage(tabId, tabUrl, callback) {
chrome.tabs.sendRequest(tabId, {method: "marquora_questionsPlease", url: tabUrl}, function(response) {
if(response.method == "questions") {
response.quest = reduceQuestions(response.quest)
if(callback) {
callback(response)
}
}
});
}
function retrieveData(tab, callback) {
if(tab) {
data = {}
data["bookmarks"] = getBookmarks()
data["hiddens"] = getHiddens()
if(checkQuora(tab.url)) {
sendQuoraMessage(tab.id, tab.url, function(response) {
data["questions"] = null
if(response) {
data["questions"] = response
setQuestions(data["questions"])
}
callback(data)
});
}
else {
data["questions"] = getQuestions()
callback(data)
}
}
}
function findHides(routs) {
hides = []
for(var i = 0; i < routs.length; i++) {
var value = getItem(routs[i]);
if(value && value.length > 0) {
if(value[0].substring(0,4) == "hide") {
hides.push(routs[i])
}
else if(value[1] && value[1].substring(0,4) == "hide") {
hides.push(routs[i])
}
}
}
return hides;
}
function include(array, obj) {
return (array.indexOf(obj) != -1)
}
function reduceQuestions(questions) {
for(var i = 0; i < questions.length; i++) {
questions[i] = reduceQuestion(questions[i])
}
return questions
}
function reduceQuestion(question) {
if(question.length > 90) { question = question.substring(0,87) + "..."}
return question
}
function updateRouts(request) {
for(var i = 0; i < request.routs.length; i++) {
routs.push(request.routs[i])
}
}
function getQuestions() {
if(quest && routs && hrefs) {
questions = {}
questions["quest"] = quest
questions["routs"] = routs
questions["hrefs"] = hrefs
return questions;
}
return null
}
function setQuestions(questions) {
quest = questions["quest"]
hrefs = questions["hrefs"]
routs = questions["routs"]
}
function getBookmarks() {
return getSelection("book")
}
function getHiddens() {
return getSelection("hide")
}
function getSelection(select) {
var selection = {}
selection["quest"] = []
selection["routs"] = []
selection["hrefs"] = []
var storage = getStorage()
if(storage) {
for(var i = 0; i < storage.length; i++) {
values = storage[i].split("|")
if(values[0] == select) {
selection["routs"].push(values[1])
selection["hrefs"].push(values[2])
selection["quest"].push(values[3])
}
}
}
return selection
}
//Mark is book or unbook
function book(href, rout, text, mark, callback) {
value = "book|" + rout + "|" + href + "|" + text
if(mark) {
setItem(value)
}
else {
removeItem(value);
}
callback()
}
//Mark is hide or unhide
function hide(tabId, href, rout, text, mark, callback) {
value = "hide|" + rout + "|" + href + "|" + text
if(mark) {
setItem(value)
tellTabToHideSingle(tabId, rout)
}
else {
removeItem(value);
tellTabToRevealSingle(tabId, rout)
}
callback()
}
function setItem(key) {
try {
marquora = window.localStorage.getItem("marquora")
if(!marquora) {
marquora = []
}
else {
marquora = marquora.split("|||")
}
marquora.push(value)
window.localStorage.setItem("marquora", marquora.join("|||"))
} catch(e) {
log("error inside setItem:" + e);
}
}
function getItem(key) {
var value = null;
try {
marquora = window.localStorage.getItem("marquora")
if(marquora) {
marquora = marquora.split("|||")
value = findKey(marquora, key)
}
} catch(e) {
log("Error inside getItem() for key: " + key);
log(e);
value = null;
}
return value;
}
function findKey(marquora, key) {
values = []
if(marquora) {
for(var i = 0; i < marquora.length; i++) {
if(marquora[i] && marquora[i].length > key.length && marquora[i].substring(5,key.length + 5) == key) {
values.push(marquora[i])
}
}
}
return values
}
function removeItem(key) {
key = key.split("|")[1]
try {
marquora = window.localStorage.getItem("marquora").split("|||")
if(marquora) {
for(var i = 0; i < marquora.length; i++) {
if(marquora[i] && marquora[i].substring(5,key.length + 5) == key) {
delete marquora[i]
}
}
}
window.localStorage.setItem("marquora", marquora.join("|||"))
} catch(e) {
log("Error removing item " + key);
log(e);
}
}
function getStorage() {
try {
marquora = window.localStorage.getItem("marquora")
if(marquora) {
return marquora.split("|||")
}
else {
return {}
}
} catch(e) {
log("Error returning storage")
log(e)
}
}
function clearStorage() {
try {
window.localStorage.setItem("marquora", [].join("|||"))
} catch(e) {
log("error in clearing storage " + e)
}
}
//checks if the url is valid for quora
function checkQuora(url) {
var regex = /.*quora\.com.*/;
if(regex.test(url)) {
return true;
}
return false;
}
//Removes unwanted questions from the tab
function tellTabToHideSingle(tabid, rout) {
chrome.tabs.sendRequest(tabid, {method: "hideQuestion", data: rout});
}
function tellTabToRevealSingle(tabid, rout) {
chrome.tabs.sendRequest(tabid, {method: "revealQuestion", data: rout});
}
function log(txt) {
if(logging) {
console.log(txt);
}
}
</script>
</head>
</html>