-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
59 lines (52 loc) · 1.9 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
async function showBookmarkWindow(tabs) {
let window = await browser.windows.create({
type: 'popup', url: '/bookmarks.html',
top: 0, left: 0, width: 500, height: 250
});
browser.tabs.onUpdated.addListener(
function update(tabid, change, tab) {
if (tabid != window.tabs[0].id || tab.url == 'about:blank' || change.status != 'complete') {
return;
}
browser.runtime.sendMessage({
type: 'add-bookmarks',
pages: tabs.map((t) => ({
id: t.id,
index: t.index,
title: t.title,
url: t.url})
)
});
browser.tabs.onUpdated.removeListener(update);
});
}
async function bookmarkRight(window, index) {
let tabs = await browser.tabs.query({windowId: window});
//to the right of index
tabs = tabs.filter((t) => t.index > index);
//remove duplicate tabs
tabs = tabs.filter((t1, i1) =>
tabs.every((t2, i2) =>
i2 >= i1 || t2.url != t1.url
));
showBookmarkWindow(tabs);
}
browser.contextMenus.create({
contexts: ['tab'],
id: 'bookmark-right',
title: browser.i18n.getMessage('menuItem')
});
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId == 'bookmark-right') {
bookmarkRight(tab.windowId, tab.index);
}
});
browser.commands.onCommand.addListener(async (command) => {
if (command == 'bookmark-right') {
let tabs = await browser.tabs.query({
active: true,
windowId: browser.windows.WINDOW_ID_CURRENT
});
bookmarkRight(browser.windows.WINDOW_ID_CURRENT, tabs[0].index);
}
});