tor-browser

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

har-menu-utils.js (3545B)


      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 EventEmitter = require("resource://devtools/shared/event-emitter.js");
     11 
     12 loader.lazyRequireGetter(
     13  this,
     14  "HarExporter",
     15  "resource://devtools/client/netmonitor/src/har/har-exporter.js",
     16  true
     17 );
     18 
     19 loader.lazyGetter(this, "HarImporter", function () {
     20  return require("resource://devtools/client/netmonitor/src/har/har-importer.js")
     21    .HarImporter;
     22 });
     23 
     24 /**
     25 * Helper object with HAR related context menu actions.
     26 */
     27 var HarMenuUtils = {
     28  /**
     29   * Copy HAR from the network panel content to the clipboard.
     30   */
     31  async copyAllAsHar(requests, connector) {
     32    const har = await HarExporter.copy(
     33      this.getDefaultHarOptions(requests, connector)
     34    );
     35 
     36    // We cannot easily expect the clipboard content from tests, instead we emit
     37    // a test event.
     38    HarMenuUtils.emitForTests("copy-all-as-har-done", har);
     39 
     40    return har;
     41  },
     42 
     43  /**
     44   * Save selected request from the network panel to a HAR file.
     45   */
     46  saveAsHar(clickedRequest, connector) {
     47    const options = this.getDefaultHarOptions([clickedRequest], connector);
     48    options.isSingleRequest = true;
     49    return HarExporter.save(options);
     50  },
     51 
     52  /**
     53   * Save all displayed requests in the network panel to a HAR file.
     54   */
     55  saveAllAsHar(requests, connector) {
     56    // This will not work in launchpad
     57    // document.execCommand(‘cut’/‘copy’) was denied because it was not called from
     58    // inside a short running user-generated event handler.
     59    // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Interact_with_the_clipboard
     60    return HarExporter.save(this.getDefaultHarOptions(requests, connector));
     61  },
     62 
     63  /**
     64   * Import HAR file and preview its content in the Network panel.
     65   */
     66  openHarFile(actions, openSplitConsole) {
     67    const fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
     68    fp.init(
     69      window.browsingContext,
     70      L10N.getStr("netmonitor.har.importHarDialogTitle"),
     71      Ci.nsIFilePicker.modeOpen
     72    );
     73 
     74    // Append file filters
     75    fp.appendFilter(
     76      L10N.getStr("netmonitor.har.importDialogHARFilter"),
     77      "*.har"
     78    );
     79    fp.appendFilter(L10N.getStr("netmonitor.har.importDialogAllFilter"), "*.*");
     80 
     81    fp.open(rv => {
     82      if (rv == Ci.nsIFilePicker.returnOK) {
     83        const file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
     84        file.initWithPath(fp.file.path);
     85        readFile(file).then(har => {
     86          if (har) {
     87            this.appendPreview(har, actions, openSplitConsole);
     88          }
     89        });
     90      }
     91    });
     92  },
     93 
     94  appendPreview(har, actions, openSplitConsole) {
     95    try {
     96      const importer = new HarImporter(actions);
     97      importer.import(har);
     98    } catch (err) {
     99      if (openSplitConsole) {
    100        openSplitConsole("Error while processing HAR file: " + err.message);
    101      }
    102    }
    103  },
    104 
    105  getDefaultHarOptions(requests, connector) {
    106    return {
    107      connector,
    108      items: requests,
    109    };
    110  },
    111 };
    112 
    113 // Helpers
    114 
    115 function readFile(file) {
    116  return new Promise(resolve => {
    117    IOUtils.read(file.path).then(data => {
    118      const decoder = new TextDecoder();
    119      resolve(decoder.decode(data));
    120    });
    121  });
    122 }
    123 
    124 EventEmitter.decorate(HarMenuUtils);
    125 
    126 // Exports from this module
    127 exports.HarMenuUtils = HarMenuUtils;