-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
113 lines (98 loc) · 3.39 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Icons from <div>Icons made by <a href="https://www.flaticon.com/authors/roundicons" title="Roundicons">Roundicons</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div>
let isEnabled = true;
var maxTabs;
var tabsCount;
let tabLimit;
let limit;
chrome.storage.sync.get("tabsLimit", function(data) {
maxTabs = data.tabsLimit;
// console.log(data);
console.log(maxTabs);
// var settingEnabled = data.someSetting;
});
// Notification data
var opt = {
type: "basic",
title: "Oh noes!",
message: "You hit your tabs limit!",
iconUrl: "icons/tab_128.png",
}
// Updates the badge text and background color.
function updateBadgeText() {
percent = tabsCount / maxTabs * 100;
// console.log(percent);
if (percent >= 25 && percent < 50) {
chrome.browserAction.setBadgeBackgroundColor({ color: '#13B618' })
} else if (percent >= 50 && percent < 75) {
chrome.browserAction.setBadgeBackgroundColor({ color: '#E77714' })
} else if (percent >= 75 && percent <= 100) {
chrome.browserAction.setBadgeBackgroundColor({ color: '#CD482C' })
}
chrome.browserAction.setBadgeText({ text: "" + tabsCount });
}
// Updates the tab count.
function updateTabsCount() {
chrome.tabs.query({
windowType: 'normal',
pinned: false
}, function(tabs) {
tabsCount = tabs.length;
updateBadgeText();
});
}
// Handles a new tab and closes it if the limit has been hit.
function handleTabCreated(tab) {
tabid = tab.id;
console.log('count:' + tabsCount);
console.log('max:' + maxTabs);
if (tabsCount >= maxTabs) {
// console.log(tab);
chrome.tabs.remove(tabid);
chrome.notifications.create(opt);
} else {
updateTabsCount();
}
}
// Handles the tab removed
function handleTabRemoved(tab) {
updateTabsCount();
}
// This handles when the tab has been updated. Unused for now.
function handleTabUpdated(tab) {
// updateTabsCount();
}
function init() {
updateTabsCount();
chrome.tabs.onCreated.addListener(handleTabCreated);
chrome.tabs.onRemoved.addListener(handleTabRemoved);
chrome.tabs.onUpdated.addListener(handleTabUpdated);
}
function teardown() {
chrome.tabs.onCreated.removeListener(handleTabCreated);
chrome.tabs.onRemoved.removeListener(handleTabRemoved);
chrome.tabs.onUpdated.removeListener(handleTabUpdated);
}
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({ 'url': chrome.extension.getURL('options.html') }, function(tab) {
// Launching options :)
});
});
// chrome.browserAction.onClicked.addListener(function (tab) {
// if (!isEnabled) {
// init();
// chrome.tabs.create({'url': chrome.extension.getURL('options.html')}, function(tab) {
// // Tab opened.
// });
// // chrome.browserAction.setIcon({ path: "icons/tab_128.png" });
// }
// else {
// teardown();
// chrome.tabs.create({'url': chrome.extension.getURL('options.html')}, function(tab) {
// // Tab opened.
// });
// // chrome.browserAction.setIcon({ path: "icons/tab_24.png" });
// // chrome.browserAction.setBadgeText({ 'text': '' });
// }
// isEnabled = !isEnabled;
// });
init();