tor-browser

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

browser_script_command_execute_last_result.js (2420B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Testing that last evaluation result can be accessed with `$_`
      7 
      8 add_task(async () => {
      9  const tab = await addTab(`data:text/html;charset=utf-8,`);
     10 
     11  const commands = await CommandsFactory.forTab(tab);
     12  await commands.targetCommand.startListening();
     13 
     14  info("$_ returns undefined if nothing has evaluated yet");
     15  let response = await commands.scriptCommand.execute("$_");
     16  basicResultCheck(response, "$_", { type: "undefined" });
     17 
     18  info("$_ returns last value and performs basic arithmetic");
     19  response = await commands.scriptCommand.execute("2+2");
     20  basicResultCheck(response, "2+2", 4);
     21 
     22  response = await commands.scriptCommand.execute("$_");
     23  basicResultCheck(response, "$_", 4);
     24 
     25  response = await commands.scriptCommand.execute("$_ + 2");
     26  basicResultCheck(response, "$_ + 2", 6);
     27 
     28  response = await commands.scriptCommand.execute("$_ + 4");
     29  basicResultCheck(response, "$_ + 4", 10);
     30 
     31  info("$_ has correct references to objects");
     32  response = await commands.scriptCommand.execute("var foo = {bar:1}; foo;");
     33  basicResultCheck(response, "var foo = {bar:1}; foo;", {
     34    type: "object",
     35    class: "Object",
     36    actor: /[a-z]/,
     37  });
     38  checkObject(response.result.getGrip().preview.ownProperties, {
     39    bar: {
     40      value: 1,
     41    },
     42  });
     43 
     44  response = await commands.scriptCommand.execute("$_");
     45  basicResultCheck(response, "$_", {
     46    type: "object",
     47    class: "Object",
     48    actor: /[a-z]/,
     49  });
     50  checkObject(response.result.getGrip().preview.ownProperties, {
     51    bar: {
     52      value: 1,
     53    },
     54  });
     55 
     56  info(
     57    "Update a property value and check that evaluating $_ returns the expected object instance"
     58  );
     59  await ContentTask.spawn(gBrowser.selectedBrowser, null, () => {
     60    content.wrappedJSObject.foo.bar = "updated_value";
     61  });
     62 
     63  response = await commands.scriptCommand.execute("$_");
     64  basicResultCheck(response, "$_", {
     65    type: "object",
     66    class: "Object",
     67    actor: /[a-z]/,
     68  });
     69  checkObject(response.result.getGrip().preview.ownProperties, {
     70    bar: {
     71      value: "updated_value",
     72    },
     73  });
     74 
     75  await commands.destroy();
     76 });
     77 
     78 function basicResultCheck(response, input, output) {
     79  checkObject(response, {
     80    input,
     81    result: output,
     82  });
     83  ok(!response.exception, "no eval exception");
     84  ok(!response.helperResult, "no helper result");
     85 }