Skip to content

JS (web)

alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Dec 3, 2021 · 44 revisions

Inline blocking sleep()

await new Promise(resolve => setTimeout(resolve, 500)); // sleep for 500 ms

Stale closure value example

let x = 7;
const foo = (val) => {
  return () => { console.log(val); };
}
new_func = foo(x);
new_func(); // => 7
x = 42;
new_func(); // STILL => 7!!!!

Simulate BeforeUnloadEvent (user closing tab/window)

Do this instead of selenium_driver.browser.close so the window doesn't actually close.

let event = document.createEvent('BeforeUnloadEvent');
event.initEvent('beforeunload', true, true);
let userPromptedBeforeLeavingPage = !dispatchEvent(event);

Dump entire body html

new XMLSerializer().serializeToString(document);

Jest

Mocking async export default function

import * as ModuleName from "path/moduleName";

let resolver;
const moduleNameSpy = jest.spyOn(
  ModuleName,
  "default"
).mockReturnValue(
  new Promise(
    (resolve, reject) => {
      resolver = resolve;
    }
  )
);

expect(moduleNameSpy).toHaveBeenCalledWith(
  arg1,
  expect.any()
);

await act(
  async () => {
    resolver(someReturnValue);
  }
);

Mocking third-party module

import { v4 } from 'uuid';

jest.mock('uuid', () => ({
  v4: jest.fn()
}));

beforeEach(() => {
  v4.mockReset();
});

describe("Mocking uuid", () => {
  it("mock uuid twice (jk three times)", () => {
    v4
    .mockReturnValueOnce("FIRST TIME")
    .mockReturnValueOnce("SECOND TIME")
    .mockReturnValueOnce("THIRD TIME")

    const result = [v4(), v4()];
    expect(result).toEqual(["FIRST TIME", "SECOND TIME"]);
  });

  it("DO IT AGAIN", () => {
    v4.mockReturnValueOnce("BLURST TIME")
    expect(v4()).toEqual("BLURST TIME");
  })
});

Console recipes

Get specific column from table:

$(`table.SOME_CLASS tr td:nth-child(${INDEX_OF_COLUMN})`).each(function(idx, e) {console.log(e.innerText)})

Delete entire page of reddit comments:

// Uses a basic for loop instead of $.each() in order to delay clicks by awaiting promises 
for (index = 0; index < 25; index++) {
    console.log("Erasing " + index);
    $($("a.edit-usertext")[index]).trigger("click");
    $($(".usertext-edit textarea")[index]).text("🤡");
    $($(".usertext-buttons button.save")[index]).click();
    console.log("Erased " + index);
    await new Promise(resolve => setTimeout(resolve, 800));
    console.log("Deleting " + index);
    $($(".del-button .togglebutton")[0]).trigger("click");
    $($(".del-button .yes")[0]).trigger("click");
    console.log("Deleted " + index);	
    await new Promise(resolve => setTimeout(resolve, 800));
}
location.reload();

Calculate PnL on TDAmeritrade transaction search page:

alert("PnL: " + $("iframe#main").contents().find(".posChange, .negChange").map( function() { return this.innerText.replace(',', ''); } ).toArray().reduce(function(tot, num) { return parseFloat(tot) + parseFloat(num); }))

Download entire NHIS dataset:

// Uses a basic for loop instead of $.each() in order to delay clicks by awaiting timeouts 

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

currentVariableCountString = $($("#dcs .number")[0]).text().trim() || "0";
currentVariableCount = parseInt(currentVariableCountString);

buttons = $(".variablesList .checkbox-column .add_variable:visible");
buttonCount = buttons.size();

targetVariableCount = buttonCount + currentVariableCount;
console.log("\n\n\n=====> CURRENT: " + currentVariableCount + ", NEW: " + buttonCount + ", SUM: " + targetVariableCount);

for (index = 0; index < buttonCount; index++) {
    $(buttons[index]).trigger("click");
    await sleep(200);
}

noUpdateWaitCount = 0;

while (noUpdateWaitCount < 8) {
    newVariableCountString = $($("#dcs .number")[0]).text().trim() || "0";
    newVariableCount = parseInt(newVariableCountString);

    // console.log("====> CURRENT: " + currentVariableCount + ", NEW: " + newVariableCount)

    if (newVariableCount == targetVariableCount) {
        console.log("\n\n=====> 👍👍👍: GREAT SUCCESS! ON TO THE NEXT PAGE...\n\n");
        $("a.next_page")[0].click();
        break;
    }

    if (newVariableCount == currentVariableCount) {
        noUpdateWaitCount++;
    } else {
        currentVariableCount = newVariableCount;
        noUpdateWaitCount = 0;
    }

    await sleep(1000);
}

if (noUpdateWaitCount >= 8) {
    console.log("\n\n=====> 🔥🔥🔥: DIDN'T ADD ALL VARIABLES! TRY AGAIN!\n\n");
    location.reload();
}

Cookie Clicker lolhax

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

stop = false;

while (stop != true) {
    document.getElementById("bigCookie").click();
    await sleep(.0001);
}
Clone this wiki locally