tor-browser

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

helpers.mjs (3371B)


      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 const lazy = {};
      6 const loggersByName = new Map();
      7 
      8 ChromeUtils.defineESModuleGetters(lazy, {
      9  BrowserUtils: "resource://gre/modules/BrowserUtils.sys.mjs",
     10  Log: "resource://gre/modules/Log.sys.mjs",
     11  PlacesUIUtils: "moz-src:///browser/components/places/PlacesUIUtils.sys.mjs",
     12 });
     13 
     14 ChromeUtils.defineLazyGetter(lazy, "relativeTimeFormat", () => {
     15  return new Services.intl.RelativeTimeFormat(undefined, { style: "narrow" });
     16 });
     17 
     18 // Cutoff of 1.5 minutes + 1 second to determine what text string to display
     19 export const NOW_THRESHOLD_MS = 91000;
     20 
     21 // Configure logging level via this pref
     22 export const LOGGING_PREF = "browser.tabs.firefox-view.logLevel";
     23 
     24 export const MAX_TABS_FOR_RECENT_BROWSING = 5;
     25 
     26 export function formatURIForDisplay(uriString) {
     27  return lazy.BrowserUtils.formatURIStringForDisplay(uriString, {
     28    showFilenameForLocalURIs: true,
     29  });
     30 }
     31 
     32 export function convertTimestamp(
     33  timestamp,
     34  fluentStrings,
     35  _nowThresholdMs = NOW_THRESHOLD_MS
     36 ) {
     37  if (!timestamp) {
     38    // It's marginally better to show nothing instead of "53 years ago"
     39    return "";
     40  }
     41  const elapsed = Date.now() - timestamp;
     42  let formattedTime;
     43  if (elapsed <= _nowThresholdMs) {
     44    // Use a different string for very recent timestamps
     45    formattedTime = fluentStrings.formatValueSync(
     46      "firefoxview-just-now-timestamp"
     47    );
     48  } else {
     49    formattedTime = lazy.relativeTimeFormat.formatBestUnit(new Date(timestamp));
     50  }
     51  return formattedTime;
     52 }
     53 
     54 export function createFaviconElement(image, targetURI = "") {
     55  let favicon = document.createElement("div");
     56  favicon.style.backgroundImage = `url('${getImageUrl(image, targetURI)}')`;
     57  favicon.classList.add("favicon");
     58  return favicon;
     59 }
     60 
     61 export function getImageUrl(icon, targetURI) {
     62  return icon ? lazy.PlacesUIUtils.getImageURL(icon) : `page-icon:${targetURI}`;
     63 }
     64 
     65 /**
     66 * Get or create a logger, whose log-level is controlled by a pref
     67 *
     68 * @param {string} loggerName - Creating named loggers helps differentiate log messages from different
     69                                components or features.
     70 */
     71 
     72 export function getLogger(loggerName) {
     73  if (!loggersByName.has(loggerName)) {
     74    let logger = lazy.Log.repository.getLogger(`FirefoxView.${loggerName}`);
     75    logger.manageLevelFromPref(LOGGING_PREF);
     76    logger.addAppender(
     77      new lazy.Log.ConsoleAppender(new lazy.Log.BasicFormatter())
     78    );
     79    loggersByName.set(loggerName, logger);
     80  }
     81  return loggersByName.get(loggerName);
     82 }
     83 
     84 export function escapeHtmlEntities(text) {
     85  return (text || "")
     86    .replace(/&/g, "&amp;")
     87    .replace(/</g, "&lt;")
     88    .replace(/>/g, "&gt;")
     89    .replace(/"/g, "&quot;")
     90    .replace(/'/g, "&#39;");
     91 }
     92 
     93 export function navigateToLink(
     94  e,
     95  url = e.originalTarget.url,
     96  { forceNewTab = true } = {}
     97 ) {
     98  let currentWindow =
     99    e.target.ownerGlobal.browsingContext.embedderWindowGlobal.browsingContext
    100      .window;
    101  if (currentWindow.openTrustedLinkIn) {
    102    let where = lazy.BrowserUtils.whereToOpenLink(
    103      e.detail.originalEvent,
    104      false,
    105      true
    106    );
    107    if (where == "current" && forceNewTab) {
    108      where = "tab";
    109    }
    110    currentWindow.openTrustedLinkIn(url, where);
    111  }
    112 }