-
Notifications
You must be signed in to change notification settings - Fork 2
/
shared.js
77 lines (71 loc) · 1.86 KB
/
shared.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
const FOLDER_TITLE = 'stack';
var getRootFolder = function(callback) {
// try firefox
chrome.bookmarks.get('unfiled_____', function(folder) {
if (!chrome.runtime.lastError) {
callback(folder[0]);
} else {
// In chrome, it the folders are indexed depth-first.
// So root is 0, "bookmark bar" is 1, "other bookmarks" is 2.
// However, if you already had bookmarks when ids were introduced,
// "other bookmarks" may have a higher id.
//
// See https://bugs.chromium.org/p/chromium/issues/detail?id=21330
chrome.bookmarks.getChildren('0', function(children) {
callback(children[1]);
});
}
});
};
var ensureFolder = function(callback) {
getRootFolder(function(root) {
chrome.bookmarks.getChildren(root.id, function(children) {
var folder = children.find(child => child.title === FOLDER_TITLE);
if (!folder) {
chrome.bookmarks.create({
parentId: root.id,
title: FOLDER_TITLE,
}, callback);
} else {
callback(folder);
}
});
});
};
var getBookmarks = function(callback) {
ensureFolder(function(folder) {
chrome.bookmarks.getChildren(folder.id, callback);
});
};
var updateCount = function(callback) {
getBookmarks(function(bookmarks) {
chrome.browserAction.setBadgeText({text: '' + bookmarks.length});
if (callback) callback();
});
};
var popBookmark = function(id, callback) {
chrome.bookmarks.get(id, function(items) {
chrome.bookmarks.remove(id, function() {
updateCount(function() {
callback(items[0]);
});
});
});
};
var pushBookmark = function(tab, callback) {
ensureFolder(function(folder) {
var url = tab.url;
if (tab.isInReaderMode) {
var _url = new URL(url);
var _search = new URLSearchParams(_url.search);
url = _search.get('url');
}
chrome.bookmarks.create({
parentId: folder.id,
title: tab.title,
url: url,
}, function() {
updateCount(callback);
});
});
};