-
Notifications
You must be signed in to change notification settings - Fork 27
/
openBookmarksHistoryEtcInNewTabs.uc.js
178 lines (177 loc) · 7.7 KB
/
openBookmarksHistoryEtcInNewTabs.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
// ==UserScript==
// @name Open Bookmarks, History, etc. in New Tabs
// @version 1.2.7
// @author aminomancer
// @homepageURL https://github.com/aminomancer/uc.css.js
// @description In vanilla Firefox, `browser.tabs.loadBookmarksInTabs` only affects bookmark items. When you enable this pref and left-click a bookmark (e.g., in the bookmarks toolbar or menu) it opens in a new tab instead of in the current tab. But if you left-click a history entry or a synced tab, it will still open in the current tab. So you'd have to middle click or ctrl+click to avoid losing your current tab's navigation state. This script just makes that preference apply to history and synced tabs too.
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/openBookmarksHistoryEtcInNewTabs.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/openBookmarksHistoryEtcInNewTabs.uc.js
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// @include main
// @include chrome://browser/content/syncedtabs/sidebar.xhtml
// ==/UserScript==
(function () {
function init() {
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
BrowserUtils: "resource://gre/modules/BrowserUtils.sys.mjs",
PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
});
if (window.PlacesUIUtils && !PlacesUIUtils._hasBeenModifiedForOBHNT) {
function getBrowserWindow(aWindow) {
return aWindow &&
aWindow.document.documentElement.getAttribute("windowtype") ==
"navigator:browser"
? aWindow
: BrowserWindowTracker.getTopWindow();
}
eval(
`PlacesUIUtils.openNodeWithEvent = function ${PlacesUIUtils.openNodeWithEvent
.toSource()
.replace(/\(function PUIU_openNodeWithEvent/, "")
.replace(/ && lazy.PlacesUtils\.nodeIsBookmark\(aNode\)/, "")
.replace(/\)$/, "")}`
);
PlacesUIUtils._hasBeenModifiedForOBHNT = true;
}
if (window.HistoryMenu) {
let proto = HistoryMenu.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
proto._onCommand = function (e) {
e = BrowserUtils.getRootEvent(e);
let placesNode = e.target._placesNode;
if (placesNode) {
if (!PrivateBrowsingUtils.isWindowPrivate(window)) {
PlacesUIUtils.markPageAsTyped(placesNode.uri);
}
let where = BrowserUtils.whereToOpenLink(e, false, true);
if (PlacesUIUtils.loadBookmarksInTabs) {
if (where == "current") where = "tab";
if (where == "tab" && gBrowser.selectedTab.isEmpty) {
where = "current";
}
}
openTrustedLinkIn(placesNode.uri, where);
}
};
proto._onClick = function (e) {
let modifKey =
AppConstants.platform == "macosx"
? e.metaKey || e.shiftKey
: e.ctrlKey || e.shiftKey;
if (e.button == 2 || (e.button == 0 && !modifKey)) return;
let target = e.originalTarget;
let tag = target.tagName;
if (
PlacesUIUtils.openInTabClosesMenu &&
(tag == "menuitem" || tag == "menu")
) {
closeMenus(e.target);
}
if (e.button == 1 && !(tag == "menuitem" || tag == "menu")) {
this.onCommand(e);
}
};
proto._onMouseUp = function (e) {
if (e.button == 2 || PlacesUIUtils.openInTabClosesMenu) return;
let target = e.originalTarget;
if (target.tagName != "menuitem") return;
let modifKey =
AppConstants.platform === "macosx" ? e.metaKey : e.ctrlKey;
if (modifKey || e.button == 1) {
target.setAttribute("closemenu", "none");
let menupopup = target.parentNode;
menupopup.addEventListener(
"popuphidden",
() => target.removeAttribute("closemenu"),
{
once: true,
}
);
} else {
target.removeAttribute("closemenu");
}
};
let popup = document.getElementById("historyMenuPopup");
popup.setAttribute(
"onclick",
`this.parentNode._placesView._onClick(event);`
);
popup.setAttribute(
"onmouseup",
`this.parentNode._placesView._onMouseUp(event);`
);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (window.PlacesPanelview) {
let proto = PlacesPanelview.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
eval(
`proto._onCommand = function ${proto._onCommand
.toSource()
.replace(/_onCommand/, "")
.replace(
/(button\.parentNode\.id == \"panelMenu_bookmarksMenu\")/,
`$1 || button.parentNode.id == "appMenu_historyMenu"`
)
.replace(
/(button\.parentNode\.id != \"panelMenu_bookmarksMenu\")/,
`($1 && button.parentNode.id != "appMenu_historyMenu")`
)}`
);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (window.SyncedTabsPanelList) {
let proto = SyncedTabsPanelList.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
eval(
`proto._createSyncedTabElement = function ${proto._createSyncedTabElement
.toSource()
.replace(/_createSyncedTabElement/, "")
.replace(/BrowserUtils\.whereToOpenLink\(e\)/, "preWhere")
.replace(
/document\.defaultView\.openUILink\(tabInfo\.url, e, {\n[^\S\r\n]*triggeringPrincipal.*\n[^\S\r\n]*{}\n[^\S\r\n]*\),\n[^\S\r\n]*}\);/,
`let where = BrowserUtils.whereToOpenLink(e, false, true);\n let preWhere = where;\n if (document.defaultView.PlacesUIUtils.loadBookmarksInTabs) {\n if (where == "current") where = "tab";\n if (where == "tab" && document.defaultView.gBrowser.selectedTab.isEmpty) where = "current";\n }\n document.defaultView.openTrustedLinkIn(tabInfo.url, where);`
)}`
);
proto._hasBeenModifiedForOBHNT = true;
}
}
if (location.href === `chrome://browser/content/syncedtabs/sidebar.xhtml`) {
let proto = syncedTabsDeckComponent.tabListComponent._View.prototype;
if (!proto._hasBeenModifiedForOBHNT) {
proto.onOpenSelected = function (url, e) {
let browserWindow = this._window.browsingContext.topChromeWindow;
let where = lazy.BrowserUtils.whereToOpenLink(e, false, true);
if (browserWindow.PlacesUIUtils.loadBookmarksInTabs) {
if (where == "current") where = "tab";
if (where == "tab" && browserWindow.gBrowser.selectedTab.isEmpty) {
where = "current";
}
}
this.props.onOpenTab(url, where, {});
};
proto._hasBeenModifiedForOBHNT = true;
}
}
}
if (
location.href !== `chrome://browser/content/browser.xhtml` ||
gBrowserInit.delayedStartupFinished
) {
init();
} else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
init();
}
};
Services.obs.addObserver(
delayedListener,
"browser-delayed-startup-finished"
);
}
})();