forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscardTab.uc.js
103 lines (90 loc) · 2.85 KB
/
discardTab.uc.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
// ==UserScript==
// @name discardTab.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description discard tab
// @include main
// @author Alice0775
// @compatibility 60
// @version 2018/09/27 10:30
// ==/UserScript==
var discardTab = {
init: function(){
this.tabContextMenu();
gBrowser.tabContainer.contextMenu.addEventListener('popupshowing', this, false);
window.addEventListener('unload', this, false)
},
uninit: function(){
gBrowser.tabContainer.contextMenu.removeEventListener('popupshowing', this, false);
window.removeEventListener('unload', this, false)
},
handleEvent: function(event) {
switch(event.type) {
case "unload":
this.uninit(event);
break;
case "popupshowing":
this.popupshowing(event);
break;
}
},
tabContextMenu: function(){
//tab context menu
var tabContext = gBrowser.tabContainer.contextMenu;
var menuitem = this.discardTabMenu
= tabContext.appendChild(
document.createElement("menuitem"));
menuitem.id = "discardTab";
if (Services.appinfo.version.split(".")[0] >= 63)
menuitem.setAttribute("label", "Discard Tab(s)");
else
menuitem.setAttribute("label", "Discard Tab");
menuitem.setAttribute("accesskey", "D");
menuitem.setAttribute("oncommand","discardTab.discardTab(TabContextMenu.contextTab);");
tabContext.addEventListener('popupshowing', this, false);
},
popupshowing: function(event) {
if (typeof gBrowser.selectedTabs != "undefined") {
let tabs = this.getSelectedTabs(TabContextMenu.contextTab);
if (tabs.length == 1) {
this.discardTabMenu.disabled = tabs[0].selected;
} else {
this.discardTabMenu.disabled = this.getSelectedTabs(TabContextMenu.contextTab) < 1;
}
} else {
this.discardTabMenu.disabled = TabContextMenu.contextTab.selected;
}
},
discardTab: function(aTab){
if (typeof gBrowser.selectedTabs != "undefined") {
this.discardSelectedTabs(this.getSelectedTabs(aTab));
} else {
this.discardSelectedTab(aTab);
}
},
discardSelectedTab(aTab) {
if (!aTab.hasAttribute("pending") &&
!aTab.hasAttribute("busy") &&
!aTab.closing &&
!aTab.selectedTab)
gBrowser.discardBrowser(aTab.linkedBrowser);
},
discardSelectedTabs: function(tabs) {
if (tabs.length < 1)
return;
for (let tab of tabs) {
this.discardSelectedTab(tab);
}
},
getSelectedTabs: function(aTab){
let contextTab = aTab;
let selectedTabs = [contextTab];
if (gBrowser.selectedTabs.indexOf(contextTab) < 0)
return selectedTabs;
for (let tab of gBrowser.selectedTabs) {
if (contextTab != tab)
selectedTabs.push(tab);
}
return selectedTabs;
}
}
discardTab.init();