tor-browser

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

HeadersPanelContextMenu.js (3903B)


      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 const {
      8  L10N,
      9 } = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
     10 const {
     11  contextMenuFormatters,
     12 } = require("resource://devtools/client/netmonitor/src/utils/context-menu-utils.js");
     13 
     14 loader.lazyRequireGetter(
     15  this,
     16  "copyString",
     17  "resource://devtools/shared/platform/clipboard.js",
     18  true
     19 );
     20 loader.lazyRequireGetter(
     21  this,
     22  "showMenu",
     23  "resource://devtools/client/shared/components/menu/utils.js",
     24  true
     25 );
     26 
     27 class HeadersPanelContextMenu {
     28  constructor(props = {}) {
     29    this.props = props;
     30    this.copyAll = this.copyAll.bind(this);
     31    this.copyValue = this.copyValue.bind(this);
     32  }
     33 
     34  /**
     35   * Handle the context menu opening.
     36   *
     37   * @param {object} event open event
     38   * @param {object} selection object representing the current selection
     39   */
     40  open(event = {}, selection) {
     41    const { target } = event;
     42    const menuItems = [
     43      {
     44        id: "headers-panel-context-menu-copyvalue",
     45        label: L10N.getStr("netmonitor.context.copyValue"),
     46        accesskey: L10N.getStr("netmonitor.context.copyValue.accesskey"),
     47        click: () => {
     48          const { name, value } = getSummaryContent(
     49            target.closest(".tabpanel-summary-container")
     50          );
     51          this.copyValue(
     52            { name, value, object: null, hasChildren: false },
     53            selection
     54          );
     55        },
     56      },
     57      {
     58        id: "headers-panel-context-menu-copyall",
     59        label: L10N.getStr("netmonitor.context.copyAll"),
     60        accesskey: L10N.getStr("netmonitor.context.copyAll.accesskey"),
     61        click: () => {
     62          const root = target.closest(".summary");
     63          const object = {};
     64          if (root) {
     65            const { children } = root;
     66            for (let i = 0; i < children.length; i++) {
     67              const content = getSummaryContent(children[i]);
     68              object[content.name] = content.value;
     69            }
     70          }
     71          return this.copyAll(object, selection);
     72        },
     73      },
     74    ];
     75 
     76    showMenu(menuItems, {
     77      screenX: event.screenX,
     78      screenY: event.screenY,
     79    });
     80  }
     81 
     82  /**
     83   * Copies all.
     84   *
     85   * @param {object} object the whole tree data
     86   * @param {object} selection object representing the current selection
     87   */
     88  copyAll(object, selection) {
     89    let buffer = "";
     90    if (selection.toString() !== "") {
     91      buffer = selection.toString();
     92    } else {
     93      const { customFormatters } = this.props;
     94      buffer = contextMenuFormatters.baseCopyAllFormatter(object);
     95      if (customFormatters?.copyAllFormatter) {
     96        buffer = customFormatters.copyAllFormatter(
     97          object,
     98          contextMenuFormatters.baseCopyAllFormatter
     99        );
    100      }
    101    }
    102    try {
    103      copyString(buffer);
    104    } catch (error) {}
    105  }
    106 
    107  /**
    108   * Copies the value of a single item.
    109   *
    110   * @param {object} object data object for specific node
    111   * @param {object} selection object representing the current selection
    112   */
    113  copyValue(object, selection) {
    114    let buffer = "";
    115    if (selection.toString() !== "") {
    116      buffer = selection.toString();
    117    } else {
    118      const { customFormatters } = this.props;
    119      buffer = contextMenuFormatters.baseCopyFormatter(object);
    120      if (customFormatters?.copyFormatter) {
    121        buffer = customFormatters.copyFormatter(
    122          object,
    123          contextMenuFormatters.baseCopyFormatter
    124        );
    125      }
    126    }
    127    try {
    128      copyString(buffer);
    129    } catch (error) {}
    130  }
    131 }
    132 
    133 function getSummaryContent(el) {
    134  return {
    135    name: el.querySelector(".tabpanel-summary-label").textContent,
    136    value: el.querySelector(".tabpanel-summary-value").textContent,
    137  };
    138 }
    139 
    140 module.exports = HeadersPanelContextMenu;