-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
75 lines (71 loc) · 2.22 KB
/
background.js
File metadata and controls
75 lines (71 loc) · 2.22 KB
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
browser.runtime.onInstalled.addListener(() => {
const request = indexedDB.open('devnull');
request.onerror = (e) => {
console.error(`Database error: ${e.target.errorCode}`);
}
request.onupgradeneeded = (e) => {
const db = e.target.result;
db.createObjectStore('groups', { keyPath: 'timestamp' });
}
});
function getTabs() {
const opts = { currentWindow: true, pinned: false };
const tabs = browser.tabs.query(opts).then((tabs) => {
return tabs.map((tab) => {
return {
id: tab.id,
title: tab.title,
url: tab.url,
highlighted: tab.highlighted,
cookieStoreId: tab.cookieStoreId
}
}).filter((tab) => tab.url !== browser.runtime.getURL('/devnull.html'));
});
return tabs;
}
function saveTabs(tabs) {
indexedDB.open('devnull').onsuccess = (e) => {
const db = e.target.result;
const store = db.transaction('groups', 'readwrite').objectStore('groups');
const highlighted = tabs.filter((tab) => tab.highlighted);
const selected = (highlighted.length > 1) ? highlighted : tabs;
const group = {
timestamp: Date.now(),
title: 'untitled unmastered',
tabs: selected.map((tab) => {
delete tab.id;
delete tab.highlighted;
return tab;
}).filter((tab) => tab.url !== 'about:newtab')
};
if (group.tabs.length > 0) {
store.add(group);
browser.tabs.query({ url: 'moz-extension://*/devnull.html' }, (tab) => {
if (tab.length)
browser.runtime.sendMessage(group);
});
}
}
}
function closeTabs(tabs) {
browser.tabs.query({ url: 'moz-extension://*/devnull.html' }, (tab) => {
if (!tab.length) {
browser.tabs.create({ url: 'devnull.html', pinned: true, index: 0, active: true });
} else {
browser.tabs.update(tab[0].id, { active: true });
}
browser.tabs.remove(tabs.map((tab) => tab.id));
});
}
browser.browserAction.onClicked.addListener((tab, OnClickData) => {
if (!OnClickData.modifiers.length) {
getTabs().then((tabs) => {
saveTabs(tabs);
closeTabs(tabs);
});
} else if (OnClickData.modifiers.includes('Command') || OnClickData.modifiers.includes('Alt')) {
getTabs().then((tabs) => {
closeTabs(tabs);
});
}
});