forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenLinkInUnloadedTab.uc.js
More file actions
425 lines (403 loc) · 20.8 KB
/
openLinkInUnloadedTab.uc.js
File metadata and controls
425 lines (403 loc) · 20.8 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// ==UserScript==
// @name Open Link in Unloaded Tab (context menu item)
// @version 1.5
// @author aminomancer
// @homepage https://github.com/aminomancer
// @description Add a new menu item to context menus prompted by right/accel-clicking on links or other link-like affordances. The menu item will open the link in a new background tab without loading the page. So the tab will start unloaded or "discarded." The context menu entry appears in the content area context menu when right-clicking a link; and in every menu where bookmarks, history, and synced tabs can be interacted with — sidebar, menubar, toolbar, toolbar button popup, and library window. Script is a remake of "Open in Unloaded Tab" by xiaoxiaoflood, but intended for use with fx-autoconfig by MrOtherGuy. It should still work with other loaders that load user scripts per-window, such as alice0775's loader, but is not compatible with older loaders or those like xiaoxiaoflood's loader. The difference is that those loaders run scripts in the global execution context, and simply call a global function when a window is launched, (the global function takes the window as a parameter) whereas fx-autoconfig loads normal scripts entirely within the window context, unless explicitly told to do otherwise. When you open a bookmark or history item in an unloaded tab, the tab draws its title from the entry in the places database. But when you open a link in an unloaded tab, there is no preexisting title. Normally when opening a link in a tab, the title is updated as the tab loads, but since we're opening the tab unloaded from the beginning, Firefox is less likely to know what the document's final title is. By default, the script works around this by generating a temporary title for the tab based on the text of the link that was opened. So if you click a hyperlink "https://mozilla.org" whose label text says "Mozilla" the title will be set to Mozilla until the tab is loaded. But if you click a hyperlink whose label text is the same as the URL itself, the title will simply be the URL. There's a user preference for this, however. If you just want to use the URL for the title no matter what, toggle this pref to false in about:config: "userChrome.openLinkInUnloadedTab.use_link_text_as_tab_title_when_unknown"
// @include main
// @include chrome://browser/content/places/bookmarksSidebar.xhtml
// @include chrome://browser/content/places/historySidebar.xhtml
// @include chrome://browser/content/places/places.xhtml
// ==/UserScript==
// The default labels are in English, but you can edit the values below to change them. Firefox doesn't natively use any phrases like "Open in New Unlaoded Tab" so there isn't any reasonable way to automatically localize this script. I'd have to do all the localization work myself, but I can only speak two languages and I don't have any help. If anyone wants to contribute localized strings for any of my scripts please feel free to post them on my repo in the Issues or Discussions tab. In the meantime, please edit the strings below yourself to change the language.
const unloadedTabMenuL10n = {
openAll: `Open All in Unloaded Tabs`, // Appears when right-clicking a container/folder in bookmarks or history.
openBookmark: `Open in New Unloaded Tab`, // Appears when right-clicking a bookmark, history item, etc.
openSyncedTab: `Open in a New Unloaded Tab`, // Appears when right-clicking a tab in the synced tabs sidebar.
openLink: `Open Link in New Unloaded Tab`, // Appears when right-clicking a link in-content.
accessKey: `u`, // All of the menu items use a predefined access key. Access keys are underlined in the menu item's label, and pressing them on your keyboard automatically selects the menu item. They serve as hotkeys while the context menu is open. The default access key is "u" for the English, "unloaded." If the chosen access key is not present in the menu item's label, instead of being underlined in the label, it will be shown after the label in parentheses, e.g. "Open All in Unloaded Tabs (G)"
};
(function () {
class UnloadedTabMenuBase {
constructor() {
if (!window.E10SUtils)
XPCOMUtils.defineLazyModuleGetters(this, {
E10SUtils: `resource://gre/modules/E10SUtils.jsm`,
});
else this.E10SUtils = window.E10SUtils;
this.useLinkPref = `userChrome.openLinkInUnloadedTab.use_link_text_as_tab_title_when_unknown`;
this.initPref(this.useLinkPref, true);
this.QUERY_TYPE_BOOKMARKS = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
this.QUERY_TYPE_HISTORY = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
this.placesMenuOpenUnloaded = this.create(document, "menuitem", {
id: "placesContext_open:unloaded",
label: unloadedTabMenuL10n.openBookmark,
accesskey: unloadedTabMenuL10n.accessKey,
disabled: true,
hidden: true,
oncommand: `unloadedTabMenu.openTab(unloadedTabMenu.getActivePlacesView(this.parentElement).selectedNode)`,
});
this.placesMenuOpenNewTab.after(this.placesMenuOpenUnloaded);
this.placesMenuOpenAllUnloaded = this.create(document, "menuitem", {
id: "placesContext_openContainer:unloaded",
label: unloadedTabMenuL10n.openAll,
accesskey: unloadedTabMenuL10n.accessKey,
disabled: true,
hidden: true,
oncommand: `unloadedTabMenu.openSelectedTabs(this.parentElement)`,
});
this.placesMenuOpenContainer?.after(this.placesMenuOpenAllUnloaded);
this.placesMenuOpenAllLinksUnloaded = this.create(document, "menuitem", {
id: "placesContext_openLinks:unloaded",
label: unloadedTabMenuL10n.openAll,
accesskey: unloadedTabMenuL10n.accessKey,
disabled: true,
hidden: true,
oncommand: `unloadedTabMenu.openSelectedTabs(this.parentElement)`,
});
this.placesMenuOpenAllLinks.after(this.placesMenuOpenAllLinksUnloaded);
this.placesContextMenu.addEventListener("popupshowing", this);
if (location.href !== `chrome://browser/content/browser.xhtml`) return;
this.syncedMenuOpenAllUnloaded = this.create(document, "menuitem", {
id: "syncedTabsOpenAllUnloaded",
label: unloadedTabMenuL10n.openAll,
accesskey: unloadedTabMenuL10n.accessKey,
disabled: true,
hidden: true,
oncommand: `unloadedTabMenu.openAllSyncedFromDevice(this.parentElement)`,
});
this.syncedMenuOpenAll.after(this.syncedMenuOpenAllUnloaded);
this.syncedMenuOpenUnloaded = this.create(document, "menuitem", {
id: "syncedTabsOpenUnloaded",
label: unloadedTabMenuL10n.openSyncedTab,
accesskey: unloadedTabMenuL10n.accessKey,
disabled: true,
hidden: true,
oncommand: `unloadedTabMenu.openSyncedTabUnloaded(this.parentElement)`,
});
this.syncedMenuOpenTab.after(this.syncedMenuOpenUnloaded);
this.syncedContextMenu.addEventListener("popupshowing", this);
this.contentMenuOpenLinkUnloaded = this.create(document, "menuitem", {
id: "context-openlinkinunloadedtab",
label: unloadedTabMenuL10n.openLink,
accesskey: unloadedTabMenuL10n.accessKey,
hidden: true,
oncommand: `unloadedTabMenu.openTab({url: gContextMenu.linkURL}, {fromContent: true, linkText: gContextMenu.linkTextStr})`,
});
this.contentMenuOpenLink.after(this.contentMenuOpenLinkUnloaded);
this.contentContextMenu.addEventListener("popupshowing", this);
this.contentContextMenu.addEventListener("popuphidden", this, false);
}
create(doc, tag, props, isHTML = false) {
let el = isHTML ? doc.createElement(tag) : doc.createXULElement(tag);
for (let prop in props) {
el.setAttribute(prop, props[prop]);
}
return el;
}
handleEvent(e) {
switch (e.type) {
case "popuphidden":
if (e.originalTarget === this.contentContextMenu)
this.contentMenuOpenLinkUnloaded.hidden = true;
break;
case "popupshowing":
switch (e.target) {
case this.contentContextMenu:
gContextMenu.showItem(
"context-openlinkinunloadedtab",
gContextMenu.onSaveableLink || gContextMenu.onPlainTextLink
);
break;
case this.placesContextMenu:
this.onPlacesContextMenuShowing(e);
break;
case this.syncedContextMenu:
this.onSyncedContextMenuShowing(e);
break;
}
break;
}
}
onPlacesContextMenuShowing(_e) {
this.placesMenuOpenAllUnloaded.disabled =
this.placesMenuOpenContainer?.disabled &&
this.placesMenuOpenBookmarkContainer?.disabled &&
this.placesMenuOpenBookmarkLinks?.disabled;
this.placesMenuOpenAllUnloaded.hidden =
this.placesMenuOpenContainer?.hidden &&
this.placesMenuOpenBookmarkContainer?.hidden &&
this.placesMenuOpenBookmarkLinks?.hidden;
this.placesMenuOpenAllLinksUnloaded.disabled = this.placesMenuOpenAllLinks?.disabled;
this.placesMenuOpenAllLinksUnloaded.hidden = this.placesMenuOpenAllLinks?.hidden;
this.placesMenuOpenUnloaded.disabled = this.placesMenuOpenNewTab?.disabled;
this.placesMenuOpenUnloaded.hidden = this.placesMenuOpenNewTab?.hidden;
}
onSyncedContextMenuShowing(_e) {
this.syncedContextMenuInited = true;
this.syncedMenuOpenAllUnloaded.disabled = this.syncedMenuOpenAll?.disabled;
this.syncedMenuOpenAllUnloaded.hidden = this.syncedMenuOpenAll?.hidden;
this.syncedMenuOpenUnloaded.disabled = this.syncedMenuOpenTab?.disabled;
this.syncedMenuOpenUnloaded.hidden = this.syncedMenuOpenTab?.hidden;
}
initPref(pref, bool) {
if (!Services.prefs.prefHasUserValue(pref)) Services.prefs.setBoolPref(pref, bool);
}
get useLinkAsTabTitle() {
return Services.prefs.getBoolPref(this.useLinkPref, true);
}
get placesContextMenu() {
return (
this._placesContextMenu ||
(this._placesContextMenu = document.getElementById("placesContext"))
);
}
get contentContextMenu() {
return (
this._contentContextMenu ||
(this._contentContextMenu = document.getElementById("contentAreaContextMenu"))
);
}
get syncedContextMenu() {
return (
this._syncedContextMenu ||
(this._syncedContextMenu = document.getElementById("SyncedTabsSidebarContext"))
);
}
get syncedTabsStore() {
return sidebar.syncedTabsDeckComponent._syncedTabsListStore;
}
get selectedSyncedRow() {
return this.syncedTabsStore.data[this.syncedTabsStore._selectedRow[0]];
}
get selectedSyncedTab() {
return this.selectedSyncedRow.tabs?.[this.syncedTabsStore._selectedRow[1]];
}
get placesMenuOpenContainer() {
return (
this._placesMenuOpenContainer ||
(this._placesMenuOpenContainer = document.getElementById(
"placesContext_openContainer:tabs"
))
);
}
get placesMenuOpenBookmarkContainer() {
return (
this._placesMenuOpenBookmarkContainer ||
(this._placesMenuOpenBookmarkContainer = document.getElementById(
"placesContext_openBookmarkContainer:tabs"
))
);
}
get placesMenuOpenBookmarkLinks() {
return (
this._placesMenuOpenBookmarkLinks ||
(this._placesMenuOpenBookmarkLinks = document.getElementById(
"placesContext_openBookmarkLinks:tabs"
))
);
}
get placesMenuOpenAllLinks() {
return (
this._placesMenuOpenAllLinks ||
(this._placesMenuOpenAllLinks = document.getElementById(
"placesContext_openLinks:tabs"
))
);
}
get placesMenuOpenNewTab() {
return (
this._placesMenuOpenNewTab ||
(this._placesMenuOpenNewTab = document.getElementById("placesContext_open:newtab"))
);
}
get syncedMenuOpenAll() {
return (
this._syncedMenuOpenAll ||
(this._syncedMenuOpenAll = this.syncedContextMenu.querySelector(
"#syncedTabsOpenAllInTabs"
))
);
}
get syncedMenuOpenTab() {
return (
this._syncedMenuOpenTab ||
(this._syncedMenuOpenTab = this.syncedContextMenu.querySelector(
"#syncedTabsOpenSelectedInTab"
))
);
}
get contentMenuOpenLink() {
return (
this._contentMenuOpenLink ||
(this._contentMenuOpenLink = document.getElementById("context-openlinkintab"))
);
}
getActivePlacesView(popup) {
if (!popup.triggerNode) return false;
return PlacesUIUtils.getViewForNode(popup.triggerNode);
}
openSelectedTabs(popup) {
let view = this.getActivePlacesView(popup);
let folder = view.selectedNode || view.selectedNodes || view.result.root;
let items = [];
if (PlacesUtils.nodeIsContainer(folder)) {
let root = PlacesUtils.getContainerNodeWithOptions(folder, false, true);
for (let i = 0; i < root.childCount; ++i) {
let child = root.getChild(i);
if (PlacesUtils.nodeIsURI(child)) {
items.push({
url: child.uri,
title: child.title,
icon: child.icon,
});
}
}
} else items = folder;
items.forEach((item) => this.openTab(item, { bulkOpen: true }));
}
openSyncedTabUnloaded(popup) {
if (!this.syncedContextMenuInited) return;
if (popup.triggerNode?.closest(".tabs-container"))
this.openTab(this.selectedSyncedTab, { syncedTabs: true });
}
openAllSyncedFromDevice(popup) {
if (!this.syncedContextMenuInited) return;
if (popup.triggerNode?.closest(".tabs-container"))
this.selectedSyncedRow.tabs.forEach((item) =>
this.openTab(item, { bulkOpen: true, syncedTabs: true })
);
}
async openTab(item, params = {}) {
let url = typeof item === "object" ? item.url || item.uri : item;
let win = window.gBrowser ? window : BrowserWindowTracker.getTopWindow();
let { gBrowser } = win;
let tabParams = {};
if (params.fromContent && gContextMenu)
tabParams = gContextMenu._openLinkInParameters({
userContextId: gBrowser.selectedTab.userContextId,
});
else {
if (params.bulkOpen)
tabParams = {
skipAnimation: true,
bulkOrderedOpen: true,
};
tabParams.triggeringPrincipal =
location.href === `chrome://browser/content/browser.xhtml` && !params.syncedTabs
? gBrowser.selectedBrowser.contentPrincipal
: Services.scriptSecurityManager.getSystemPrincipal();
}
tabParams.noInitialLabel = true;
let tab = gBrowser.addTab(null, tabParams);
let uri = Services.io.newURI(url);
let info =
this.getInfoFromHistory(uri, this.QUERY_TYPE_HISTORY) ||
this.getInfoFromHistory(uri, this.QUERY_TYPE_BOOKMARKS);
let tentativeIcon = item?.icon || info?.icon;
win.SessionStore.setTabState(tab, {
entries: [
{
url: url,
title:
item?.title ||
info?.title ||
(this.useLinkAsTabTitle && params.linkText),
triggeringPrincipal_base64: this.E10SUtils.serializePrincipal(
tabParams.triggeringPrincipal
),
},
],
lastAccessed: tab.lastAccessed,
});
let iconURL;
let isReady = false;
tab.addEventListener(
"SSTabRestoring",
function () {
isReady = true;
win.unloadedTabMenu.maybeSetIcon(
tab,
iconURL,
isReady,
tabParams.triggeringPrincipal
);
},
{ once: true }
);
let tempURL =
(await PlacesUtils.promiseFaviconData(uri.spec, 256).then(
(data) => data?.uri?.spec
)) || tentativeIcon;
if (tempURL) {
let blob = await fetch(tempURL)
.then((r) => r.blob())
.catch((e) => {
if (
params.fromContent &&
gContextMenu.linkURI.host === gContextMenu.contentData.principal.host
) {
iconURL = gBrowser.getTabForBrowser(gContextMenu.browser).image;
win.unloadedTabMenu.maybeSetIcon(
tab,
iconURL,
isReady,
tabParams.triggeringPrincipal
);
return;
}
});
let reader = new FileReader();
reader.onloadend = function () {
iconURL = reader.result;
win.unloadedTabMenu.maybeSetIcon(
tab,
iconURL,
isReady,
tabParams.triggeringPrincipal
);
};
reader.readAsDataURL(blob);
}
}
maybeSetIcon(tab, iconURL, isReady, principal) {
if (iconURL && isReady) tab.ownerGlobal.gBrowser.setIcon(tab, iconURL, null, principal);
}
getInfoFromHistory(aURI, aQueryType) {
let options = PlacesUtils.history.getNewQueryOptions();
options.queryType = aQueryType;
options.maxResults = 1;
let query = PlacesUtils.history.getNewQuery();
query.uri = aURI;
let root = PlacesUtils.history.executeQuery(query, options).root;
root.containerOpen = true;
if (!root.childCount) {
root.containerOpen = false;
return null;
}
let child = root.getChild(0);
root.containerOpen = false;
return {
title: child.title,
icon: child.icon,
};
}
}
function init() {
window.unloadedTabMenu = new UnloadedTabMenuBase();
}
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");
}
})();