lowMediumErrorPages.js (5555B)
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 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /* eslint-disable no-unsanitized/property */ /* bug 1889942 */ 6 7 /** 8 * Handles the parsing of the ErrorPages URI and then passes them to injectValues 9 */ 10 function parseQuery(queryString) { 11 if (queryString[0] === "?") { 12 queryString = queryString.substr(1); 13 } 14 const query = Object.fromEntries(new URLSearchParams(queryString).entries()); 15 injectValues(query); 16 updateShowSSL(query); 17 updateShowHSTS(query); 18 } 19 20 /** 21 * Updates the HTML elements based on the queryMap 22 */ 23 function injectValues(queryMap) { 24 const tryAgainButton = document.getElementById("errorTryAgain"); 25 const continueHttpButton = document.getElementById("continueHttp"); 26 const backFromHttpButton = document.getElementById("backFromHttp"); 27 28 // Go through each element and inject the values 29 document.title = queryMap.title; 30 tryAgainButton.innerHTML = queryMap.button; 31 continueHttpButton.innerHTML = queryMap.continueHttpButton; 32 backFromHttpButton.innerHTML = queryMap.badCertGoBack; 33 document.getElementById("errorTitleText").innerHTML = queryMap.title; 34 document.getElementById("errorShortDesc").innerHTML = queryMap.description; 35 document.getElementById("advancedButton").innerHTML = 36 queryMap.badCertAdvanced; 37 document.getElementById("badCertTechnicalInfo").innerHTML = 38 queryMap.badCertTechInfo; 39 document.getElementById("advancedPanelBackButton").innerHTML = 40 queryMap.badCertGoBack; 41 document.getElementById("advancedPanelAcceptButton").innerHTML = 42 queryMap.badCertAcceptTemporary; 43 44 // If no image is passed in, remove the element so as not to leave an empty iframe 45 const errorImage = document.getElementById("errorImage"); 46 if (!queryMap.image) { 47 errorImage.remove(); 48 } else { 49 errorImage.src = "resource://android/assets/" + queryMap.image; 50 } 51 52 if (queryMap.showContinueHttp === "true") { 53 // On the "HTTPS-Only" error page "Try again" doesn't make sense since reloading the page 54 // will just show an error page again. 55 tryAgainButton.style.display = "none"; 56 } else { 57 continueHttpButton.style.display = "none"; 58 backFromHttpButton.style.display = "none"; 59 } 60 61 if (queryMap.errorCode) { 62 const errorCode = document.getElementById("errorCode"); 63 errorCode.textContent = queryMap.errorCode; 64 } 65 } 66 67 let advancedVisible = false; 68 69 /** 70 * Used to show or hide the "accept" button based on the validity of the SSL certificate 71 */ 72 function updateShowSSL(queryMap) { 73 /** @type {'true' | 'false'} */ 74 const showSSL = queryMap.showSSL; 75 if (typeof document.addCertException === "undefined") { 76 document.getElementById("advancedButton").style.display = "none"; 77 } else if (showSSL === "true") { 78 document.getElementById("advancedButton").style.display = "block"; 79 } else { 80 document.getElementById("advancedButton").style.display = "none"; 81 } 82 } 83 84 /** 85 * Used to show or hide the "accept" button based for the HSTS error page 86 */ 87 function updateShowHSTS(queryMap) { 88 const showHSTS = queryMap.showHSTS; 89 if (showHSTS === "true") { 90 document.getElementById("advancedButton").style.display = "block"; 91 document.getElementById("advancedPanelAcceptButton").style.display = "none"; 92 } 93 } 94 95 /** 96 * Used to display information about the SSL certificate in `error_pages.html` 97 */ 98 function toggleAdvancedAndScroll() { 99 const advancedPanel = document.getElementById("badCertAdvancedPanel"); 100 if (advancedVisible) { 101 advancedPanel.style.display = "none"; 102 } else { 103 advancedPanel.style.display = "block"; 104 } 105 advancedVisible = !advancedVisible; 106 107 const horizontalLine = document.getElementById("horizontalLine"); 108 const advancedPanelAcceptButton = document.getElementById( 109 "advancedPanelAcceptButton" 110 ); 111 const badCertAdvancedPanel = document.getElementById("badCertAdvancedPanel"); 112 113 // We know that the button is being displayed 114 if (badCertAdvancedPanel.style.display === "block") { 115 horizontalLine.hidden = false; 116 advancedPanelAcceptButton.scrollIntoView({ 117 behavior: "smooth", 118 block: "center", 119 inline: "nearest", 120 }); 121 } else { 122 horizontalLine.hidden = true; 123 } 124 } 125 126 /** 127 * Used to bypass an SSL pages in `error_pages.html` 128 */ 129 async function acceptAndContinue(temporary) { 130 try { 131 await document.addCertException(temporary); 132 location.reload(); 133 } catch (error) { 134 console.error("Unexpected error: " + error); 135 } 136 } 137 138 document.addEventListener("DOMContentLoaded", function () { 139 if (window.history.length == 1) { 140 document.getElementById("advancedPanelBackButton").style.display = "none"; 141 document.getElementById("backFromHttp").style.display = "none"; 142 } else { 143 document 144 .getElementById("advancedPanelBackButton") 145 .addEventListener("click", () => window.history.back()); 146 document 147 .getElementById("backFromHttp") 148 .addEventListener("click", () => window.history.back()); 149 } 150 151 document 152 .getElementById("errorTryAgain") 153 .addEventListener("click", () => window.location.reload()); 154 document 155 .getElementById("advancedButton") 156 .addEventListener("click", toggleAdvancedAndScroll); 157 document 158 .getElementById("advancedPanelAcceptButton") 159 .addEventListener("click", () => acceptAndContinue(true)); 160 document 161 .getElementById("continueHttp") 162 .addEventListener("click", () => document.reloadWithHttpsOnlyException()); 163 }); 164 165 parseQuery(document.documentURI);