tor-browser

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

object.js (3199B)


      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 loader.lazyServiceGetter(
      8  this,
      9  "clipboardHelper",
     10  "@mozilla.org/widget/clipboardhelper;1",
     11  "nsIClipboardHelper"
     12 );
     13 
     14 loader.lazyRequireGetter(
     15  this,
     16  "l10n",
     17  "resource://devtools/client/webconsole/utils/messages.js",
     18  true
     19 );
     20 
     21 loader.lazyRequireGetter(
     22  this,
     23  "actions",
     24  "resource://devtools/client/webconsole/actions/index.js",
     25  false
     26 );
     27 
     28 loader.lazyRequireGetter(
     29  this,
     30  "PriorityLevels",
     31  "resource://devtools/client/shared/components/NotificationBox.js",
     32  true
     33 );
     34 
     35 function storeAsGlobal(actor) {
     36  return async ({ commands, hud }) => {
     37    const evalString = `{ let i = 0;
     38      while (this.hasOwnProperty("temp" + i) && i < 1000) {
     39        i++;
     40      }
     41      this["temp" + i] = _self;
     42      "temp" + i;
     43    }`;
     44 
     45    const res = await commands.scriptCommand.execute(evalString, {
     46      selectedObjectActor: actor,
     47      // Prevent any type of breakpoint when evaluating this code
     48      disableBreaks: true,
     49      // Ensure always overriding "$0" and "_self" console command, even if the page implements a variable with the same name.
     50      preferConsoleCommandsOverLocalSymbols: true,
     51    });
     52 
     53    // Select the adhoc target in the console.
     54    if (hud.toolbox) {
     55      const objectFront = commands.client.getFrontByID(actor);
     56      if (objectFront) {
     57        const targetActorID = objectFront.targetFront?.actorID;
     58        if (targetActorID) {
     59          hud.toolbox.selectTarget(targetActorID);
     60        }
     61      }
     62    }
     63 
     64    hud.focusInput();
     65    hud.setInputValue(res.result);
     66  };
     67 }
     68 
     69 function copyMessageObject(actor, variableText) {
     70  return async ({ commands, dispatch }) => {
     71    if (actor) {
     72      // The Debugger.Object of the OA will be bound to |_self| during evaluation.
     73      // See server/actors/webconsole/eval-with-debugger.js `evalWithDebugger`.
     74      const res = await commands.scriptCommand.execute("copy(_self)", {
     75        selectedObjectActor: actor,
     76        // Prevent any type of breakpoint when evaluating this code
     77        disableBreaks: true,
     78        // ensure always overriding "$0" and "_self" console command, even if the page implements a variable with the same name.
     79        preferConsoleCommandsOverLocalSymbols: true,
     80      });
     81      if (res.helperResult.type === "copyValueToClipboard") {
     82        clipboardHelper.copyString(res.helperResult.value);
     83      } else if (res.helperResult.type === "error") {
     84        const nofificationName = "copy-value-to-clipboard-notification";
     85        dispatch(
     86          actions.appendNotification(
     87            l10n.getFormatStr(
     88              res.helperResult.message,
     89              res.helperResult.messageArgs
     90            ),
     91            nofificationName,
     92            null,
     93            PriorityLevels.PRIORITY_CRITICAL_HIGH,
     94            null,
     95            () => dispatch(actions.removeNotification(nofificationName))
     96          )
     97        );
     98      }
     99    } else {
    100      clipboardHelper.copyString(variableText);
    101    }
    102  };
    103 }
    104 
    105 module.exports = {
    106  storeAsGlobal,
    107  copyMessageObject,
    108 };