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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/**
* This module inject dynamically menu items into browser UI.
*
* Menu definitions are fetched from:
* - devtools/client/menus for top level entires
* - devtools/client/definitions for tool-specifics entries
*/
const { LocalizationHelper } = require("resource://devtools/shared/l10n.js");
const MENUS_L10N = new LocalizationHelper(
"devtools/client/locales/menus.properties"
);
loader.lazyRequireGetter(
this,
"gDevTools",
"resource://devtools/client/framework/devtools.js",
true
);
loader.lazyRequireGetter(
this,
"gDevToolsBrowser",
"resource://devtools/client/framework/devtools-browser.js",
true
);
loader.lazyRequireGetter(
this,
"Telemetry",
"resource://devtools/client/shared/telemetry.js"
);
let telemetry = null;
// Keep list of inserted DOM Elements in order to remove them on unload
// Maps browser xul document => list of DOM Elements
const FragmentsCache = new Map();
function l10n(key) {
return MENUS_L10N.getStr(key);
}
/**
* Create a xul:menuitem element
*
* @param {HTMLDocument} doc
* The document to which menus are to be added.
* @param {String} id
* Element id.
* @param {String} label
* Menu label.
* @param {String} accesskey (optional)
* Access key of the menuitem, used as shortcut while opening the menu.
* @param {Boolean} isCheckbox (optional)
* If true, the menuitem will act as a checkbox and have an optional
* tick on its left.
* @param {String} appMenuL10nId (optional)
* A Fluent key to set the appmenu-data-l10n-id attribute of the menuitem
* to. This can then be used to show a different string when cloning the
* menuitem to show in the AppMenu or panel contexts.
*
* @return XULMenuItemElement
*/
function createMenuItem({
doc,
id,
label,
accesskey,
isCheckbox,
appMenuL10nId,
}) {
const menuitem = doc.createXULElement("menuitem");
menuitem.id = id;
menuitem.setAttribute("label", label);
if (accesskey) {
menuitem.setAttribute("accesskey", accesskey);
}
if (isCheckbox) {
menuitem.setAttribute("type", "checkbox");
menuitem.setAttribute("autocheck", "false");
}
if (appMenuL10nId) {
menuitem.setAttribute("appmenu-data-l10n-id", appMenuL10nId);
}
return menuitem;
}
/**
* Add a menu entry for a tool definition
*
* @param {Object} toolDefinition
* Tool definition of the tool to add a menu entry.
* @param {HTMLDocument} doc
* The document to which the tool menu item is to be added.
*/
function createToolMenuElements(toolDefinition, doc) {
const id = toolDefinition.id;
const menuId = "menuitem_" + id;
// Prevent multiple entries for the same tool.
if (doc.getElementById(menuId)) {
return null;
}
const oncommand = async function (id, event) {
try {
const window = event.target.ownerDocument.defaultView;
await gDevToolsBrowser.selectToolCommand(window, id, Cu.now());
sendEntryPointTelemetry(window);
} catch (e) {
console.error(`Exception while opening ${id}: ${e}\n${e.stack}`);
}
}.bind(null, id);
const menuitem = createMenuItem({
doc,
id: "menuitem_" + id,
label: toolDefinition.menuLabel || toolDefinition.label,
accesskey: toolDefinition.accesskey,
appMenuL10nId: toolDefinition.appMenuL10nId,
});
// Refer to the key in order to display the key shortcut at menu ends
// This <key> element is being created by devtools/client/devtools-startup.js
menuitem.setAttribute("key", "key_" + id);
menuitem.addEventListener("command", oncommand);
return menuitem;
}
/**
* Send entry point telemetry explaining how the devtools were launched when
* launched from the System Menu.. This functionality also lives inside
* `devtools/startup/devtools-startup.js` but that codepath is only used the
* first time a toolbox is opened for a tab.
*/
function sendEntryPointTelemetry(window) {
if (!telemetry) {
telemetry = new Telemetry();
}
telemetry.addEventProperty(window, "open", "tools", null, "shortcut", "");
telemetry.addEventProperty(
window,
"open",
"tools",
null,
"entrypoint",
"SystemMenu"
);
}
/**
* Create xul menuitem, key elements for a given tool.
* And then insert them into browser DOM.
*
* @param {HTMLDocument} doc
* The document to which the tool is to be registered.
* @param {Object} toolDefinition
* Tool definition of the tool to register.
* @param {Object} prevDef
* The tool definition after which the tool menu item is to be added.
*/
function insertToolMenuElements(doc, toolDefinition, prevDef) {
const menuitem = createToolMenuElements(toolDefinition, doc);
if (!menuitem) {
return;
}
let ref;
if (prevDef) {
const menuitem = doc.getElementById("menuitem_" + prevDef.id);
ref = menuitem?.nextSibling ? menuitem.nextSibling : null;
} else {
ref = doc.getElementById("menu_devtools_remotedebugging");
}
if (ref) {
ref.parentNode.insertBefore(menuitem, ref);
}
}
exports.insertToolMenuElements = insertToolMenuElements;
/**
* Remove a tool's menuitem from a window
*
* @param {string} toolId
* Id of the tool to add a menu entry for
* @param {HTMLDocument} doc
* The document to which the tool menu item is to be removed from
*/
function removeToolFromMenu(toolId, doc) {
const key = doc.getElementById("key_" + toolId);
if (key) {
key.remove();
}
const menuitem = doc.getElementById("menuitem_" + toolId);
if (menuitem) {
menuitem.remove();
}
}
exports.removeToolFromMenu = removeToolFromMenu;
/**
* Add all tools to the developer tools menu of a window.
*
* @param {HTMLDocument} doc
* The document to which the tool items are to be added.
*/
function addAllToolsToMenu(doc) {
const fragMenuItems = doc.createDocumentFragment();
for (const toolDefinition of gDevTools.getToolDefinitionArray()) {
if (!toolDefinition.inMenu) {
continue;
}
const menuItem = createToolMenuElements(toolDefinition, doc);
if (!menuItem) {
continue;
}
fragMenuItems.appendChild(menuItem);
}
const mps = doc.getElementById("menu_devtools_remotedebugging");
if (mps) {
mps.parentNode.insertBefore(fragMenuItems, mps);
}
}
/**
* Add global menus that are not panel specific.
*
* @param {HTMLDocument} doc
* The document to which menus are to be added.
*/
function addTopLevelItems(doc) {
const menuItems = doc.createDocumentFragment();
const { menuitems } = require("resource://devtools/client/menus.js");
for (const item of menuitems) {
if (item.separator) {
const separator = doc.createXULElement("menuseparator");
separator.id = item.id;
menuItems.appendChild(separator);
} else {
const { id, l10nKey } = item;
// Create a <menuitem>
const menuitem = createMenuItem({
doc,
id,
label: l10n(l10nKey + ".label"),
accesskey: l10n(l10nKey + ".accesskey"),
isCheckbox: item.checkbox,
appMenuL10nId: item.appMenuL10nId,
});
menuitem.addEventListener("command", item.oncommand);
menuItems.appendChild(menuitem);
if (item.keyId) {
menuitem.setAttribute("key", "key_" + item.keyId);
}
}
}
// Cache all nodes before insertion to be able to remove them on unload
const nodes = [];
for (const node of menuItems.children) {
nodes.push(node);
}
FragmentsCache.set(doc, nodes);
const menu = doc.getElementById("menuWebDeveloperPopup");
menu.appendChild(menuItems);
// There is still "Page Source" and "Task Manager" menuitems hardcoded
// into browser.xhtml. Instead of manually inserting everything around it,
// move them to the expected position.
const pageSourceMenu = doc.getElementById("menu_pageSource");
const extensionsForDevelopersMenu = doc.getElementById(
"extensionsForDevelopers"
);
menu.insertBefore(pageSourceMenu, extensionsForDevelopersMenu);
const taskManagerMenu = doc.getElementById("menu_taskManager");
const remoteDebuggingMenu = doc.getElementById(
"menu_devtools_remotedebugging"
);
menu.insertBefore(taskManagerMenu, remoteDebuggingMenu);
}
/**
* Remove global menus that are not panel specific.
*
* @param {HTMLDocument} doc
* The document to which menus are to be added.
*/
function removeTopLevelItems(doc) {
const nodes = FragmentsCache.get(doc);
if (!nodes) {
return;
}
FragmentsCache.delete(doc);
for (const node of nodes) {
node.remove();
}
}
/**
* Add menus to a browser document
*
* @param {HTMLDocument} doc
* The document to which menus are to be added.
*/
exports.addMenus = function (doc) {
addTopLevelItems(doc);
addAllToolsToMenu(doc);
};
/**
* Remove menus from a browser document
*
* @param {HTMLDocument} doc
* The document to which menus are to be removed.
*/
exports.removeMenus = function (doc) {
// We only remove top level entries. Per-tool entries are removed while
// unregistering each tool.
removeTopLevelItems(doc);
};
|