Skip to content

v0.5.0

Compare
Choose a tag to compare
@ibuninngu ibuninngu released this 10 Mar 16:35
· 29 commits to main since this release
8322c14

Scripting

Automate with javascript

Table of Contents

Global objects

  • log ... console.log
  • assert ... console.assert
  • await sleep
  • BlueFoxScript
  • BlueFoxJs

Scripting Reference

BlueFoxScript.runScript()

Run String:script

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  let script = await (
    await fetch("https://ooo.bluefox.ooo/BlueFoxDemo/js/confirm.js")
  ).text();
  window.alert(
    `Confirmed: ${(await blueFoxScript.runScript(script)).result.value}`
  );
})();

BlueFoxScript.runWorkspaceScript()

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  await blueFoxScript.runWorkspaceScript("/alert.js");
})();

BlueFoxScript.getWorkspaceFile()

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  let file = await blueFoxScript.getWorkspaceFile("/img/BlueFox.png");

  // Property
  file.name;
  file.type;
  file.blob;
  file.object;
})();

BlueFoxScript.tabs.info[n]

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  tab = await blueFoxScript.createWindow(
    "https://ooo.bluefox.ooo/BlueFoxDemo/8bit.html"
  );

  tab = await blueFoxScript.findTab(
    "https://ooo.bluefox.ooo/BlueFoxDemo/8bit.html"
  )[0];

  // Property
  tab.info;
  tab.responses;
  tab.keepResponses;
})();

BlueFoxScript[tabId].tails() ... Method chaining

(async () => {
  let tails = tab.tails({
    sleep: 100,
    dispatchEvents: [
      {
        option: {
          eventObject: "Event",
          eventType: "change",
          eventArgs: {
            bubbles: true,
          },
        },
      },
      {
        option: {
          eventObject: "Event",
          eventType: "input",
          eventArgs: {
            bubbles: true,
          },
        },
      },
    ],
  });
})();

.init()

(async () => {
  tails.init({ sleep: 100 });
})();

.target()

(async () => {
  tails.target(`CSS Selector or XPath`);
})();

.defined()

same as .target(`:is([data-testid="${UI Name}"],[bluefox-label="${UI Name}"],[aria-description="${UI Name}"],[aria-label="${UI Name}"])`)

(async () => {
  tails.defined(`UI Name`);
})();

.set()

(async () => {
  tails.set({
    property: {
      path: "value",
    },
    attribute: {
      key: "value",
    },
  });
})();

.setProperty()

(async () => {
  tails.setProperty({
    path: "value",
  });
})();

.setAttribute()

(async () => {
  tails.setAttribute({
    key: "value",
  });
})();

.push()

(async () => {
  tails.push({
    property: {
      path: "value",
    },
    attribute: {
      key: "value",
    },
  });
})();

.call()

(async () => {
  tails.call(`click`, null);
})();

.event()

(async () => {
  tails.event({
    eventObject: "path",
    eventType: "event type",
    eventArgs: "any",
  });
})();

.focus()

move into Shadow DOM, iframe

(async () => {
  tails.focus(`Property`, (reset = false));
})();

.capture()

(async () => {
  tails.capture(
    (selector = `CSS Selector or XPath`),
    (object = {
      fileName: "capture",
      format: "png",
      quality: 100,
    })
  );
})();

.key()

Argument Reference
https://chromedevtools.github.io/devtools-protocol/1-3/Input/#method-dispatchKeyEvent

(async () => {
  tails.key({
    type: "keyDown",
    windowsVirtualKeyCode: 65,
  });
})();

.open()

(async () => {
  tails.open("URL");
})();

.sleep()

sleep millisecond

(async () => {
  tails.sleep(1000);
})();

.file()

(async () => {
  tails.file([
    {
      name: "FileName",
      type: "MIME",
      blob: [...Uint8Array],
      object: "Uint8Array",
    },
  ]);
})();

.getProperties()

(async () => {
  let properties = await tails.getProperties(`CSS Selector or XPath`);
})();

.run()

run Tails

(async () => {
  await tails.run({ sleep: 100 });
})();

