/
focus-last-tab.uc.js
executable file
·260 lines (248 loc) · 11.3 KB
/
focus-last-tab.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// ==UserScript==
// @name Focus Last or Unread Tab
// @description タブを閉じたときに直前に開いてたタブか未読タブにフォーカスしたり
// @version 1.4.8
// @include main
// @compatibility Firefox ESR31.3, 34.0.5
// @author oflow
// @namespace http://oflow.me/archives/1269
// ==/UserScript==
(function() {
// ピン留めしたタブも含めるか [true=ピン留めタブもフォーカス, false=ピン留めは無視]
const FLT_FOCUS_PINNED = true;
// タブバー上のShift + マウスホイールで順番にタブをフォーカス [true=使う, false=使わない]
const FLT_USE_SHIFT_WHEEL = true;
// タブの右クリックメニューに並び替えと未読にするを追加 [true=追加する, false=しない]
const FLT_USE_CONTEXTMENU = true;
// 未読タブ用のCSS
var FLT_UNREAD_CSS = '.tabbrowser-tab[data-flt-unread] .tab-text { }';
/* 未読CSSサンプル */
/*
var FLT_UNREAD_CSS = '.tabbrowser-tab[data-flt-unread] {\
padding-top: 0 !important;\
}\
.tabbrowser-tab[pinned][data-flt-unread] {\
padding-top: 1px !important;\
}\
.tabbrowser-tab[data-flt-unread] .tab-stack {\
border-top: 2px solid #e55 !important;\
margin-top: -2px !important;\
padding-top: 2px !important;\
}\
.tabbrowser-tab[data-flt-unread] .tab-icon-image,\
.tabbrowser-tab[data-flt-unread] .tab-throbber {\
margin-top: -1px !important;\
}\
.tabbrowser-tab[data-flt-unread] .tab-text {\
font-weight: normal !important;\
}';
*/
// Session store APIを使ってタブにタイムスタンプ・未読を保存する
var ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
var ucjsFocusLastTab = {
handleEvent: function(e) {
var tab = e.target;
switch (e.type) {
case 'TabOpen':
this.open(tab);
break;
case 'TabClose':
this.close(tab);
break;
case 'TabSelect':
if (!this.scroll) {
// ホイールスクロール中にタイムスタンプ更新するとマズい
this.select(tab);
}
this.scroll = false;
break;
case 'DOMMouseScroll':
if (e.shiftKey) {
this.scroll = true;
this.wheel(gBrowser.selectedTab, e.detail);
e.stopPropagation();
}
break;
case 'SSTabRestoring':
this.restore();
break;
case 'unload':
this.uninit();
break;
}
},
_sort: function(tabs) {
if (!tabs || !tabs.length) {
return [];
}
tabs.sort(function(a, b) {
if (a.timestamp < b.timestamp) {
return -1;
} else if (a.timestamp > b.timestamp) {
return 1;
} else {
return 0;
}
});
return tabs;
},
restore: function() {
// 復元時に未読属性がついてないのをなんとかする
var tabs = gBrowser.tabs,
tabsLen = tabs.length;
for (var i = 0; i < tabsLen; i++) {
var unread = ss.getTabValue(tabs[i], 'data-flt-unread');
if (unread) {
this.markAsUnread(tabs[i]);
} else {
this.markAsRead(tabs[i]);
}
}
},
sort: function() {
var tabs = gBrowser.tabs,
tabsLen = tabs.length,
container = [],
pinnedTab = 0;
for (var i = 0; i < tabsLen; i++) {
var timestamp = ss.getTabValue(tabs[i], 'data-flt-timestamp');
if (tabs[i].pinned) {
pinnedTab++;
} else {
container.push({'timestamp': timestamp, 'tab': tabs[i]});
}
}
this._sort(container).reverse().forEach(function(sortedTabs) {
gBrowser.moveTabTo(sortedTabs.tab, pinnedTab);
});
},
wheel: function(selectedTab, detail) {
var tabs = selectedTab.parentNode.childNodes,
tabsLen = tabs.length,
container = [],
selectedTimestamp = ss.getTabValue(selectedTab, 'data-flt-timestamp');
for (var i = 0; i < tabsLen; i++) {
var timestamp = ss.getTabValue(tabs[i], 'data-flt-timestamp');
if (tabs[i] == selectedTab || !timestamp) {
continue;
}
if (detail < 0) {
// ↑スクロールで1つ戻る
if (selectedTimestamp < timestamp) {
continue;
}
} else if (detail > 0) {
// ↓スクロールで1つ進む
if (selectedTimestamp > timestamp) {
continue;
}
}
container.push({'timestamp': timestamp, 'tab': tabs[i]});
}
if (container.length) {
var index = (detail > 0 ? container.length - 1 : 0);
gBrowser.selectedTab = this._sort(container)[index].tab;
}
},
close: function(tab) {
var tabs = tab.parentNode.childNodes,
tabsLen = tabs.length,
container = [],
hasUnread = false;
if (tab.getAttribute('selected') !== 'true') {
// 現在開いているタブじゃない場合は何もしない
return;
}
for (var i = 0; i < tabsLen; i++) {
var timestamp = ss.getTabValue(tabs[i], 'data-flt-timestamp'),
unread = ss.getTabValue(tabs[i], 'data-flt-unread');
if (!FLT_FOCUS_PINNED && tabs[i].pinned) {
continue;
}
if (tabs[i] == tab || !timestamp) {
continue;
}
if (unread) {
if (!hasUnread) {
container = [];
}
hasUnread = true;
container.push({'timestamp': timestamp, 'tab': tabs[i]});
} else if (!hasUnread) {
container.push({'timestamp': timestamp, 'tab': tabs[i]});
}
}
if (container.length) {
var index = (hasUnread ? 0 : container.length - 1),
selectedTab = this._sort(container)[index].tab;
if (hasUnread) {
this.markAsRead(selectedTab);
}
gBrowser.selectedTab = selectedTab;
}
},
select: function(tab) {
// 選択したらタイムスタンプ更新して未読消す
ss.setTabValue(tab, 'data-flt-timestamp', Date.now().toString());
this.markAsRead(tab);
},
open: function(tab) {
// タイムスタンプ追加、未読追加
ss.setTabValue(tab, 'data-flt-timestamp', Date.now().toString());
this.markAsUnread(tab);
},
markAsUnread: function(tab) {
tab.setAttribute('data-flt-unread', 'true');
ss.setTabValue(tab, 'data-flt-unread', 'true');
},
markAsRead: function(tab) {
tab.removeAttribute('data-flt-unread');
ss.deleteTabValue(tab, 'data-flt-unread');
},
createMenu: function() {
var tabContextMenu = document.getElementById('tabContextMenu'),
insertBeforeMenu = document.getElementById('context_reloadAllTabs');
var menuitem = document.createElement('menuitem');
menuitem.setAttribute('id', 'ucjs_flt_sort_by_timestamp');
menuitem.setAttribute('label', decodeURIComponent(escape('タブを並び替える(FLT)')));
menuitem.addEventListener('command', function() { ucjsFocusLastTab.sort(); }, false);
tabContextMenu.insertBefore(menuitem, insertBeforeMenu);
var menuitem = document.createElement('menuitem');
menuitem.setAttribute('id', 'ucjs_flt_mark_as_unread');
menuitem.setAttribute('label', decodeURIComponent(escape('タブを未読にする(FLT)')));
menuitem.addEventListener('command', function() { ucjsFocusLastTab.markAsUnread(document.popupNode); }, false);
tabContextMenu.insertBefore(menuitem, insertBeforeMenu);
},
init: function() {
if (FLT_USE_CONTEXTMENU) {
this.createMenu();
}
// loadInBackground trueにしてるのでTabOpenも入れとく
gBrowser.tabContainer.addEventListener('TabClose', this, false);
gBrowser.tabContainer.addEventListener('TabSelect', this, false);
gBrowser.tabContainer.addEventListener('TabOpen', this, false);
if (FLT_USE_SHIFT_WHEEL) {
gBrowser.tabContainer.addEventListener('DOMMouseScroll', this, true);
}
if (FLT_UNREAD_CSS) {
var style = document.createProcessingInstruction('xml-stylesheet','type="text/css" href="data:text/css,'+ encodeURIComponent(FLT_UNREAD_CSS) +'"');
style.id = 'ucjs-flt-style';
document.insertBefore(style, document.documentElement);
}
// セッション復元時に未読属性を戻す
gBrowser.tabContainer.addEventListener('SSTabRestoring', this, false);
window.addEventListener('unload', this, false);
},
uninit: function() {
gBrowser.tabContainer.removeEventListener('TabClose', this, false);
gBrowser.tabContainer.removeEventListener('TabSelect', this, false);
gBrowser.tabContainer.removeEventListener('TabOpen', this, false);
if (FLT_USE_SHIFT_WHEEL) {
gBrowser.tabContainer.removeEventListener('DOMMouseScroll', this, true);
}
gBrowser.tabContainer.removeEventListener('SSTabRestoring', this, false);
window.removeEventListener('unload', this, false);
}
};
ucjsFocusLastTab.init();
})();