tor-browser

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

screenshots-buttons.js (3103B)


      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 "use strict";
      6 
      7 // This is loaded into chrome windows with the subscript loader. Wrap in
      8 // a block to prevent accidentally leaking globals onto `window`.
      9 {
     10  ChromeUtils.defineESModuleGetters(this, {
     11    ScreenshotsUtils: "resource:///modules/ScreenshotsUtils.sys.mjs",
     12  });
     13 
     14  class ScreenshotsButtons extends MozXULElement {
     15    static #template = null;
     16 
     17    static get markup() {
     18      return `
     19        <html:link rel="stylesheet" href="chrome://global/skin/global.css" />
     20        <html:link rel="stylesheet" href="chrome://browser/content/screenshots/screenshots-buttons.css" />
     21        <html:moz-button-group>
     22          <html:button id="visible-page" class="screenshot-button footer-button" data-l10n-id="screenshots-save-visible-button"></html:button>
     23          <html:button id="full-page" class="screenshot-button footer-button primary" data-l10n-id="screenshots-save-page-button"></html:button>
     24        </html:moz-button-group>
     25 
     26      `;
     27    }
     28 
     29    static get fragment() {
     30      if (!ScreenshotsButtons.#template) {
     31        ScreenshotsButtons.#template = MozXULElement.parseXULToFragment(
     32          ScreenshotsButtons.markup
     33        );
     34      }
     35      return ScreenshotsButtons.#template;
     36    }
     37 
     38    connectedCallback() {
     39      const shadowRoot = this.attachShadow({ mode: "open" });
     40      document.l10n.connectRoot(shadowRoot);
     41 
     42      this.shadowRoot.append(ScreenshotsButtons.fragment);
     43 
     44      let visibleButton = shadowRoot.getElementById("visible-page");
     45      visibleButton.onclick = function () {
     46        ScreenshotsUtils.takeScreenshot(gBrowser.selectedBrowser, "Visible");
     47      };
     48 
     49      let fullpageButton = shadowRoot.getElementById("full-page");
     50      fullpageButton.onclick = function () {
     51        ScreenshotsUtils.takeScreenshot(gBrowser.selectedBrowser, "FullPage");
     52      };
     53    }
     54 
     55    disconnectedCallback() {
     56      document.l10n.disconnectRoot(this.shadowRoot);
     57    }
     58 
     59    /**
     60     * Focus the last used button.
     61     * This will default to the visible page button.
     62     *
     63     * @param {string} buttonToFocus
     64     */
     65    async focusButton(buttonToFocus) {
     66      await this.shadowRoot.querySelector("moz-button-group").updateComplete;
     67      if (buttonToFocus === "fullpage") {
     68        this.shadowRoot
     69          .getElementById("full-page")
     70          .focus({ focusVisible: true });
     71      } else if (buttonToFocus === "first") {
     72        this.shadowRoot
     73          .querySelector("moz-button-group")
     74          .firstElementChild.focus({ focusVisible: true });
     75      } else if (buttonToFocus === "last") {
     76        this.shadowRoot
     77          .querySelector("moz-button-group")
     78          .lastElementChild.focus({ focusVisible: true });
     79      } else {
     80        this.shadowRoot
     81          .getElementById("visible-page")
     82          .focus({ focusVisible: true });
     83      }
     84    }
     85  }
     86 
     87  customElements.define("screenshots-buttons", ScreenshotsButtons, {
     88    extends: "toolbar",
     89  });
     90 }