-
Notifications
You must be signed in to change notification settings - Fork 6
/
background.js
executable file
·63 lines (48 loc) · 2.17 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
var booklight = function booklight() {
var booklight = this;
this.foldersList = [];
this.urls = [];
this.getBookmarks = function() {
chrome.bookmarks.getTree(function(bookmarksTree) {
booklight.foldersList = filterRecursively(bookmarksTree, "children", function(node) {
if (node.url) booklight.urls.push(node);
return !node.url && node.id > 0;
}).sort(function(a, b) {
// The sort functions make sure that we will have the last used folders on top
return b.dateGroupModified - a.dateGroupModified;
});
chrome.storage.local.set({"booklightFolders": booklight.foldersList }, function(bookmarks) { console.log("Setting the folders list into the local storage !!") });
chrome.storage.local.set({"booklightUrls": booklight.urls }, function(bookmarks) { console.log("Setting the urls list into the local storage !!") });
});
// Recursively filter the passed TreeNodes
function filterRecursively(nodeArray, childrenProperty, filterFn, results) {
results = results || [];
nodeArray.forEach( function( node ) {
if (filterFn(node)) results.push({title: node.title, id: node.id, dateGroupModified: node.dateGroupModified, folder: isLeaf(node), parentId: node.parentId});
if (node.children) filterRecursively(node.children, childrenProperty, filterFn, results);
});
return results;
};
// Check if the current bookmark is a leaf (does not contain more folders)
function isLeaf(node) {
var leafyNodes = [];
node.children.forEach(function(child){
if (!child.hasOwnProperty('children')) leafyNodes.push(1);
});
var isLeaf = leafyNodes.length == node.children.length ? true : false;
return isLeaf;
}
}
this.attachListeners = function() {
chrome.runtime.onMessage.addListener(function(request, sender, sendrequest) {
if (request.message == "booklight") {
console.log("adding: " + request.url + " title: " + request.title + " to folder id: " + request.folder);
chrome.bookmarks.create({ 'parentId': request.folder, 'title': request.title, 'url': request.url });
sendrequest({message: "success"});
}
});
}
}
var booklight = new booklight();
booklight.attachListeners();
booklight.getBookmarks();