/
background.js
50 lines (45 loc) · 1.37 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
let lastClick = 0;
let timer = null;
chrome.action.onClicked.addListener((tab) => {
chrome.storage.sync.get(['websites', 'reverse'], function(data) {
let websites = data.websites || [];
let reverse = data.reverse || false;
if (timer === null) {
timer = setTimeout(function() {
for (var i = 0; i < websites.length; i++) {
chrome.tabs.create({ url: websites[i] });
}
timer = null; // reset timer
}, 300); // set the timer
} else {
clearTimeout(timer);
timer = null;
chrome.tabs.query({}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
var tab = tabs[i];
var inList = websites.some(function(website) {
return tab.url.indexOf(website) === 0;
});
if ((reverse && !inList) || (!reverse && inList)) { // Change the logic here
chrome.tabs.remove(tab.id);
}
}
});
}
});
});
// Create a context menu item
chrome.runtime.onInstalled.addListener(function() {
const editWebsitesTitle = chrome.i18n.getMessage("editWebsites");
chrome.contextMenus.create({
id: 'options',
title: editWebsitesTitle,
contexts: ['action']
});
});
// Handle context menu item clicks
chrome.contextMenus.onClicked.addListener(function(info) {
if (info.menuItemId === 'options') {
chrome.runtime.openOptionsPage();
}
});