Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / browser / base / content / blockedSite.js
blobdd2aa914fe709fedcda5957c31d8a97a33cb9490
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Error url MUST be formatted like this:
6 //   about:blocked?e=error_code&u=url(&o=1)?
7 //     (o=1 when user overrides are allowed)
9 // Note that this file uses document.documentURI to get
10 // the URL (with the format from above). This is because
11 // document.location.href gets the current URI off the docshell,
12 // which is the URL displayed in the location bar, i.e.
13 // the URI that the user attempted to load.
15 function getErrorCode() {
16   var url = document.documentURI;
17   var error = url.search(/e\=/);
18   var duffUrl = url.search(/\&u\=/);
19   return decodeURIComponent(url.slice(error + 2, duffUrl));
22 function getURL() {
23   var url = document.documentURI;
24   var match = url.match(/&u=([^&]+)&/);
26   // match == null if not found; if so, return an empty string
27   // instead of what would turn out to be portions of the URI
28   if (!match) {
29     return "";
30   }
32   url = decodeURIComponent(match[1]);
34   // If this is a view-source page, then get then real URI of the page
35   if (url.startsWith("view-source:")) {
36     url = url.slice(12);
37   }
38   return url;
41 /**
42  * Check whether this warning page is overridable or not, in which case
43  * the "ignore the risk" suggestion in the error description
44  * should not be shown.
45  */
46 function getOverride() {
47   var url = document.documentURI;
48   var match = url.match(/&o=1&/);
49   return !!match;
52 /**
53  * Attempt to get the hostname via document.location.  Fail back
54  * to getURL so that we always return something meaningful.
55  */
56 function getHostString() {
57   try {
58     return document.location.hostname;
59   } catch (e) {
60     return getURL();
61   }
64 function onClickSeeDetails() {
65   let details = document.getElementById("errorDescriptionContainer");
66   details.hidden = !details.hidden;
69 function initPage() {
70   var error = "";
71   switch (getErrorCode()) {
72     case "malwareBlocked":
73       error = "malware";
74       break;
75     case "deceptiveBlocked":
76       error = "phishing";
77       break;
78     case "unwantedBlocked":
79       error = "unwanted";
80       break;
81     case "harmfulBlocked":
82       error = "harmful";
83       break;
84     default:
85       return;
86   }
88   // Set page contents depending on type of blocked page
89   // Prepare the title and short description text
90   let titleText = document.getElementById("errorTitleText");
91   document.l10n.setAttributes(
92     titleText,
93     "safeb-blocked-" + error + "-page-title"
94   );
95   let shortDesc = document.getElementById("errorShortDescText");
96   document.l10n.setAttributes(
97     shortDesc,
98     "safeb-blocked-" + error + "-page-short-desc"
99   );
101   // Prepare the inner description, ensuring any redundant inner elements are removed.
102   let innerDesc = document.getElementById("errorInnerDescription");
103   let innerDescL10nID = "safeb-blocked-" + error + "-page-error-desc-";
104   if (!getOverride()) {
105     innerDescL10nID += "no-override";
106     document.getElementById("ignore_warning_link").remove();
107   } else {
108     innerDescL10nID += "override";
109   }
110   if (error == "unwanted" || error == "harmful") {
111     document.getElementById("report_detection").remove();
112   }
114   // Add the inner description:
115   // Map specific elements to a different message ID, to allow updates to
116   // existing labels
117   let descriptionMapping = {
118     malware: innerDescL10nID + "-sumo",
119   };
120   document.l10n.setAttributes(
121     innerDesc,
122     descriptionMapping[error] || innerDescL10nID,
123     {
124       sitename: getHostString(),
125     }
126   );
128   // Add the learn more content:
129   // Map specific elements to a different message ID, to allow updates to
130   // existing labels
131   let stringMapping = {
132     malware: "safeb-blocked-malware-page-learn-more-sumo",
133   };
135   let learnMore = document.getElementById("learn_more");
136   document.l10n.setAttributes(
137     learnMore,
138     stringMapping[error] || `safeb-blocked-${error}-page-learn-more`
139   );
141   // Set sitename to bold by adding class
142   let errorSitename = document.getElementById("error_desc_sitename");
143   errorSitename.setAttribute("class", "sitename");
145   let titleEl = document.createElement("title");
146   document.l10n.setAttributes(
147     titleEl,
148     "safeb-blocked-" + error + "-page-title"
149   );
150   document.head.appendChild(titleEl);
152   // Inform the test harness that we're done loading the page.
153   var event = new CustomEvent("AboutBlockedLoaded", {
154     bubbles: true,
155     detail: {
156       url: this.getURL(),
157       err: error,
158     },
159   });
160   document.dispatchEvent(event);
163 let seeDetailsButton = document.getElementById("seeDetailsButton");
164 seeDetailsButton.addEventListener("click", onClickSeeDetails);
165 // Note: It is important to run the script this way, instead of using
166 // an onload handler. This is because error pages are loaded as
167 // LOAD_BACKGROUND, which means that onload handlers will not be executed.
168 initPage();