tor-browser

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

MessageListContextMenu.js (2991B)


      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 loader.lazyRequireGetter(
     11  this,
     12  "showMenu",
     13  "resource://devtools/client/shared/components/menu/utils.js",
     14  true
     15 );
     16 loader.lazyRequireGetter(
     17  this,
     18  "copyString",
     19  "resource://devtools/shared/platform/clipboard.js",
     20  true
     21 );
     22 const {
     23  getMessagePayload,
     24 } = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");
     25 
     26 const toHexString = str =>
     27  Array.from(str, c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("");
     28 
     29 class MessageListContextMenu {
     30  constructor(props) {
     31    this.props = props;
     32  }
     33 
     34  /**
     35   * Handle the context menu opening.
     36   */
     37  open(event = {}, item) {
     38    const menuItems = [];
     39    if (this.props.showBinaryOptions) {
     40      menuItems.push(
     41        {
     42          id: "message-list-context-copy-message-base64",
     43          label: L10N.getStr("netmonitor.ws.context.copyFrameAsBase64"),
     44          accesskey: L10N.getStr(
     45            "netmonitor.ws.context.copyFrameAsBase64.accesskey"
     46          ),
     47          click: () => this.copyMessagePayload(item, btoa),
     48        },
     49        {
     50          id: "message-list-context-copy-message-hex",
     51          label: L10N.getStr("netmonitor.ws.context.copyFrameAsHex"),
     52          accesskey: L10N.getStr(
     53            "netmonitor.ws.context.copyFrameAsHex.accesskey"
     54          ),
     55          click: () => this.copyMessagePayload(item, toHexString),
     56        },
     57        {
     58          id: "message-list-context-copy-message-text",
     59          label: L10N.getStr("netmonitor.ws.context.copyFrameAsText"),
     60          accesskey: L10N.getStr(
     61            "netmonitor.ws.context.copyFrameAsText.accesskey"
     62          ),
     63          click: () => this.copyMessagePayload(item),
     64        }
     65      );
     66    } else {
     67      menuItems.push({
     68        id: "message-list-context-copy-message",
     69        label: L10N.getStr("netmonitor.ws.context.copyFrame"),
     70        accesskey: L10N.getStr("netmonitor.ws.context.copyFrame.accesskey"),
     71        click: () => this.copyMessagePayload(item),
     72      });
     73    }
     74 
     75    showMenu(menuItems, {
     76      screenX: event.screenX,
     77      screenY: event.screenY,
     78    });
     79  }
     80 
     81  /**
     82   * Copy the full payload from the selected message.
     83   * Can optionally format the payload before copying.
     84   *
     85   * @param {object} item
     86   * @param {object|string} item.payload
     87   *   Payload to copy.
     88   * @param {function(string): string} [format]
     89   *   Optional function to format the payload before copying.
     90   */
     91  copyMessagePayload(item, format) {
     92    getMessagePayload(item.payload, this.props.connector.getLongString).then(
     93      payload => {
     94        if (format) {
     95          payload = format(payload);
     96        }
     97        copyString(payload);
     98      }
     99    );
    100  }
    101 }
    102 
    103 module.exports = MessageListContextMenu;