/
tabContextHistoryMenu.uc.js
189 lines (164 loc) · 6.98 KB
/
tabContextHistoryMenu.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
// ==UserScript==
// @name tabContextHistoryMenu.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description タブのコンテキストメニューにそのタブの履歴を表示, クリックで履歴を遷移, 中クリックで新規タブに選択履歴をオープン
// @include main
// @compatibility Firefox 3.0, 3.5 3.6a2pre 3.7a1pre
// @author Alice0775
// @version 2013/04/21 Bug 713642 asyncFaviconCallers
// @version 2010/03/26 13:00 Minefield/3.7a3pre Bug 554991 - allow tab context menu to be modified by normal XUL overlays
// @version 2010/03/15 00:00 Minefield/3.7a3pre Bug 347930 - Tab strip should be a toolbar instead
// @version 2009/08/22 09:00
// ==/UserScript==
var tabContextHistoryMenu = {
menu: null,
popup: null,
tabContext: null,
init: function() {
//tab context popup
this.tabContext = document.getAnonymousElementByAttribute(
gBrowser, "anonid", "tabContextMenu") ||
gBrowser.tabContainer.contextMenu;
var menu = document.createElement("menu");
menu.setAttribute("id", "tabContextHistoryMenu");
menu.setAttribute("label", "Tab History");
menu.setAttribute("accesskey", "H");
this.menu = this.tabContext.insertBefore(menu, this.tabContext.firstChild);
var popup = document.createElement("menupopup");
this.popup = this.menu.appendChild(popup);
popup.id = "tabContextHistoryMenupopup";
popup.setAttribute("oncommand", "tabContextHistoryMenu.gotoHistoryIndex(event); event.stopPropagation();");
popup.setAttribute("onclick", "checkForMiddleClick(this, event); this.parentNode.parentNode.hidePopup();");
// event
window.addEventListener("unload", this, false);
this.tabContext.addEventListener("popupshowing", this, false);
popup.setAttribute("onpopupshowing",
"tabContextHistoryMenu.populateTabContextHistoryMenu(gBrowser.mContextTab);");
// Lazily add the hover listeners on first showing and never remove them
// Show history item's uri in the status bar when hovering, and clear on exit
popup.addEventListener("DOMMenuItemActive", this, false);
popup.addEventListener("DOMMenuItemInactive", this, false);
},
uninit: function() {
window.removeEventListener("unload", this, false);
this.tabContext.removeEventListener("popupshowing", this, false);
this.popup.removeEventListener("DOMMenuItemActive", this, false);
this.popup.removeEventListener("DOMMenuItemInactive", this, false);
},
handleEvent: function(aEvent) {
switch(aEvent.type) {
case 'DOMMenuItemActive':
//// Only the current page should have the checked attribute, so skip it
//if (!aEvent.target.hasAttribute("checked"))
window.XULBrowserWindow.setOverLink(aEvent.target.getAttribute("uri"));
break;
case 'DOMMenuItemInactive':
window.XULBrowserWindow.setOverLink("");
break;
case 'popupshowing':
this.tabContextPopupshowing();
break;
case 'unload':
this.uninit();
break;
}
},
tabContextPopupshowing: function() {
this.menu.setAttribute("disabled", true);
var aTab = window.gBrowser.mContextTab;
if (aTab) {
var webNav = aTab.linkedBrowser.webNavigation;
var sessionHistory = webNav.sessionHistory;
var count = sessionHistory.count;
if (count > 0)
this.menu.setAttribute("disabled", false);
}
},
aTab: null,
populateTabContextHistoryMenu: function(aTab) {
this.aTab = aTab;
// Remove old entries if any
var children = this.popup.childNodes;
for (var i = children.length - 1; i >= 0; --i) {
if (children[i].hasAttribute("index"))
this.popup.removeChild(children[i]);
}
var webNav = aTab.linkedBrowser.webNavigation;
var sessionHistory = webNav.sessionHistory;
var count = sessionHistory.count;
if (count <= 0) // don't display the popup for a single item
return false;
const MAX_HISTORY_MENU_ITEMS = 15;
var index = sessionHistory.index;
var half_length = Math.floor(MAX_HISTORY_MENU_ITEMS / 2);
var start = Math.max(index - half_length, 0);
var end = Math.min(start == 0 ? MAX_HISTORY_MENU_ITEMS : index + half_length + 1, count);
if (end == count)
start = Math.max(count - MAX_HISTORY_MENU_ITEMS, 0);
var tooltipBack = gNavigatorBundle.getString("tabHistory.goBack");
var tooltipCurrent = gNavigatorBundle.getString("tabHistory.current");
var tooltipForward = gNavigatorBundle.getString("tabHistory.goForward");
for (var j = end - 1; j >= start; j--) {
let item = document.createElement("menuitem");
let entry = sessionHistory.getEntryAtIndex(j, false);
let uri = entry.URI.spec;
item.setAttribute("uri", uri);
item.setAttribute("label", entry.title || uri);
item.setAttribute("index", j);
if (j != index) {
this.getFavicon(uri, (function(uri){this.style.listStyleImage = "url(" + uri.spec + ")";}).bind(item));
}
if (j < index) {
item.className = "unified-nav-back menuitem-iconic";
item.setAttribute("tooltiptext", tooltipBack);
} else if (j == index) {
item.setAttribute("type", "radio");
item.setAttribute("checked", "true");
item.className = "unified-nav-current";
item.setAttribute("tooltiptext", tooltipCurrent);
} else {
item.className = "unified-nav-forward menuitem-iconic";
item.setAttribute("tooltiptext", tooltipForward);
}
this.popup.appendChild(item);
}
return true;
},
getFavicon: function(URL, callback) {
var faviconService = Components.classes["@mozilla.org/browser/favicon-service;1"].getService(Components.interfaces.nsIFaviconService);
var IOService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var URI = IOService.newURI(URL, null, null);
if (typeof faviconService.getFaviconImageForPage == "undefined") {
faviconService.getFaviconURLForPage(URI, callback);
} else {
callback(faviconService.getFaviconImageForPage(URI));
}
},
gotoHistoryIndex: function gotoHistoryIndex(aEvent) {
var index = aEvent.target.getAttribute("index");
if (!index)
return false;
var where = whereToOpenLink(aEvent);
if (where == "current") {
// Normal click. Go there in the current tab and update session history.
gBrowser.selectedTab = this.aTab;
try {
gBrowser.gotoIndex(index);
}
catch(ex) {
return false;
}
return true;
}
else {
// Modified click. Go there in a new tab/window.
// This code doesn't copy history or work well with framed pages.
var sessionHistory = this.aTab.linkedBrowser.webNavigation.sessionHistory;
var entry = sessionHistory.getEntryAtIndex(index, false);
var url = entry.URI.spec;
openUILinkIn(url, where);
return true;
}
}
}
tabContextHistoryMenu.init();