-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChromeBookmarkStore.js
130 lines (115 loc) · 3.31 KB
/
ChromeBookmarkStore.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
define(function () {
function ChromeBookmarkStore()
{
}
ChromeBookmarkStore.prototype = {
CreateBookmarkFolder: function (name, id, callback)
{
chrome.bookmarks.create({
title: name,
parentId: id
}, function (tagTree) {
callback(tagTree);
});
},
MoveBookmark: function (bookmarkId, targetLocationId)
{
chrome.bookmarks.move(bookmarkId, {
parentId: targetLocationId
});
},
WriteBookmarkList: function (bookmarkList) {
chrome.bookmarks.getTree(function (bookmarkTree) {
bookmarkList.createHierarchy(bookmarkTree[0].children[0]);
});
},
GetBookmarkTree: function (callback) {
chrome.bookmarks.getTree(function (bookmarkTree) {
callback(bookmarkTree[0].children);
});
},
RemoveBookmark: function (bookmark, callback) {
chrome.bookmarks.remove(bookmark.id, function () {
callback(bookmark);
});
},
UpdateBookmarkTitle: function (bookmark, newTitle, callback) {
chrome.bookmarks.update(bookmark.id, {
title: newTitle
}, function (updatedBookmark) {
callback(bookmark, updatedBookmark.title);
});
},
RemoveEmptyFolders: function () {
function removeAll(bookmarkTree) {
if ('url' in bookmarkTree) {
return;
}
if (bookmarkTree.children.length > 0) {
bookmarkTree.children.forEach(removeAll);
return;
}
chrome.bookmarks.remove(bookmarkTree.id);
}
chrome.bookmarks.getTree(function (bookmarkTree) {
removeAll(bookmarkTree[0]);
});
},
RemoveDuplicates: function () {
var urls = {};
function removeAll(bookmarkTree, hierarchy) {
if ('url' in bookmarkTree) {
if (bookmarkTree.url in urls) {
chrome.bookmarks.remove(bookmarkTree.id);
} else {
urls[bookmarkTree.url] = hierarchy;
}
return;
}
if (bookmarkTree.children && bookmarkTree.children.length > 0) {
bookmarkTree.children.forEach(function (bookmarkTree) {
removeAll(bookmarkTree, hierarchy.concat([bookmarkTree.title]));
});
return;
}
}
chrome.bookmarks.getTree(function (bookmarkTree) {
removeAll(bookmarkTree[0], []);
});
},
CreateHierarchy: function (folder) {
var store = this;
function write(folder, bookmarkTree) {
var childrenByName = {};
if (bookmarkTree.children) {
bookmarkTree.children.forEach(function (subBookmarkTree) {
if (!('url' in subBookmarkTree)) {
childrenByName[subBookmarkTree.title] = subBookmarkTree;
}
});
}
folder._folders.forEach(function (subFolder) {
if (subFolder.title in childrenByName) {
subFolderTree = childrenByName[subFolder.title];
// We have to move the folder to reset the index for ordering
store.MoveBookmark(subFolderTree.id, bookmarkTree.id);
write(subFolder, childrenByName[subFolder.title]);
} else {
store.CreateBookmarkFolder(subFolder.title, bookmarkTree.id, function (tagTree) {
childrenByName[subFolder.title] = tagTree;
write(subFolder, childrenByName[subFolder.title]);
});
}
});
folder._bookmarks.forEach(function (bookmark) {
store.MoveBookmark(bookmark.id, bookmarkTree.id);
});
}
chrome.bookmarks.getSubTree('1', function (bookmarkTreeParent) {
var bookmarksBar = bookmarkTreeParent[0];
write(folder, bookmarksBar);
});
}
};
return new ChromeBookmarkStore();
});