.runTillNextOnLoad()

run Tails and await till next window.onload

(async () => {
  await tails.runTillNextOnLoad({ sleep: 100 });
})();

BlueFoxScript[tabId].addEventListeners()

(async () => {
  let listener_info = await tab.addEventListeners(
    `[data-testid="bit-1"]`,
    "click",
    async (object) => {
      log(object);

      // Property
      object.event;
      object.properties;
      listener_info[0].uuid == object.event.uuid;
      listener_info[0].selector == object.event.target;
    }
  );
  // Property
  listener_info[0].uuid;
  listener_info[0].selector;
})();

BlueFoxScript[tabId].dispatchScript()

(async () => {
  await tab.dispatchScript(() => {
    window.alert("^.,.^ BlueFox");
  });
})();

BlueFoxScript[tabId].dispatchScriptTillTrue()

(async () => {
  await tab.dispatchScriptTillTrue(() => {
    return true;
  }, (max_polling = 5000));
})();

BlueFoxScript[tabId].dispatchAction()

run Tails

(async () => {
  await tab.dispatchAction(tails.tail);
})();

BlueFoxScript[tabId].captureScreenshot()

(async () => {
  let base64_png_image = await tab.captureScreenshot(
    (config = {
      format: "png",
      captureBeyondViewport: true,
    })
  );
})();

BlueFoxScript[tabId].close()

(async () => {
  await tab.close();
})();

BlueFoxScript[tabId].reload()

(async () => {
  await tab.reload();
})();

BlueFoxScript[tabId].cookie.get()

https://developer.chrome.com/docs/extensions/reference/api/cookies#method-getAll

(async () => {
  await tab.cookie.get({ domain: "domain" });
})();

BlueFoxScript[tabId].cookie.remove()

https://developer.chrome.com/docs/extensions/reference/api/cookies#method-remove

(async () => {
  await tab.cookie.remove({ name: "name" });
})();

BlueFoxScript[tabId].cookie.set()

https://developer.chrome.com/docs/extensions/reference/api/cookies#method-set

(async () => {
  await tab.cookie.set({ name: "name", value: "value" });
})();

Step by Step

1. Inside async function

(async () => {
  // Write your code here
})();

2. Initialize

(async () => {
  let blueFoxScript = await new BlueFoxScript();
})();

3. Open target window

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  let tab = await blueFoxScript.createWindow("https://www.google.com");
})();

4. Initialize tails

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  let tab = await blueFoxScript.createWindow("https://www.google.com");

  let tails = await tab.tails();
})();

5. Preparing tails

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  let tab = await blueFoxScript.createWindow("https://www.google.com");

  let tails = await tab.tails();
  tails
    .target("textarea")
    .setProperty({ value: "^.,.^ BlueFox" })
    .target("[name='btnK'][tabindex='0']")
    .call("click", null);
})();

6. Run tails

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  let tab = await blueFoxScript.createWindow("https://www.google.com");

  let tails = await tab.tails();
  tails
    .target("textarea")
    .setProperty({ value: "^.,.^ BlueFox" })
    .target("[name='btnK'][tabindex='0']")
    .call("click", null);

  await tails.run({ sleep: 50 });
})();

7. Run script and get result

(async () => {
  let blueFoxScript = await new BlueFoxScript();

  let tab = await blueFoxScript.createWindow("https://www.google.com");

  let tails = await tab.tails();
  tails
    .target("textarea")
    .setProperty({ value: "^.,.^ BlueFox" })
    .target("[name='btnK'][tabindex='0']")
    .call("click", null);

  await tails.runTillNextOnLoad({ sleep: 50 });

  let search_result = await tab.dispatchScript(() => {
    return [
      ...document.querySelectorAll("#search :is(a[data-jsarwt='1'],a[jsname])"),
    ]
      .filter((_) => {
        return _.querySelector("h3");
      })
      .map((_) => {
        return {
          href: _.href,
          title: _.querySelector("h3").textContent,
        };
      });
  });
  window.alert(JSON.stringify(search_result.result.value, null, 4));
})();