/
temp_background.js
308 lines (269 loc) · 8.95 KB
/
temp_background.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
chrome.contextMenus.create({
"title": "No search",
"id": "log-selection",
"contexts": ["selection"]
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "log-selection") {
} else if (info.menuItemId == "what-command") {
}
});
let tabUrlDict = {};
function SearchDict() {
this.tab_ids = {};
this.search_terms = [];
this.addUrlToTab = function (searchTerm, tabId, tabUrl, previousTabId) {
if (!this.tab_ids[tabId]) {
this.tab_ids[tabId] = [];
}
if (!searchTerm || searchTerm === "") {
var countUrls = this.tab_ids[tabId].length;
if (countUrls == 0) { //we have just opened a new tab
var countPrevUrls = this.tab_ids[previousTabId].length;
searchTerm = this.tab_ids[previousTabId][countPrevUrls - 1].search_term;
} else {
searchTerm = this.tab_ids[tabId][countUrls - 1].search_term;
}
}
this.tab_ids[tabId].push({url: tabUrl, search_term: searchTerm});
//if we don't already have search term recorded, record it now
if (this.search_terms.indexOf(searchTerm) == -1 && searchTerm != "mystery search term") {
this.search_terms.push(searchTerm);
var newSearchTermData = {msg: "new_search_term", data: {search_term: searchTerm}};
sendToGoogleSheets(newSearchTermData);
}
}
this.getAllSearchTerms = function() {
return this.search_terms;
}
this.getCurrentSearchTerm = function(tabId, url) {
console.log(tabId + " is tabId " + url + " is url" );
var currentTabInfo = this.tab_ids[tabId];
if (currentTabInfo) {
for (var i = 0; i < currentTabInfo.length; i++) {
var dict = currentTabInfo[i];
if (dict.url === url) {
console.log(dict.search_term);
return dict.search_term;
}
}
}
return "mystery search term";
}
}
/**
* What should an entry look like
* search_term: "search term"
* page_result: "www.reddit.com"
* highlight: "this is a highlight"
*/
var Highlight = function(searchTerm, pageResult, highlight) {
this.search_term = searchTerm;
this.page_result = pageResult;
this.highlight = highlight;
}
/**
* Get the URL parameters
* source: https://css-tricks.com/snippets/javascript/get-url-variables/
* @param {String} url The URL
* @return {Object} The URL parameters
*/
var getParams = function (url) {
var params = {};
var parser = document.createElement('a');
parser.href = url;
var query = parser.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
params[pair[0]] = decodeURIComponent(pair[1].replace(/\+/g, " "));
}
return params;
};
var getSearchTerm = function (url) {
query = getParams(url).q;
return query ? query : "";
}
chrome.runtime.onInstalled.addListener(function() {
let tokenAcquired = false;
let lastOpenTabId = null;
getAuthTokenSilent();
//chrome.browserAction.disable();
//ask if we already have authentication. if we don't, ask for it
let searchDict = new SearchDict();
let pluginEnabled = true;
let newUrl = null;
let isNewSearch = false;
let currentSearchTerm = "test";
let possibleSchemes = ["https:", "http:", "www:"];
let lastUrlHref = "";
let currentUrl = null;
chrome.browserAction.disable();
if (pluginEnabled) {
chrome.tabs.onHighlighted.addListener(function(highlightInfo) {
chrome.tabs.query({"active": true, "lastFocusedWindow": true}, function(tabs) {
currentUrl = new URL(tabs[0].url);
if(possibleSchemes.indexOf(currentUrl.protocol) != -1) {
chrome.browserAction.enable(tabs[0].id);
var currentSearchTerm = searchDict.getCurrentSearchTerm(tabs[0].id, currentUrl.href);
updateContextMenu(currentSearchTerm);
}
})
})
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tab.url) {
newUrl = new URL(tab.url);
if(possibleSchemes.indexOf(newUrl.protocol) != -1) {
chrome.browserAction.enable(tabId);
if(lastUrlHref != newUrl.href) {
newSearchTerm = "";
if (newUrl.hostname == "www.google.at") {
newSearchTerm = getSearchTerm(newUrl);
}
searchDict.addUrlToTab(newSearchTerm, tabId, newUrl.href, lastOpenTabId);
lastUrlHref = newUrl.href;
lastOpenTabId = tabId;
updateContextMenu(newSearchTerm);
}
}
}
}
)
//add listener for when popup requests our collected info
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.msg === "get_data") {
//get info about how we got to this tab id
var currentSearchTerm = searchDict.getCurrentSearchTerm(request.tab_id, request.url);
//make a new highlight object
var highlightObj = new Highlight(currentSearchTerm, request.url, request.highlighted_text);//currentSearchTerm, request.url, request.highlighted_text);
var allSearchTerms = searchDict.getAllSearchTerms();
chrome.runtime.sendMessage({
msg: "send_data",
status: "ok",
highlight: highlightObj,
search_terms: allSearchTerms
});
} else if (request.msg == "get_search_terms") {
var allSearchTerms = searchDict.getAllSearchTerms();
chrome.runtime.sendMessage({ msg: "send_search_terms",
status: "ok",
content: allSearchTerms
});
} else if (request.msg == "send_new_search_term") {
searchDict.addUrlToTab(request.search_term, request.tab_id, request.tab_url, lastOpenTabId);
var updatedSearchTerms = searchDict.getAllSearchTerms();
chrome.runtime.sendMessage({
msg: "added_search_term",
status: "ok",
search_terms: updatedSearchTerms,
new_search_term: request.search_term
});
} else {
console.log("got a msg but dunno what");
}
}
);
};
});
/* check Authentication functions - start */
var showAuthenticationNeeded = function() {
chrome.browserAction.setBadgeText({text: "!"});
}
var removeAuthenticationNeeded = function () {
chrome.browserAction.setBadgeText({text: ""});
}
var getAuthToken = function(options) {
chrome.identity.getAuthToken(
{ 'interactive': options.interactive },
options.callback);
}
var getAuthTokenSilent = function() {
getAuthToken({
'interactive': false,
'callback': getAuthTokenSilentCallback,
});
}
var getAuthTokenSilentCallback = function(token) {
if (chrome.runtime.lastError) {
//TODO show error message next to extension icon
//getAuthTokenInteractive();
showAuthenticationNeeded();
} else {
removeAuthenticationNeeded();
}
}
var getAuthTokenInteractiveCallback = function(token) {
if (chrome.runtime.lastError) {
//TODO tell the user this hasn't worked and give a link for some details
return false;
} else {
removeAuthenticationNeeded();
return true;
}
}
var getAuthTokenInteractive = function() {
getAuthToken({
'interactive': true,
'callback': getAuthTokenInteractiveCallback,
});
}
/* Authenticaion checking - finished */
var sendToGoogleSheets = function (data) {
var SCRIPT_ID='15uJJKus-863eXQy40-Cd6XBntQU80BAYAk_mCwfJeYjRR__BwMOu2Mob'; // Apps Script script id
var STATE_START=1;
var STATE_ACQUIRING_AUTHTOKEN=2;
var STATE_AUTHTOKEN_ACQUIRED=3;
var state = STATE_START;
var jsonToSend = data;
var updateGoogleSheet = function() {
//if not currently signed in, get auth
getAuthTokenSilent();
//then send data to API
sendDataToGoogleSheets();
}
var sendDataToGoogleSheets = function() {
getAuthToken({
'interactive': false,
'callback': sendDataToGoogleSheetsCallback,
});
}
var sendDataToGoogleSheetsCallback = function(token) {
post({ 'url': 'https://script.googleapis.com/v1/scripts/' + SCRIPT_ID +
':run',
'callback': googleAPIResponse,
'token': token,
'request': {'function': 'setData',
'parameters': JSON.stringify(jsonToSend)}
});
}
var googleAPIResponse = function(response) {
var info;
if (response.response.result.status == 'ok') {
info = "Data has been enetered into " + response.response.result.doc;
} else {
info = "Error..." + response.response.result.status;
}
console.log(info);
}
var post = function(options) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
options.callback(JSON.parse(xhr.responseText));
} else if (xhr.readyState === 4 && xhr.status !== 200) {
console.log("error in post of background");
}
};
xhr.open('POST', options.url, true);
xhr.setRequestHeader('Authorization', 'Bearer ' + options.token);
xhr.send(JSON.stringify(options.request));
}
updateGoogleSheet(jsonToSend);
}
var updateContextMenu = function(searchTerm) {
//update the rightclick option for someone to search here
chrome.contextMenus.update("log-selection", {
"title": "Log this selection to '" + searchTerm + "'",
});
}