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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* 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/. */
// Services = object with smart getters for common XPCOM services
var { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
var { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
ChromeUtils.defineESModuleGetters(this, {
BrowserUtils: "resource://gre/modules/BrowserUtils.sys.mjs",
ContextualIdentityService:
"resource://gre/modules/ContextualIdentityService.sys.mjs",
ExtensionSettingsStore:
"resource://gre/modules/ExtensionSettingsStore.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
ShellService: "resource:///modules/ShellService.sys.mjs",
URILoadingHelper: "resource:///modules/URILoadingHelper.sys.mjs",
});
XPCOMUtils.defineLazyModuleGetters(this, {
AboutNewTab: "resource:///modules/AboutNewTab.jsm",
BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
});
XPCOMUtils.defineLazyGetter(this, "ReferrerInfo", () =>
Components.Constructor(
"@mozilla.org/referrer-info;1",
"nsIReferrerInfo",
"init"
)
);
Object.defineProperty(this, "BROWSER_NEW_TAB_URL", {
enumerable: true,
get() {
if (PrivateBrowsingUtils.isWindowPrivate(window)) {
if (
!PrivateBrowsingUtils.permanentPrivateBrowsing &&
!AboutNewTab.newTabURLOverridden
) {
return "about:privatebrowsing";
}
// If an extension controls the setting and does not have private
// browsing permission, use the default setting.
let extensionControlled = Services.prefs.getBoolPref(
"browser.newtab.extensionControlled",
false
);
let privateAllowed = Services.prefs.getBoolPref(
"browser.newtab.privateAllowed",
false
);
// There is a potential on upgrade that the prefs are not set yet, so we double check
// for moz-extension.
if (
!privateAllowed &&
(extensionControlled ||
AboutNewTab.newTabURL.startsWith("moz-extension://"))
) {
return "about:privatebrowsing";
}
}
return AboutNewTab.newTabURL;
},
});
var TAB_DROP_TYPE = "application/x-moz-tabbrowser-tab";
var gBidiUI = false;
/**
* Determines whether the given url is considered a special URL for new tabs.
*/
function isBlankPageURL(aURL) {
return (
aURL == "about:blank" ||
aURL == "about:home" ||
aURL == BROWSER_NEW_TAB_URL ||
aURL == "chrome://browser/content/blanktab.html"
);
}
function doGetProtocolFlags(aURI) {
return Services.io.getDynamicProtocolFlags(aURI);
}
function openUILink(
url,
event,
aIgnoreButton,
aIgnoreAlt,
aAllowThirdPartyFixup,
aPostData,
aReferrerInfo
) {
return URILoadingHelper.openUILink(
window,
url,
event,
aIgnoreButton,
aIgnoreAlt,
aAllowThirdPartyFixup,
aPostData,
aReferrerInfo
);
}
// This is here for historical reasons. bug 1742889 covers cleaning this up.
function getRootEvent(aEvent) {
return BrowserUtils.getRootEvent(aEvent);
}
// This is here for historical reasons. bug 1742889 covers cleaning this up.
function whereToOpenLink(e, ignoreButton, ignoreAlt) {
return BrowserUtils.whereToOpenLink(e, ignoreButton, ignoreAlt);
}
function openTrustedLinkIn(url, where, params) {
URILoadingHelper.openTrustedLinkIn(window, url, where, params);
}
function openWebLinkIn(url, where, params) {
URILoadingHelper.openWebLinkIn(window, url, where, params);
}
function openLinkIn(url, where, params) {
return URILoadingHelper.openLinkIn(window, url, where, params);
}
// Used as an onclick handler for UI elements with link-like behavior.
// e.g. onclick="checkForMiddleClick(this, event);"
// Not needed for menuitems because those fire command events even on middle clicks.
function checkForMiddleClick(node, event) {
// We should be using the disabled property here instead of the attribute,
// but some elements that this function is used with don't support it (e.g.
// menuitem).
if (node.getAttribute("disabled") == "true") {
return;
} // Do nothing
if (event.target.tagName == "menuitem") {
// Menu items fire command on middle-click by themselves.
return;
}
if (event.button == 1) {
/* Execute the node's oncommand or command.
*/
let cmdEvent = document.createEvent("xulcommandevent");
cmdEvent.initCommandEvent(
"command",
true,
true,
window,
0,
event.ctrlKey,
event.altKey,
event.shiftKey,
event.metaKey,
0,
event,
event.mozInputSource
);
node.dispatchEvent(cmdEvent);
// Stop the propagation of the click event, to prevent the event from being
// handled more than once.
// E.g. see https://bugzilla.mozilla.org/show_bug.cgi?id=1657992#c4
event.stopPropagation();
event.preventDefault();
// If the middle-click was on part of a menu, close the menu.
// (Menus close automatically with left-click but not with middle-click.)
closeMenus(event.target);
}
}
// Populate a menu with user-context menu items. This method should be called
// by onpopupshowing passing the event as first argument.
function createUserContextMenu(
event,
{
isContextMenu = false,
excludeUserContextId = 0,
showDefaultTab = false,
useAccessKeys = true,
} = {}
) {
while (event.target.hasChildNodes()) {
event.target.firstChild.remove();
}
let bundle = Services.strings.createBundle(
"chrome://browser/locale/browser.properties"
);
let docfrag = document.createDocumentFragment();
// If we are excluding a userContextId, we want to add a 'no-container' item.
if (excludeUserContextId || showDefaultTab) {
let menuitem = document.createXULElement("menuitem");
menuitem.setAttribute("data-usercontextid", "0");
menuitem.setAttribute(
"label",
bundle.GetStringFromName("userContextNone.label")
);
menuitem.setAttribute(
"accesskey",
bundle.GetStringFromName("userContextNone.accesskey")
);
if (!isContextMenu) {
menuitem.setAttribute("command", "Browser:NewUserContextTab");
}
docfrag.appendChild(menuitem);
let menuseparator = document.createXULElement("menuseparator");
docfrag.appendChild(menuseparator);
}
ContextualIdentityService.getPublicIdentities().forEach(identity => {
if (identity.userContextId == excludeUserContextId) {
return;
}
let menuitem = document.createXULElement("menuitem");
menuitem.setAttribute("data-usercontextid", identity.userContextId);
menuitem.setAttribute(
"label",
ContextualIdentityService.getUserContextLabel(identity.userContextId)
);
if (identity.accessKey && useAccessKeys) {
menuitem.setAttribute(
"accesskey",
bundle.GetStringFromName(identity.accessKey)
);
}
menuitem.classList.add("menuitem-iconic");
menuitem.classList.add("identity-color-" + identity.color);
if (!isContextMenu) {
menuitem.setAttribute("command", "Browser:NewUserContextTab");
}
menuitem.classList.add("identity-icon-" + identity.icon);
docfrag.appendChild(menuitem);
});
if (!isContextMenu) {
docfrag.appendChild(document.createXULElement("menuseparator"));
let menuitem = document.createXULElement("menuitem");
menuitem.setAttribute(
"label",
bundle.GetStringFromName("userContext.aboutPage.label")
);
if (useAccessKeys) {
menuitem.setAttribute(
"accesskey",
bundle.GetStringFromName("userContext.aboutPage.accesskey")
);
}
menuitem.setAttribute("command", "Browser:OpenAboutContainers");
docfrag.appendChild(menuitem);
}
event.target.appendChild(docfrag);
return true;
}
// Closes all popups that are ancestors of the node.
function closeMenus(node) {
if ("tagName" in node) {
if (
node.namespaceURI ==
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" &&
(node.tagName == "menupopup" || node.tagName == "popup")
) {
node.hidePopup();
}
closeMenus(node.parentNode);
}
}
/** This function takes in a key element and compares it to the keys pressed during an event.
*
* @param aEvent
* The KeyboardEvent event you want to compare against your key.
*
* @param aKey
* The <key> element checked to see if it was called in aEvent.
* For example, aKey can be a variable set to document.getElementById("key_close")
* to check if the close command key was pressed in aEvent.
*/
function eventMatchesKey(aEvent, aKey) {
let keyPressed = aKey.getAttribute("key").toLowerCase();
let keyModifiers = aKey.getAttribute("modifiers");
let modifiers = ["Alt", "Control", "Meta", "Shift"];
if (aEvent.key != keyPressed) {
return false;
}
let eventModifiers = modifiers.filter(modifier =>
aEvent.getModifierState(modifier)
);
// Check if aEvent has a modifier and aKey doesn't
if (eventModifiers.length && !keyModifiers.length) {
return false;
}
// Check whether aKey's modifiers match aEvent's modifiers
if (keyModifiers) {
keyModifiers = keyModifiers.split(/[\s,]+/);
// Capitalize first letter of aKey's modifers to compare to aEvent's modifier
keyModifiers.forEach(function (modifier, index) {
if (modifier == "accel") {
keyModifiers[index] =
AppConstants.platform == "macosx" ? "Meta" : "Control";
} else {
keyModifiers[index] = modifier[0].toUpperCase() + modifier.slice(1);
}
});
return modifiers.every(
modifier =>
keyModifiers.includes(modifier) == aEvent.getModifierState(modifier)
);
}
return true;
}
// Gather all descendent text under given document node.
function gatherTextUnder(root) {
var text = "";
var node = root.firstChild;
var depth = 1;
while (node && depth > 0) {
// See if this node is text.
if (node.nodeType == Node.TEXT_NODE) {
// Add this text to our collection.
text += " " + node.data;
} else if (HTMLImageElement.isInstance(node)) {
// If it has an "alt" attribute, add that.
var altText = node.getAttribute("alt");
if (altText && altText != "") {
text += " " + altText;
}
}
// Find next node to test.
// First, see if this node has children.
if (node.hasChildNodes()) {
// Go to first child.
node = node.firstChild;
depth++;
} else {
// No children, try next sibling (or parent next sibling).
while (depth > 0 && !node.nextSibling) {
node = node.parentNode;
depth--;
}
if (node.nextSibling) {
node = node.nextSibling;
}
}
}
// Strip leading and tailing whitespace.
text = text.trim();
// Compress remaining whitespace.
text = text.replace(/\s+/g, " ");
return text;
}
// This function exists for legacy reasons.
function getShellService() {
return ShellService;
}
function isBidiEnabled() {
// first check the pref.
if (Services.prefs.getBoolPref("bidi.browser.ui", false)) {
return true;
}
// now see if the app locale is an RTL one.
const isRTL = Services.locale.isAppLocaleRTL;
if (isRTL) {
Services.prefs.setBoolPref("bidi.browser.ui", true);
}
return isRTL;
}
function openAboutDialog() {
for (let win of Services.wm.getEnumerator("Browser:About")) {
// Only open one about window (Bug 599573)
if (win.closed) {
continue;
}
win.focus();
return;
}
var features = "chrome,";
if (AppConstants.platform == "win") {
features += "centerscreen,dependent";
} else if (AppConstants.platform == "macosx") {
features += "centerscreen,resizable=no,minimizable=no";
} else {
features += "centerscreen,dependent,dialog=no";
}
window.openDialog("chrome://browser/content/aboutDialog.xhtml", "", features);
}
async function openPreferences(paneID, extraArgs) {
// This function is duplicated from preferences.js.
function internalPrefCategoryNameToFriendlyName(aName) {
return (aName || "").replace(/^pane./, function (toReplace) {
return toReplace[4].toLowerCase();
});
}
let win = Services.wm.getMostRecentWindow("navigator:browser");
let friendlyCategoryName = internalPrefCategoryNameToFriendlyName(paneID);
let params;
if (extraArgs && extraArgs.urlParams) {
params = new URLSearchParams();
let urlParams = extraArgs.urlParams;
for (let name in urlParams) {
if (urlParams[name] !== undefined) {
params.set(name, urlParams[name]);
}
}
}
let preferencesURL =
"about:preferences" +
(params ? "?" + params : "") +
(friendlyCategoryName ? "#" + friendlyCategoryName : "");
let newLoad = true;
let browser = null;
if (!win) {
let windowArguments = Cc["@mozilla.org/array;1"].createInstance(
Ci.nsIMutableArray
);
let supportsStringPrefURL = Cc[
"@mozilla.org/supports-string;1"
].createInstance(Ci.nsISupportsString);
supportsStringPrefURL.data = preferencesURL;
windowArguments.appendElement(supportsStringPrefURL);
win = Services.ww.openWindow(
null,
AppConstants.BROWSER_CHROME_URL,
"_blank",
"chrome,dialog=no,all",
windowArguments
);
} else {
let shouldReplaceFragment = friendlyCategoryName
? "whenComparingAndReplace"
: "whenComparing";
newLoad = !win.switchToTabHavingURI(preferencesURL, true, {
ignoreFragment: shouldReplaceFragment,
replaceQueryString: true,
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});
browser = win.gBrowser.selectedBrowser;
}
if (!newLoad && paneID) {
if (browser.contentDocument?.readyState != "complete") {
await new Promise(resolve => {
browser.addEventListener("load", resolve, {
capture: true,
once: true,
});
});
}
browser.contentWindow.gotoPref(paneID);
}
}
/**
* Opens the troubleshooting information (about:support) page for this version
* of the application.
*/
function openTroubleshootingPage() {
openTrustedLinkIn("about:support", "tab");
}
/**
* Opens the feedback page for this version of the application.
*/
function openFeedbackPage() {
var url = Services.urlFormatter.formatURLPref("app.feedback.baseURL");
openTrustedLinkIn(url, "tab");
}
/**
* Appends UTM parameters to then opens the SUMO URL for device migration.
*/
function openSwitchingDevicesPage() {
let url = getHelpLinkURL("switching-devices");
let parsedUrl = new URL(url);
parsedUrl.searchParams.set("utm_content", "switch-to-new-device");
parsedUrl.searchParams.set("utm_source", "help-menu");
parsedUrl.searchParams.set("utm_medium", "firefox-desktop");
parsedUrl.searchParams.set("utm_campaign", "migration");
openTrustedLinkIn(parsedUrl.href, "tab");
}
function buildHelpMenu() {
document.getElementById("feedbackPage").disabled =
!Services.policies.isAllowed("feedbackCommands");
document.getElementById("helpSafeMode").disabled =
!Services.policies.isAllowed("safeMode");
document.getElementById("troubleShooting").disabled =
!Services.policies.isAllowed("aboutSupport");
let supportMenu = Services.policies.getSupportMenu();
if (supportMenu) {
let menuitem = document.getElementById("helpPolicySupport");
menuitem.hidden = false;
menuitem.setAttribute("label", supportMenu.Title);
if ("AccessKey" in supportMenu) {
menuitem.setAttribute("accesskey", supportMenu.AccessKey);
}
document.getElementById("helpPolicySeparator").hidden = false;
}
// Enable/disable the "Report Web Forgery" menu item.
if (typeof gSafeBrowsing != "undefined") {
gSafeBrowsing.setReportPhishingMenu();
}
// We're testing to see if the WebCompat team's "Report Site Issue"
// access point makes sense in the Help menu. Normally checking this
// pref wouldn't be enough, since there's also the case that the
// add-on has somehow been disabled by the user or third-party software
// without flipping the pref. Since this add-on is only used on pre-release
// channels, and since the jury is still out on whether or not the Help menu
// is the right place for this item, we're going to do a least-effort
// approach here and assume that the pref is enough to determine whether the
// menuitem should appear.
//
// See bug 1690573 for further details.
let reportSiteIssueEnabled = Services.prefs.getBoolPref(
"extensions.webcompat-reporter.enabled",
false
);
let reportSiteIssue = document.getElementById("help_reportSiteIssue");
reportSiteIssue.hidden = !reportSiteIssueEnabled;
if (reportSiteIssueEnabled) {
let uri = gBrowser.currentURI;
let isReportablePage =
uri && (uri.schemeIs("http") || uri.schemeIs("https"));
reportSiteIssue.disabled = !isReportablePage;
}
if (NimbusFeatures.deviceMigration.getVariable("helpMenuHidden")) {
let helpMenuItem = document.getElementById("helpSwitchDevice");
helpMenuItem.hidden = true;
}
}
function isElementVisible(aElement) {
if (!aElement) {
return false;
}
// If aElement or a direct or indirect parent is hidden or collapsed,
// height, width or both will be 0.
var rect = aElement.getBoundingClientRect();
return rect.height > 0 && rect.width > 0;
}
function makeURLAbsolute(aBase, aUrl) {
// Note: makeURI() will throw if aUri is not a valid URI
return makeURI(aUrl, null, makeURI(aBase)).spec;
}
function getHelpLinkURL(aHelpTopic) {
var url = Services.urlFormatter.formatURLPref("app.support.baseURL");
return url + aHelpTopic;
}
// aCalledFromModal is optional
function openHelpLink(aHelpTopic, aCalledFromModal, aWhere) {
var url = getHelpLinkURL(aHelpTopic);
var where = aWhere;
if (!aWhere) {
where = aCalledFromModal ? "window" : "tab";
}
openTrustedLinkIn(url, where);
}
function openPrefsHelp(aEvent) {
let helpTopic = aEvent.target.getAttribute("helpTopic");
openHelpLink(helpTopic);
}
|