-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventPage.js
603 lines (566 loc) · 24.2 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
chrome.extension.onMessage.addListener(
function(request, sender) {
getTopLevelBookmarkNode("Other Bookmarks", function(otherNode){
if (other === null) {
console.error("'Other Bookmarks' not found.");
return;
}
var other = otherNode.children, otherID = otherNode.id;
var flattenedFolders = retrieveFlatFolderNames(otherNode);
var folders = onlyFolders(other);
var alreadyBookmarked = false;
var bookmark = null;
var bookmarkFolderID = null;
chrome.bookmarks.search(sender.tab.url, function(results){
results.forEach(function(currentBookmark){
if (currentBookmark.url == sender.tab.url){
alreadyBookmarked = true;
bookmarkFolderID = currentBookmark.parentId;
bookmark = currentBookmark;
return false;
}
});
if (alreadyBookmarked){
populateInterface(bookmarkFolderID, flattenedFolders, bookmark);
return;
}
determineBestFolder(sender.tab, request, folders, function(folderName){
if (folderName === "Uncategorized"){
// Find the uncategorized folder and create bookmark there
getFolder(folderName, other, otherID, true, function(uncategorized){
chrome.bookmarks.create({'parentId': uncategorized.id,
'title': sender.tab.title || "",
'url': sender.tab.url}, function(bookmark) {
populateInterface(uncategorized.id, flattenedFolders, bookmark);
});
});
}else{
getFolder(folderName, other, otherID, false, function(folder) {
chrome.bookmarks.create({'parentId': folder.id,
'title': sender.tab.title || "",
'url': sender.tab.url}, function(bookmark) {
populateInterface(folder.id, flattenedFolders, bookmark);
});
});
}
});
});
});
});
// Given a string and a length, return the string at that length or shorter broken at the last
// full word
function trimDocument(dokument, length){
var d, i;
if (!dokument)
return dokument;
if (dokument && dokument.length <= length)
return dokument;
d = dokument.substring(0, length);
if (/\W/.test(dokument[length]) || /\W/.test(dokument[length-1])){
return d;
}
d = d.split(/\W/);
if (d.length === 1){
return d[0];
}
i = d.slice(0, d.length-1).join(" ").length;
return dokument.substring(0, i);
}
// Convenience function to create string from a response object that
// contains a title, meta and description string
function createDocument(title, response){
if (response.description && response.description.length)
return trimDocument(title + " " + response.description + " " + response.keywords || "", 250);
return trimDocument(title + " " + response.keywords || "", 250);
}
// Repeat string function from http://stackoverflow.com/questions/202605/repeat-string-javascript
function repeat(pattern, count) {
if (count < 1) return '';
var result = '';
while (count > 0) {
if (count & 1) result += pattern;
count >>= 1, pattern += pattern;
}
return result;
}
// Given a node, retrieve all children folders in a flat list
// with optional leading spaces to express depth
function retrieveFlatFolderNames(folder, indent, depth){
if (typeof indent === "undefined")
indent = unescape(" ".replace(/ /g, "%A0"));
if (typeof depth === "undefined")
depth = 0;
var folders = [];
for (var childIndex in folder.children){
var child = folder.children[childIndex];
if (!child.hasOwnProperty("url")) {
child.indentedTitle = repeat(indent, depth) + child.title;
folders.push(child);
folders = folders.concat(retrieveFlatFolderNames(child, indent, depth+1));
}
}
return folders;
}
// find a toplevel bookmark with a given name
function getTopLevelBookmarkNode(name, callback){
chrome.bookmarks.getTree(function(results){
var topLevel = results[0].children, nodeKey;
for (nodeKey in topLevel){
var node = topLevel[nodeKey];
if (node.title == name)
return callback(node);
}
return callback(null);
});
}
// Given a list of bookmark nodes, return those that are folders (not actual bookmarks)
function onlyFolders(nodes) {
var folders = [];
for (var nodeKey in nodes){
var node = nodes[nodeKey];
if (!node.hasOwnProperty("url"))
folders.push(node);
}
return folders;
}
// Given a list of folder objects, returns one with a the given name
// If not found, and create is true, creates the folder under folder
// otherID
function getFolder(name, folders, otherID, create, callback){
for (var folderKey in folders){
var folder = folders[folderKey];
if (folder.title.trim() === name.trim())
return callback(folder);
}
if (create){
// Create uncategorized folder
chrome.bookmarks.create({'parentId': otherID, 'title': name},
function(folder) {
callback(folder);
}
);
}else{
// We couldn't find the folder, this should not happen, but
// if it does, get the uncategorized folder
getFolder("Uncategorized", folders, otherId, true, callback);
}
}
function populateInterface(folderID, otherFolders, bookmark){
var views = chrome.extension.getViews({type:"popup"});
var pop_doc = views[0].document;
var title_input = pop_doc.getElementById("bookmark_title");
var folder_input = pop_doc.getElementById("bookmark_folders");
var chevron = pop_doc.getElementById("expand");
var create_folder_btn = pop_doc.getElementById("create_folder");
var create_folder_txt = pop_doc.getElementById("folder_name");
chevron.addEventListener("click", function() {
if (chevron.className === "icon-chevron-right"){
chevron.className = "icon-chevron-down";
create_folder_btn.style.display = "inline";
create_folder_txt.style.display = "inline";
}else{
chevron.className = "icon-chevron-right";
create_folder_btn.style.display = "none";
create_folder_txt.style.display = "none";
}
});
create_folder_btn.addEventListener("click", function(){
var folder_name = create_folder_txt.value;
if (folder_name.trim().length)
{
chrome.bookmarks.create({title:folder_name.trim()}, function(node){
chrome.bookmarks.move(bookmark.id, {parentId:node.id});
window.close();
});
}
});
title_input.value = bookmark.title;
title_input.addEventListener("keyup", function() {
chrome.bookmarks.update(bookmark.id, {title: title_input.value}, function(){});
});
chrome.bookmarks.getRecent(2, function(bookmarks){
if (bookmarks.length > 0)
if ((bookmarks[0].url === bookmark.url) && (bookmarks.length > 1))
id = bookmarks[1].parentId;
else
id = bookmarks[0].parentId;
else
id = null;
var cache_option;
otherFolders.forEach(function(currentFolder){
var newOption = new Option(currentFolder.indentedTitle, currentFolder.id);
newOption.selected = (folderID == currentFolder.id);
if (currentFolder.id == id) {
newOption.className = "last";
cache_option = newOption;
}
folder_input.add(newOption, null);
});
pop_doc.getElementById("remove").addEventListener("click", function(){
chrome.bookmarks.remove(bookmark.id);
window.close();
}, false);
// Setup move to last used folder option
var last_folder = pop_doc.getElementById("last_folder");
last_folder.addEventListener("click", function(){
if (cache_option !== null){
chrome.bookmarks.move(bookmark.id, {parentId:id},function(){
cache_option.selected = true;
});
}
});
// Setup change handler
folder_input.addEventListener("change", function() {
chrome.bookmarks.move(bookmark.id, {parentId: folder_input.selectedOptions[0].value}, function(){});
});
});
}
function determineBestFolder(page, resp, folders, callback){
if (!page.title || page.title.trim().length === 0){
callback("Uncategorized");
return;
}
chrome.storage.local.get({
"feature_count":{},
"klass_count":{}
}, function(storage){
var c = new NaiveBayesClassifier(storage), folder = null, klass;
klass = c.classify(createDocument(page.title.toLowerCase(), resp));
if (klass !== "Uncategorized"){
callback(klass);
return;
}
// if we weren't able to categorize, try the folder names
// the name of the folder
// Odd results if folder name is all spaces
folders = folders.filter(function(folder){
if (folder.title.trim().length) return true;
return false;
});
folders.forEach(function(currentFolder){
var regex = new RegExp("\\b"+currentFolder.title+"\\b","i");
if (regex.test(page.title)){
folder = currentFolder;
return false;
}
});
if (folder)
callback(folder.title);
else
callback(klass);
});
}
// Given a top level folder, return all descendants
// children that are not folders
function getAllPagesInFolder(folder){
var pages = [];
for (var childIndex in folder.children){
var child = folder.children[childIndex];
if (child.hasOwnProperty("url") && child.hasOwnProperty('title') && child.title.trim().length)
pages.push(child);
else
pages = pages.concat(getAllPagesInFolder(child));
}
return pages;
}
// Given a return all descendant
// children that are folders
function getAllFoldersInFolder(folder){
var folders = [];
for (var childIndex in folder.children){
var child = folder.children[childIndex];
if (!child.hasOwnProperty("url"))
folders.push(child);
else
folders = folders.concat(getAllFoldersInFolder(child));
}
return folders;
}
// Given a bookmark, navigate up the tree to find its highest
// parent folder immediately below "Other Bookmarks"
function findKlass(node, callback, descendant) {
if (!node.hasOwnProperty("parentId")){
callback(false, undefined);
return;
}
if (!node.hasOwnProperty("url") && node.parentId == "0" && node.title == "Other Bookmarks"){
// it is possible this was called on the "Other Bookmarks" itself, in which case the answer
// is undefined
if (typeof descendant === "undefined"){
callback(false, undefined);
return;
}
callback(descendant.title !== "Uncategorized" && descendant.title.trim().length !==0,
descendant.title.trim(), descendant.id);
return;
}
chrome.bookmarks.get(node.parentId, function(results){
findKlass(results[0], callback, node);
});
}
function isKlassNode(node, callback){
chrome.bookmarks.get(node.parentId, function(results){
var parent = results[0];
if (!node.hasOwnProperty("url") && parent.parentId === "0" && parent.title === "Other Bookmarks")
callback(true);
else
callback(false);
});
}
function trainFolder(node, klass, classifier, cache, untrain) {
var verb = untrain ? 'untrain' : 'train';
cache = cache ? cache : {};
function train(c, cache){
var pages = getAllPagesInFolder(node), index;
for (index in pages){
page = pages[index];
c[verb](page.title, klass);
}
var folders = getAllFoldersInFolder(node);
folders.push(node);
for (index in folders){
folder = folders[index];
cache[folder.id] = folder.title.trim();
}
}
if (typeof classifier === "undefined" || typeof cache === "undefiend") {
chrome.storage.local.get({
"feature_count":{},
"klass_count":{},
"cache":{}
}, function(storage){
var c = new NaiveBayesClassifier(storage);
train(c, storage.cache);
var save = c.to_object();
save.cache = cache;
// Since we aren't monitoring for folder creation
// whenever we train on a klass we need to cache its name
save.cache[node.id] = klass;
chrome.storage.local.set(save, function(){});
});
}else{
train(classifier, cache);
cache[node.id] = klass;
}
}
// Events
//
// On install, scan all bookmarks and train the classifier
chrome.runtime.onInstalled.addListener(function(details) {
if (details.reason == "install"){
var c = new NaiveBayesClassifier(),
cache = {};
getTopLevelBookmarkNode("Other Bookmarks", function(node){
var nodes = node.children;
var topLevelFolders = onlyFolders(nodes);
topLevelFolders = topLevelFolders.filter(function(bookmark){
if (bookmark.title.trim()) return true;
return false;
});
for (var folderIndex in topLevelFolders){
var klass = topLevelFolders[folderIndex].title;
if (klass === "Uncategorized") continue;
var folder = topLevelFolders[folderIndex];
trainFolder(folder, klass, c, cache);
}
var save = c.to_object();
save.cache = cache;
chrome.storage.local.set(save, function(){
});
});
}
});
// Helper function taking a classifier, document, and class
// Trains on the document and saves the state to local disk
function trainHelper(dokument, klass, klass_id) {
chrome.storage.local.get({
"feature_count":{},
"klass_count":{},
"cache" : {}
}, function(storage){
var c = new NaiveBayesClassifier(storage);
// We don't look for folder creation so make sure this
// klass has been cached.
storage.cache[klass_id] = klass;
c.train(dokument, klass);
var save = c.to_object();
save.cache = storage.cache;
chrome.storage.local.set(save, function(){});
});
}
// On bookmark created, determine its root parent
// if it is "Other Bookmarks", train on the folder
// determine if a bookmark was created or just a folder
chrome.bookmarks.onCreated.addListener(function(id, bookmark) {
if (bookmark.hasOwnProperty("url") && bookmark.title && bookmark.title.trim()) {
findKlass(bookmark, function(train, klass, klass_id){
if (train){
// If possible (the user just hit the star button on a
// live page) we would like to train on the meta data
// associated with the page as well (not just the title)
chrome.tabs.query({url:bookmark.url}, function(tabs){
if (tabs.length){
var tab_id = tabs[0].id;
chrome.tabs.sendMessage(tab_id,{}, function(response){
// Now we have the additional info about the page
var dokument = createDocument(bookmark.title.toLowerCase(), response);
trainHelper(dokument, klass, klass_id);
});
}else{
trainHelper(bookmark.title.toLowerCase(), klass, klass_id);
}
});
}
});
}
});
// The api doesn't tell us the children of nodes that were deleted
// would have to track this ourself. This does not seem worth it.
// If the root folder is deleted, we will remove the class
chrome.bookmarks.onRemoved.addListener(function(id, removeInfo) {
chrome.storage.local.get({
"feature_count":{},
"klass_count":{},
"cache" : {}
}, function(storage){
chrome.bookmarks.get(removeInfo.parentId, function(parentNode){
var cache = storage.cache;
var c = new NaiveBayesClassifier(storage);
if (cache[id]){
var title = cache[id];
delete cache[id];
// If it's a class folder, remove the class from the classifier
if (parentNode.parentId === "0" && parentNode.title === "Other Bookmarks"){
c.delete_klass(title);
}
var save = c.to_object();
save.cache = cache;
chrome.storage.local.set(save, function(){});
}
});
});
});
// Bookmarks are created and moved often, need to make it possible to
// untrain and train on the new folder
chrome.bookmarks.onMoved.addListener(function(id, moveInfo) {
chrome.storage.local.get({
"feature_count": {},
"klass_count": {},
"cache": {}
}, function(storage){
var c = new NaiveBayesClassifier(storage);
chrome.bookmarks.get(id, function(bookmarks) {
var bookmark = bookmarks[0];
// It moved, but does it matter?
if (bookmark.parentId === moveInfo.oldParentId) return;
// Is this a bookmark or a folder that was moved
if (bookmark.hasOwnProperty("url")) {
// can't train a bookmark with no title
if (!bookmark.title || !bookmark.title.trim()) return;
chrome.bookmarks.get([moveInfo.parentId, moveInfo.oldParentId], function(results){
var parent = results[0], oldParent = results[1];
findKlass(oldParent, function(train, oldKlass){
if (train){
// If we had trained on meta too we aren't tracking it
// so we can't fully untrain on this, but we can do our best
// TODO this may be bad because we are reversing some of the features
// but fully decrementing the document count for this class
c.untrain(bookmark.title, oldKlass);
}
// train, this has to come last so we only need to save once
findKlass(parent, function(train, newKlass){
if (train){
// Don't do anything for empty folder names
c.train(bookmark.title, newKlass);
}
chrome.storage.local.set(c.to_object(), function(){});
});
});
});
} else {
chrome.bookmarks.getSubTree(id, function(results){
var node = results[0];
chrome.bookmarks.getSubTree(moveInfo.oldParentId, function(results){
var oldParent = results[0];
var wasAKlass = false;
if (oldParent.parentId === "0" && oldParent.title === "Other Bookmarks"){
wasAKlass = true;
}
findKlass(oldParent, function(untrain, oldKlass){
// if the node was a root node than we can't use its oldParent's klass
// as the old klass because it would have had no klass
if (wasAKlass)
oldKlass = node.title;
findKlass(node, function(train, newKlass){
// Note it really doesn't matter if we were a root node before or are one now
// As long as we get the old and new klass designations right, that will take
// care of itself
if (oldKlass === newKlass){
// Either folder was outside "Other Bookmarks" folder and still is
// or the klass did not change during the move
return;
}
// if this was a folder that was moved from outside "Other bookmarks"
// we need to cache it's title, because that may not have happened previously
if (typeof oldKlass === "undefined")
cache[id] = node.title;
if (typeof oldKlass !== "undefined" && oldKlass !== "Uncategorized" && oldKlass.trim().length){
c.delete_class(oldKlass);
// If we weren't a top level class before, then retrain the oldKlass sans the moved folder
if (!wasAKlass)
trainFolder(oldParent, oldKlass, c, storage.cache);
}
if (typeof newKlass !== "undefined" && newKlass !== "Uncategorized" && newKlass.trim().length){
trainFolder(node, newKlass, c, storage.cache);
}
var save = c.to_object();
save.cache = storage.cache;
chrome.storage.local.set(save, function(){});
});
});
});
});
}
});
});
});
// If a bookmark folder title was changed, update its cache
// If a top-level folder changes, we have to untrain change the name of its Klass
// inside the klassifier
chrome.bookmarks.onChanged.addListener(function(id, changeInfo) {
chrome.storage.local.get({
"feature_count":{},
"klass_count":{},
"cache":{}
}, function(storage){
if (!changeInfo.hasOwnProperty('url')) {
var c = new NaiveBayesClassifier(storage);
// We may not have the old title cached here if they created a root
// folder, never added any children, and renamed it
var old_title = storage.cache[id] || "";
if (old_title === changeInfo.title) return;
// This only matters if this folder is a klass folder
chrome.bookmarks.getSubTree(id, function(results){
var node = results[0];
isKlassNode(node, function(klassNode){
if (!klassNode) return;
if (old_title === "Uncategorized" || old_title.trim().length === 0) {
// No untraining, things in an uncategorized never got trained
if (changeInfo.title.trim().length)
trainFolder(node, changeInfo.title, c, storage.cache);
}else if (changeInfo.title === "Uncategorized" || changeInfo.title.trim().length === 0){
// Changed title to Uncategorized, just untrain on old folder
if (old_title.trim().length)
trainFolder(node, old_title, c, storage.cache, true);
}else{
c.rename_class(old_title, changeInfo.title);
}
storage.cache[id] = changeInfo.title;
var save = c.to_object();
save.cache = storage.cache;
chrome.storage.local.set(save, function(){});
});
});
}
});
});