-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
executable file
·316 lines (285 loc) · 11.3 KB
/
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
309
310
311
312
313
314
315
"use strict";
let switchingTask = false;
let CTASKID;
var currentTabInfo = null;
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.create({'url':'html/about.html'});
})
//todo consolidate all the message listeners into one listner
chrome.runtime.onMessage.addListener(function (request, sender) {
if (request.type === "create-task") {
createTask(request.taskName, request.tabs, false, {});
if (request.activated) {
switchingTask = true;
saveTaskInWindow(CTASKID);
deactivateTaskInWindow(CTASKID);
activateTaskInWindow(TASKS["lastAssignedId"]);
}
chrome.tabs.query({active: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {message: 'update-dock'}, function(response) {
});
});
} else if (request.type === "add-to-task") {
const senderTab = sender.tab;
const senderWindowId = senderTab.windowId;
chrome.windows.get(senderWindowId, {populate: true}, function (window) {
const tabs = [];
for (let i = 0; i < window.tabs.length; i++) {
if (window.tabs[i].highlighted) {
tabs.push(window.tabs[i]);
}
}
//tabs array is now ready to use
//remove tabs that were highlighted
const tabIdsToClose = [];
for (let j = 0; j < tabs.length; j++) {
tabIdsToClose.push(tabs[j].id)
}
chrome.tabs.remove(tabIdsToClose);
addTabsToTask(request.taskId, tabs);
});
} else if (request.type === "switch-task" && request.nextTaskId !== "") {
if (CTASKID != request.nextTaskId) {
switchingTask = true;
saveTaskInWindow(CTASKID, function () {
deactivateTaskInWindow(CTASKID, function () {
activateTaskInWindow(request.nextTaskId);
});
});
}
} else if (request.type === "close-task") {
closeTask(request.taskId);
} else if (request.type === "rename-task") {
renameTask(request.taskId, request.newTaskName);
} else if (request.type === "delete-task") {
deleteTask(request.taskToRemove);
} else if (request.type === "download-tasks") {
updateClickReport('Export JSON');
downloadTasks();
} else if (request.type === "download-collections") {
downloadCollections();
} else if (request.type === "add-url-to-task") {
addURLToTask(request.url, request.taskId);
} else if (request.type === "archive-task") {
archiveTask(request.taskId);
} else if (request.type === "pause-tasks") {
CTASKID = 0;
updateStorage("CTASKID", 0);
} else if (request.type === "open-liked-pages") {
openLikedPages(request.taskId);
} else if (request.type === "search-archive") {
// if (request.query != null) {
// chrome.tabs.create({"url": "html/searchArchive.html?q=" + request.query});
// }
searchArchive(request.query, sender.tab.id);
} else if (request.type === "onmouseover") {
const fromWindowID = sender.tab.windowId;
const targetURL = request["target-url"];
const highlightTabIndexes = [sender.tab.index];
chrome.windows.get(fromWindowID, {"populate": true}, function (window) {
window.tabs.forEach(function (tab) {
if (targetURL === tab.url) {
highlightTabIndexes.push(tab.index)
}
});
chrome.tabs.highlight({"windowId": fromWindowID, "tabs": highlightTabIndexes});
});
} else if (request.type === "onmouseout") {
chrome.tabs.highlight({"windowId": sender.tab.windowId, "tabs": sender.tab.index});
} else if (request.type === "clicklog") {
// chrome.storage.local.get("Click Log", function (clickLog) {
// clickLog = clickLog["Click Log"];
// if (clickLog.hasOwnProperty(request.text)) {
// clickLog[request.text]++;
// } else {
// clickLog[request.text] = 1;
// }
// chrome.storage.local.set({"Click Log": clickLog});
// });
} else if (request.type === "likePages") {
likePages(request.urls, request.taskId);
} else if (request.type === "deletePages") {
deleteFromHistory(request.urls, request.taskId);
} else if (request.type === "restore-tasks") {
TASKS = request.taskObject;
updateStorage("TASKS", TASKS);
} else if (request.type === "restore-collections") {
let temp = request.collectionsObject["Collections"];
updateStorage("Collections", temp);
} else if (request.type === "give unarchived tasks dict") {
let tasksDict = filterTasks({"archived": false});
chrome.runtime.sendMessage({
"type": "unarchived tasks dict",
"tasksDict": tasksDict
});
} else if (request.type === "detect-task") {
detectTask(request.topics, request.url, request.title);
} else if (request.type === "interests found") {
fireInterestNotification(request.interests);
} else if (request.type === 'save-page-content') {
parseDocumentAndSavePageContent(request.article, request.url);
} else if (request.type === 'delete-page-content') {
deletePageContent(request.url);
}
});
// Whenever TASKS is updated in the storage, send a message to the currently active tab to update the dock so that the changes are reflected
chrome.storage.onChanged.addListener(function (changes, namespace) {
if (changes.hasOwnProperty('TASKS')) {
chrome.tabs.query({active: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {message: 'update-dock'}, function(response) {
});
});
}
});
function downloadCollections() {
let dateObj = new Date();
let date = dateObj.toDateString();
chrome.storage.local.get("Collections", function (collections) {
downloadObjectAsJson(collections, "Sailboat Collections from " + date);
});
}
function searchArchive(query, tabId) {
query = query.replace(/\+/g, ' ');
var results = searchIndex.search(query);
chrome.tabs.sendMessage(tabId, {"type": "show-archived-results-on-google-page", "results": results});
}
// add to history
function addToHistory(url, title, taskId, startTime, endTime) {
if (url.indexOf('://newtab/') < 0 && url !== "about:blank" && url) {
let today = new Date();
let dd = today.getDate();
let mm = today.getMonth() + 1; //January is 0!
let yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = dd + '-' + mm + '-' + yyyy;
let historyToday = 'HISTORY-' + today;
// add/update the entry in the history object
chrome.storage.local.get([historyToday], function (results) {
let history;
//initialise the history object for today if not present
if (!results[historyToday]) {
history = {};
} else {
history = results[historyToday];
}
// get the task name
let taskObject = TASKS[taskId];
let taskName = taskObject.name;
if (!(taskId in history)) {
history[taskId] = {
"urls": {},
"totalTime": 0,
"taskName": taskName
}
}
let taskHistory = history[taskId];
let urls = taskHistory["urls"];
let totalTaskTime = taskHistory["totalTime"];
let timeDiff = (endTime - startTime) / 1000;
totalTaskTime += timeDiff;
if (!(url in urls)) {
urls[url] = {
"timeIntervals": [],
"timeSpent": 0,
"title": title,
"lastVisited": startTime.getTime()
}
}
let urlHistory = urls[url];
let timeIntervals = urlHistory["timeIntervals"];
let timeSpent = urlHistory["timeSpent"];
timeIntervals.push([startTime.toLocaleTimeString(), endTime.toLocaleTimeString()]);
timeSpent += timeDiff;
urls[url] = {
"timeIntervals": timeIntervals,
"timeSpent": timeSpent,
"title": title,
"lastVisited": startTime.getTime()
};
history[taskId] = {
"urls": urls,
"totalTime": totalTaskTime,
"taskName": taskName
};
let o = {};
o[historyToday] = history;
chrome.storage.local.set(o);
});
}
}
function handleOnUpdated(tabId, changeInfo, tab) {
if(currentTabInfo != null) {
if(changeInfo.url && changeInfo.url !== currentTabInfo.url) {
addToHistory(currentTabInfo.url, currentTabInfo.title, CTASKID, currentTabInfo.startTime, new Date());
currentTabInfo = null;
}
}
currentTabInfo = {
id: tab.id,
url: tab.url,
title: tab.title,
startTime: new Date()
};
}
function handleOnActivated(activeInfo) {
if(currentTabInfo != null) {
addToHistory(currentTabInfo.url, currentTabInfo.title, CTASKID, currentTabInfo.startTime, new Date());
currentTabInfo = null;
}
chrome.tabs.get(activeInfo.tabId, function (tab) {
currentTabInfo = {
id: tab.id,
url: tab.url,
title: tab.title,
startTime: new Date()
};
});
}
function handleOnRemoved(tabId, removeInfo) {
if(currentTabInfo != null) {
if(tabId === currentTabInfo.id) {
addToHistory(currentTabInfo.url, currentTabInfo.title, CTASKID, currentTabInfo.startTime, new Date());
}
}
}
function handleWindowOnFocusChanged(windowId) {
if(currentTabInfo != null) {
addToHistory(currentTabInfo.url, currentTabInfo.title, CTASKID, currentTabInfo.startTime, new Date());
currentTabInfo = null;
}
}
chrome.tabs.onUpdated.addListener(handleOnUpdated);
chrome.tabs.onActivated.addListener(handleOnActivated);
chrome.tabs.onRemoved.addListener(handleOnRemoved);
chrome.windows.onFocusChanged.addListener(handleWindowOnFocusChanged);
function parseDocumentAndSavePageContent(article, url) {
chrome.storage.local.get('page-content', function(response) {
let pageContent = response['page-content'];
delete article['content']; // delete the actual content as it increases the size unnecessarily
pageContent[url] = article;
searchIndex.addDoc({
'id': url,
'title': article.title,
'text': article.textContent
});
chrome.storage.local.set({'page-content': pageContent});
});
}
function deletePageContent(url) {
chrome.storage.local.get("page-content", function (pageContentObj) {
pageContentObj = pageContentObj["page-content"];
searchIndex.removeDoc({
'id': url,
'title': pageContentObj[url].title,
'text': pageContentObj[url].textContent
});
delete pageContentObj[url];
chrome.storage.local.set({"page-content": pageContentObj});
console.log("Content deleted");
});
}