-
Notifications
You must be signed in to change notification settings - Fork 35
/
UndoCloseTabButtonN.uc.js
208 lines (185 loc) · 9.03 KB
/
UndoCloseTabButtonN.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
// ==UserScript==
// @name UndoCloseTabButtonN
// @description 閉じたタブを復元するツールバーボタン&タブバーの空き上の中クリックで最後に閉じたタブを復元
// @version 1.2.6
// @include main
// @charset UTF-8
// @note 2023/08/16 Fx117 fix this is undefined
// @note 2023/06/08 Fx115 SessionStore.getClosedTabData → SessionStore.getClosedTabDataForWindow
// @note 2022/11/12 修改左中右按键行为
// @note 2021/12/12 Fx95 SessionStore.getClosedTabData / getClosedWindowData の戻り値がJSONからArrayに変更
// @note 2019/01/23 Fx66でタブバー中クリックが効かないのを修正
// @note 2019/07/04 Fx69
// @note 2019/09/03 Fx70
// @note 2019/12/09 Fx72
// ==/UserScript==
// アイコン初期位置はタブバーです
(function () {
"use strict";
const useTabbarMiddleClick = true; // タブバーの空き・タブ追加ボタン上の中クリックで最後に閉じたタブを復元するか?
const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
window.ucjsUndoCloseTabButtonService = {
prepareMenu(event) {
const doc = (event.view && event.view.document) || document;
const menu = event.originalTarget;
this.removeChilds(menu);
// 閉じたタブ
let data = "getClosedTabDataForWindow" in SessionStore ? SessionStore.getClosedTabDataForWindow(window) : SessionStore.getClosedTabData(window);
if (typeof (data) === "string") {
data = JSON.parse(data);
}
const tabLength = data.length;
for (let i = 0; i < tabLength; i++) {
const item = data[i];
const m = this.createFaviconMenuitem(doc, item.title, item.image, i, this.undoTab);
const state = item.state;
let idx = state.index;
if (idx == 0)
idx = state.entries.length;
if (--idx >= 0 && state.entries[idx])
m.setAttribute("targetURI", state.entries[idx].url);
menu.appendChild(m);
}
// 閉じたウィンドウ
data = SessionStore.getClosedWindowData(window);
if (typeof (data) === "string") {
data = JSON.parse(data);
}
const winLength = data.length;
if (winLength > 0) {
if (tabLength > 0)
menu.appendChild(this.$C(doc, "menuseparator"));
menu.appendChild(this.$C(doc, "menuitem", {
disabled: true,
label: Services.locale.appLocaleAsBCP47.includes("zh-") ? "已关闭的窗口" : "閉じたウインドウ"
}));
for (let i = 0; i < winLength; i++) {
const item = data[i];
let title = item.title;
const tabsCount = item.tabs.length - 1;
if (tabsCount > 0)
title += " (他:" + tabsCount + ")";
const tab = item.tabs[item.selected - 1];
const m = this.createFaviconMenuitem(doc, title, tab.image, i, this.undoWindow);
menu.appendChild(m);
}
}
if (tabLength + winLength === 0) {
/* menu.appendChild(this.$C(doc, "menuitem", {
disabled: true,
label : "履歴がありません"
}));*/
event.preventDefault();
}
},
createFaviconMenuitem(doc, label, icon, value, command) {
const attr = {
class: "menuitem-iconic bookmark-item menuitem-with-favicon",
label: label,
value: value
};
if (icon) {
if (/^https?:/.test(icon))
icon = "moz-anno:favicon:" + icon;
attr.image = icon;
}
const m = this.$C(doc, "menuitem", attr);
m.addEventListener("command", command, false);
return m;
},
undoTab(event) {
undoCloseTab(event.originalTarget.getAttribute("value"));
},
undoWindow(event) {
undoCloseWindow(event.originalTarget.getAttribute("value"));
},
removeChilds(element) {
const range = document.createRange();
range.selectNodeContents(element);
range.deleteContents();
},
onClick(event) {
if (event.button === 0 && event.target.id === "ucjs-undo-close-tab-button") {
console.log(event.target.id)
event.preventDefault();
event.stopPropagation();
undoCloseTab();
} else if (event.button == 1) {
switch (event.originalTarget.localName) {
case "box": // -Fx65
case "scrollbox": // Fx66-
event.preventDefault();
event.stopPropagation();
undoCloseTab();
break;
}
} else if (event.button === 2 && event.target.id === "ucjs-undo-close-tab-button") {
event.preventDefault();
event.stopPropagation();
let pos = "after_end", x, y;
if ((event.target.ownerGlobal.innerWidth / 2) > event.pageX) {
pos = "after_position";
x = 0;
y = 0 + event.target.clientHeight;
}
event.target.querySelector("menupopup").openPopup(event.target, pos, x, y);
}
},
$C(doc, tag, attrs) {
const e = tag instanceof Node ? tag : doc.createElementNS(XULNS, tag);
if (attrs) {
Object.entries(attrs).forEach(([key, value]) => e.setAttribute(key, value));
}
return e;
},
};
function run() {
if (useTabbarMiddleClick) {
gBrowser.tabContainer.addEventListener("click", ucjsUndoCloseTabButtonService.onClick, true);
}
const buttonId = "ucjs-undo-close-tab-button";
if (document.getElementById(buttonId)) {
return;
}
try {
Cu.import("resource:///modules/CustomizableUI.jsm");
CustomizableUI.createWidget({
id: buttonId,
defaultArea: CustomizableUI.AREA_TABSTRIP,
type: "custom",
onBuild: doc => {
const btn = ucjsUndoCloseTabButtonService.$C(doc, "toolbarbutton", {
id: buttonId,
class: "toolbarbutton-1 chromeclass-toolbar-additional",
type: "contextmenu",
anchor: "dropmarker",
label: Services.locale.appLocaleAsBCP47.includes("zh-") ? "已关闭的标签" : "閉じたタブ",
tooltiptext: Services.locale.appLocaleAsBCP47.includes("zh-") ? "查看已经关闭的标签\n中键快速打开最后一个关闭的标签" : "閉じたタブ\n中クリックで最後に閉じたタブを復元",
image: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSIwIDAgNTEyIDUxMiIgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIj4KICAgIDxwYXRoIHN0cm9rZS13aWR0aD0iMjQiIGZpbGw9IiM1NTU1NTUiIHN0cm9rZT0iI2ZmZmZmZiIgZD0iTSA2IDQ4MCBsIDUwMCAwIGwgMCAtNjAgbCAtNTAgMCBsIDAgLTIyMCBsIC00MDAgMCBsIDAgMjIwIGwgLTUwIDAgeiIvPgogICAgPHBhdGggc3Ryb2tlLXdpZHRoPSIzMCIgZmlsbD0iIzQ0ODhmZiIgc3Ryb2tlPSIjZGRlZWZmIiBkPSJNIDI3MiAzMiBsIC0xNjAgMTMwIGwgMTYwIDEzMCBsIDAgLTc1IGwgNjAgMCBhIDYwIDYwIDAgMCAxIDAgMTIwIGwgLTIwIDAgbCAwIDExMCBsIDIwIDAgYSAxNzAgMTcwIDAgMCAwIDAgLTM0MCBsIC02MCAwIHoiLz4KPC9zdmc+",
onclick: "ucjsUndoCloseTabButtonService.onClick(event);",
});
const menu = ucjsUndoCloseTabButtonService.$C(doc, "menupopup", {
tooltip: "bhTooltip",
popupsinherittooltip: "true",
oncontextmenu: "event.preventDefault();",
onpopupshowing: "ucjsUndoCloseTabButtonService.prepareMenu(event);",
});
btn.appendChild(menu);
return btn;
},
});
} catch (e) { }
}
if (gBrowserInit.delayedStartupFinished) {
run();
} else {
const OBS_TOPIC = "browser-delayed-startup-finished";
const delayedStartupFinished = (subject, topic) => {
if (topic === OBS_TOPIC && subject === window) {
Services.obs.removeObserver(delayedStartupFinished, topic);
run();
}
};
Services.obs.addObserver(delayedStartupFinished, OBS_TOPIC);
}
})();