This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
74 lines (68 loc) · 2.16 KB
/
index.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
var title = require('./package.json').title;
var { ActionButton } = require('sdk/ui/button/action');
var simplePrefs = require('sdk/simple-prefs');
var _ = require('sdk/l10n').get;
var windows = require('sdk/windows');
var tabs = require('sdk/tabs');
var notifications = require('sdk/notifications');
var { setTimeout } = require('sdk/timers');
var privateBrowsing = require('sdk/private-browsing');
var chroma = require('chroma-js');
exports.main = function(){
var maxTabs = simplePrefs.prefs.maxTabs;
var max = (maxTabs > 1) ? maxTabs : 10; // Max at least 2 tabs please
var maxPrivateBrowsing = simplePrefs.prefs.maxPrivateBrowsing;
var button = ActionButton({
id: 'max-tabs-button',
label: title,
icon: './icon.svg',
disabled: true
});
var colorScale = chroma.scale(['#A6A6A6', '#B90000']);
var updateButton = function(win, tabsLen){
if (win == 'window') tabsLen = windows.browserWindows.activeWindow.tabs.length;
button.state(win, {
label: title + ' - ' + tabsLen + '/' + max,
badge: (tabsLen > 99) ? '99+' : tabsLen,
badgeColor: colorScale(tabsLen/max).hex()
});
};
var updateAllButtons = function(){
for (let window of windows.browserWindows){
updateButton(window, window.tabs.length);
}
};
updateAllButtons();
simplePrefs.on('maxTabs', function(){
var maxTabs = simplePrefs.prefs.maxTabs;
max = (maxTabs > 1) ? maxTabs : 10;
updateAllButtons();
});
simplePrefs.on('maxPrivateBrowsing', function(){
maxPrivateBrowsing = simplePrefs.prefs.maxPrivateBrowsing;
});
tabs.on('open', function(tab){
var window = tab.window;
// setTimeout is needed because window.tabs.length value seems to update *slower*
setTimeout(function(){
var tabsLen = window.tabs.length;
if (tabsLen <= max || (privateBrowsing.isPrivate(window) && !maxPrivateBrowsing)){
updateButton(window, tabsLen);
} else {
tab.close();
notifications.notify({
title: title,
text: _('not_open_max_tabs', max)
});
}
}, 1);
});
var updateOnClose = function(tab){
var window = tab.window;
setTimeout(function(){
updateButton(window, window.tabs.length);
}, 1);
};
tabs.on('close', updateOnClose);
tabs.on('activate', updateOnClose);
};