tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

messagepreview.js (1842B)


      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 /* global MPShowMessage, MPIsEnabled, MPShouldShowHint, MPToggleLights */
      6 
      7 "use strict";
      8 
      9 // decode a 16-bit string in which only one byte of each
     10 // 16-bit unit is occupied, to UTF-8. This is necessary to
     11 // comply with `btoa` API constraints.
     12 function fromBinary(encoded) {
     13  const binary = atob(decodeURIComponent(encoded));
     14  const bytes = new Uint8Array(binary.length);
     15  for (let i = 0; i < bytes.length; i++) {
     16    bytes[i] = binary.charCodeAt(i);
     17  }
     18  return String.fromCharCode(...new Uint16Array(bytes.buffer));
     19 }
     20 
     21 function decodeMessageFromUrl() {
     22  const url = new URL(document.location.href);
     23 
     24  if (url.searchParams.has("json")) {
     25    const encodedMessage = url.searchParams.get("json");
     26 
     27    return fromBinary(encodedMessage);
     28  }
     29  return null;
     30 }
     31 
     32 function showHint() {
     33  document.body.classList.add("hint-box");
     34  document.body.innerHTML = `<div class="hint">Message preview is not enabled. Enable it in about:config by setting <code>browser.newtabpage.activity-stream.asrouter.devtoolsEnabled</code> to true.</div>`;
     35 }
     36 
     37 // Light switch things
     38 document.addEventListener("DOMContentLoaded", () => {
     39  document
     40    .querySelector("#light-switch")
     41    .addEventListener("click", MPToggleLights);
     42 });
     43 
     44 const message = decodeMessageFromUrl();
     45 
     46 if (message) {
     47  // If message preview is enabled, show the message.
     48  if (MPIsEnabled()) {
     49    MPShowMessage(message);
     50  } else if (MPShouldShowHint()) {
     51    // If running in a local build, show a hint about how to enable preview.
     52    if (document.body) {
     53      showHint();
     54    } else {
     55      document.addEventListener("DOMContentLoaded", showHint, { once: true });
     56    }
     57  }
     58 }