tor-browser

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

OpenInTabsUtils.sys.mjs (2331B)


      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/. */
      4 
      5 const lazy = {};
      6 
      7 ChromeUtils.defineLazyGetter(lazy, "l10n", () => {
      8  return new Localization(
      9    ["browser/tabbrowser.ftl", "branding/brand.ftl"],
     10    true
     11  );
     12 });
     13 
     14 /**
     15 * Utility functions that can be used when opening multiple tabs, that can be
     16 * called without any tabbrowser instance.
     17 */
     18 export const OpenInTabsUtils = {
     19  /**
     20   * Gives the user a chance to cancel loading lots of tabs at once.
     21   */
     22  confirmOpenInTabs(numTabsToOpen, aWindow) {
     23    const WARN_ON_OPEN_PREF = "browser.tabs.warnOnOpen";
     24    const MAX_OPNE_PREF = "browser.tabs.maxOpenBeforeWarn";
     25    if (!Services.prefs.getBoolPref(WARN_ON_OPEN_PREF)) {
     26      return true;
     27    }
     28    if (numTabsToOpen < Services.prefs.getIntPref(MAX_OPNE_PREF)) {
     29      return true;
     30    }
     31 
     32    // default to true: if it were false, we wouldn't get this far
     33    let warnOnOpen = { value: true };
     34 
     35    const [title, message, button, checkbox] = lazy.l10n.formatMessagesSync([
     36      { id: "tabbrowser-confirm-open-multiple-tabs-title" },
     37      {
     38        id: "tabbrowser-confirm-open-multiple-tabs-message",
     39        args: { tabCount: numTabsToOpen },
     40      },
     41      { id: "tabbrowser-confirm-open-multiple-tabs-button" },
     42      { id: "tabbrowser-confirm-open-multiple-tabs-checkbox" },
     43    ]);
     44 
     45    let buttonPressed = Services.prompt.confirmEx(
     46      aWindow,
     47      title.value,
     48      message.value,
     49      Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0 +
     50        Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1,
     51      button.value,
     52      null,
     53      null,
     54      checkbox.value,
     55      warnOnOpen
     56    );
     57 
     58    let reallyOpen = buttonPressed == 0;
     59    // don't set the pref unless they press OK and it's false
     60    if (reallyOpen && !warnOnOpen.value) {
     61      Services.prefs.setBoolPref(WARN_ON_OPEN_PREF, false);
     62    }
     63 
     64    return reallyOpen;
     65  },
     66 
     67  /*
     68   * Async version of confirmOpenInTabs.
     69   */
     70  promiseConfirmOpenInTabs(numTabsToOpen, aWindow) {
     71    return new Promise(resolve => {
     72      Services.tm.dispatchToMainThread(() => {
     73        resolve(this.confirmOpenInTabs(numTabsToOpen, aWindow));
     74      });
     75    });
     76  },
     77 };