-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
41 lines (38 loc) · 1.23 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
const folderName = chrome.i18n.getMessage('folderName');
function setupBookmarkFolder(details) {
chrome.bookmarks.search(folderName, results => {
if (!results.length) {
chrome.bookmarks.getTree(tree => {
chrome.bookmarks.create({
parentId: tree.shift().children.pop().id,
title: folderName,
}, result => {
console.log(`${folderName} folder successfully created`);
});
});
}
});
};
function handleBookmark(id, bookmark) {
chrome.bookmarks.get(bookmark.parentId, bookmarks => {
if (bookmarks.length === 1 && bookmarks[0].title === folderName) {
chrome.windows.getAll({
windowTypes: ['normal']
}, windows => {
let minIdWindow = windows.reduce((resultWindow, window) => {
return (window.id < resultWindow.id) ? window : resultWindow;
}, windows[0]);
chrome.tabs.create({
windowId: minIdWindow.id,
url: bookmark.url,
active: false,
}, tab => {
chrome.bookmarks.remove(id);
});
});
}
});
};
chrome.runtime.onInstalled.addListener(setupBookmarkFolder)
chrome.bookmarks.onCreated.addListener(handleBookmark);
chrome.bookmarks.onMoved.addListener(handleBookmark);