Repository navigation
Expand file tree
/
Copy pathopenBookmarkInContainerTab.uc.js
More file actions
335 lines (325 loc) · 16.4 KB
/
Copy pathopenBookmarkInContainerTab.uc.js
File metadata and controls
335 lines (325 loc) · 16.4 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
// ==UserScript==
// @name Open Bookmark in Container Tab (context menu)
// @version 1.1
// @author aminomancer
// @homepage https://github.com/aminomancer/uc.css.js
// @description Adds a new menu to context menus prompted by right-clicking bookmarks, history entries, etc. that allows you to open them in a container tab. This does basically the same thing as the similarly-named addon by Rob Wu, just by a different method. By doing this with an autoconfig script, we can make the menu appear in a logical order towards the top of the context menu rather than at the very bottom, where context menu items from addons always go.
// @include main
// @include chrome://browser/content/places/bookmarksSidebar.xhtml
// @include chrome://browser/content/places/historySidebar.xhtml
// @include chrome://browser/content/places/places.xhtml
// ==/UserScript==
class OpenPlacesInContainerTabMenu {
static config = {
l10n: {
openAll: `Open All in Container Tabs`, // Appears when right-clicking a container/folder in bookmarks or history.
openBookmark: `Open in New Container Tab`, // Appears when right-clicking a bookmark, history item, etc.
openSyncedTab: `Open in a New Container Tab`, // Appears when right-clicking a tab in the synced tabs sidebar.
accessKey: `c`, // 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 "c" for the english "Container." This collides with the "Copy" menu item, so if you leave this set to "c" the script will change the Copy access key to "y" so they don't collide. It won't do that if you change this variable though. 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 Container Tabs (G)"
},
};
constructor() {
let { l10n } = OpenPlacesInContainerTabMenu.config;
let popups = [];
this.loadSheet();
if (l10n.accessKey === `c`) this.placesMenuCopy.accessKey = `y`;
this.placesMenuOpenInContainer = this.create(document, "menu", {
id: "placesContext_open:container",
label: l10n.openBookmark,
accesskey: l10n.accessKey,
disabled: true,
hidden: true,
});
this.placesMenuOpenNewTab.after(this.placesMenuOpenInContainer);
let openPopup = this.placesMenuOpenInContainer.appendChild(
document.createXULElement("menupopup")
);
openPopup.addEventListener("command", (e) => this.openLinkInContainer(e, openPopup));
popups.push(openPopup);
this.placesMenuOpenAllInContainer = this.create(document, "menu", {
id: "placesContext_openContainer:container",
label: l10n.openAll,
accesskey: l10n.accessKey,
disabled: true,
hidden: true,
});
this.placesMenuOpenContainer?.after(this.placesMenuOpenAllInContainer);
let openAllPopup = this.placesMenuOpenAllInContainer.appendChild(
document.createXULElement("menupopup")
);
openAllPopup.addEventListener("command", (e) =>
this.openSelectedInContainer(e, openAllPopup)
);
popups.push(openAllPopup);
this.placesMenuOpenAllLinksInContainer = this.create(document, "menu", {
id: "placesContext_openLinks:container",
label: l10n.openAll,
accesskey: l10n.accessKey,
disabled: true,
hidden: true,
});
this.placesMenuOpenAllLinks.after(this.placesMenuOpenAllLinksInContainer);
let openAllLinksPopup = this.placesMenuOpenAllLinksInContainer.appendChild(
document.createXULElement("menupopup")
);
openAllLinksPopup.addEventListener("command", (e) =>
this.openSelectedInContainer(e, openAllLinksPopup)
);
popups.push(openAllLinksPopup);
this.placesContextMenu.addEventListener("popupshowing", this);
if (location.href === `chrome://browser/content/browser.xhtml`) {
if (l10n.accessKey === `c`) this.syncedMenuCopy.accessKey = `y`;
this.syncedMenuOpenAllInContainer = this.create(document, "menu", {
id: "syncedTabsOpenAllInContainer",
label: l10n.openAll,
accesskey: l10n.accessKey,
disabled: true,
hidden: true,
});
this.syncedMenuOpenAll.after(this.syncedMenuOpenAllInContainer);
let syncedOpenAllPopup = this.syncedMenuOpenAllInContainer.appendChild(
document.createXULElement("menupopup")
);
syncedOpenAllPopup.addEventListener("command", (e) =>
this.openAllSyncedFromDevice(e, syncedOpenAllPopup)
);
popups.push(syncedOpenAllPopup);
this.syncedMenuOpenInContainer = this.create(document, "menu", {
id: "syncedTabsOpenInContainer",
label: l10n.openSyncedTab,
accesskey: l10n.accessKey,
disabled: true,
hidden: true,
});
this.syncedMenuOpenTab.after(this.syncedMenuOpenInContainer);
let syncedOpenPopup = this.syncedMenuOpenInContainer.appendChild(
document.createXULElement("menupopup")
);
syncedOpenPopup.addEventListener("command", (e) =>
this.openSyncedTab(e, syncedOpenPopup)
);
popups.push(syncedOpenPopup);
this.syncedContextMenu.addEventListener("popupshowing", this);
this.syncedFilterContextMenu.addEventListener("popupshowing", this, { once: true });
}
popups.forEach((popup) =>
popup.addEventListener("popupshowing", (e) =>
createUserContextMenu(e, {
isContextMenu: true,
showDefaultTab: true,
})
)
);
}
get placesContextMenu() {
return (
this._placesContextMenu ||
(this._placesContextMenu = document.getElementById("placesContext"))
);
}
get syncedContextMenu() {
return (
this._syncedContextMenu ||
(this._syncedContextMenu = document.getElementById("SyncedTabsSidebarContext"))
);
}
get syncedFilterContextMenu() {
return (
this._syncedFilterContextMenu ||
(this._syncedFilterContextMenu = document.getElementById(
"SyncedTabsSidebarTabsFilterContext"
))
);
}
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 placesMenuCopy() {
return (
this._placesMenuCopy ||
(this._placesMenuCopy = document.getElementById("placesContext_copy"))
);
}
get syncedMenuOpenAll() {
return (
this._syncedMenuOpenAll ||
(this._syncedMenuOpenAll = this.syncedContextMenu.querySelector(
"#syncedTabsOpenAllInTabs"
))
);
}
get syncedMenuOpenTab() {
return (
this._syncedMenuOpenTab ||
(this._syncedMenuOpenTab = this.syncedContextMenu.querySelector(
"#syncedTabsOpenSelectedInTab"
))
);
}
get syncedMenuCopy() {
return (
this._syncedMenuCopy ||
(this._syncedMenuCopy = this.syncedContextMenu.querySelector("#syncedTabsCopySelected"))
);
}
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;
}
getActivePlacesView(popup) {
if (!popup.triggerNode) return false;
return PlacesUIUtils.getViewForNode(popup.triggerNode);
}
handleEvent(e) {
switch (e.target) {
case this.placesContextMenu:
this.onPlacesContextMenuShowing(e);
break;
case this.syncedContextMenu:
this.onSyncedContextMenuShowing(e);
break;
case this.syncedFilterContextMenu:
// fix a bug
MozXULElement.insertFTLIfNeeded("browser/syncedTabs.ftl");
e.target.querySelectorAll("[data-lazy-l10n-id]").forEach((el) => {
el.setAttribute("data-l10n-id", el.getAttribute("data-lazy-l10n-id"));
el.removeAttribute("data-lazy-l10n-id");
});
break;
}
}
onPlacesContextMenuShowing(_e) {
this.placesMenuOpenAllInContainer.disabled =
this.placesMenuOpenContainer?.disabled &&
this.placesMenuOpenBookmarkContainer?.disabled &&
this.placesMenuOpenBookmarkLinks?.disabled;
this.placesMenuOpenAllInContainer.hidden =
this.placesMenuOpenContainer?.hidden &&
this.placesMenuOpenBookmarkContainer?.hidden &&
this.placesMenuOpenBookmarkLinks?.hidden;
this.placesMenuOpenAllLinksInContainer.disabled = this.placesMenuOpenAllLinks?.disabled;
this.placesMenuOpenAllLinksInContainer.hidden = this.placesMenuOpenAllLinks?.hidden;
this.placesMenuOpenInContainer.disabled = this.placesMenuOpenNewTab?.disabled;
this.placesMenuOpenInContainer.hidden = this.placesMenuOpenNewTab?.hidden;
}
onSyncedContextMenuShowing(_e) {
this.syncedContextMenuInited = true;
this.syncedMenuOpenAllInContainer.disabled = this.syncedMenuOpenAll?.disabled;
this.syncedMenuOpenAllInContainer.hidden = this.syncedMenuOpenAll?.hidden;
this.syncedMenuOpenInContainer.disabled = this.syncedMenuOpenTab?.disabled;
this.syncedMenuOpenInContainer.hidden = this.syncedMenuOpenTab?.hidden;
}
openLinkInContainer(e, popup, item) {
let win = window.gBrowser ? window : BrowserWindowTracker.getTopWindow();
if (!win) return;
let { gBrowser, Services } = win;
let urls = [];
if (!item) {
let view = this.getActivePlacesView(popup);
if (!view) return;
item = view.selectedNode;
}
if (item instanceof Array)
item.forEach((node) => {
let url = typeof node === "object" ? node.url || node.uri : node;
urls.push(url);
});
else urls = [typeof item === "object" ? item.url || item.uri : item];
gBrowser.loadTabs(urls, {
userContextId: parseInt(e.target.getAttribute("data-usercontextid")),
inBackground: Services.prefs.getBoolPref("browser.tabs.loadBookmarksInBackground"),
replace: false,
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});
}
openSelectedInContainer(e, popup) {
let view = this.getActivePlacesView(popup);
if (!view) return;
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 });
}
} else items = folder;
this.openLinkInContainer(e, popup, items);
}
openSyncedTab(e, popup) {
if (!this.syncedContextMenuInited) return;
if (popup.triggerNode?.closest(".tabs-container"))
this.openLinkInContainer(e, popup, this.selectedSyncedTab);
}
openAllSyncedFromDevice(e, popup) {
if (!this.syncedContextMenuInited) return;
if (popup.triggerNode?.closest(".tabs-container"))
this.openLinkInContainer(e, popup, this.selectedSyncedRow.tabs);
}
loadSheet() {
const css = `.identity-color-blue{--identity-tab-color:#37adff;--identity-icon-color:#37adff;}.identity-color-turquoise{--identity-tab-color:#00c79a;--identity-icon-color:#00c79a;}.identity-color-green{--identity-tab-color:#51cd00;--identity-icon-color:#51cd00;}.identity-color-yellow{--identity-tab-color:#ffcb00;--identity-icon-color:#ffcb00;}.identity-color-orange{--identity-tab-color:#ff9f00;--identity-icon-color:#ff9f00;}.identity-color-red{--identity-tab-color:#ff613d;--identity-icon-color:#ff613d;}.identity-color-pink{--identity-tab-color:#ff4bda;--identity-icon-color:#ff4bda;}.identity-color-purple{--identity-tab-color:#af51f5;--identity-icon-color:#af51f5;}.identity-color-toolbar{--identity-tab-color:var(--lwt-toolbar-field-color,FieldText);--identity-icon-color:var(--lwt-toolbar-field-color,FieldText);}.identity-icon-fence{--identity-icon:url("resource://usercontext-content/fence.svg");}.identity-icon-fingerprint{--identity-icon:url("resource://usercontext-content/fingerprint.svg");}.identity-icon-briefcase{--identity-icon:url("resource://usercontext-content/briefcase.svg");}.identity-icon-dollar{--identity-icon:url("resource://usercontext-content/dollar.svg");}.identity-icon-cart{--identity-icon:url("resource://usercontext-content/cart.svg");}.identity-icon-circle{--identity-icon:url("resource://usercontext-content/circle.svg");}.identity-icon-vacation{--identity-icon:url("resource://usercontext-content/vacation.svg");}.identity-icon-gift{--identity-icon:url("resource://usercontext-content/gift.svg");}.identity-icon-food{--identity-icon:url("resource://usercontext-content/food.svg");}.identity-icon-fruit{--identity-icon:url("resource://usercontext-content/fruit.svg");}.identity-icon-pet{--identity-icon:url("resource://usercontext-content/pet.svg");}.identity-icon-tree{--identity-icon:url("resource://usercontext-content/tree.svg");}.identity-icon-chill{--identity-icon:url("resource://usercontext-content/chill.svg");}.menuitem-iconic[data-usercontextid]{list-style-image:var(--identity-icon);-moz-context-properties:fill;fill:var(--identity-icon-color);}`;
let sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(
Ci.nsIStyleSheetService
);
let uri = Services.io.newURI("data:text/css;charset=UTF=8," + encodeURIComponent(css));
if (sss.sheetRegistered(uri, sss.AUTHOR_SHEET)) return;
sss.loadAndRegisterSheet(uri, sss.AUTHOR_SHEET);
}
}
if (
location.href !== `chrome://browser/content/browser.xhtml` ||
gBrowserInit.delayedStartupFinished
)
new OpenPlacesInContainerTabMenu();
else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
new OpenPlacesInContainerTabMenu();
}
};
Services.obs.addObserver(delayedListener, "browser-delayed-startup-finished");
}