Skip to content

Instantly share code, notes, and snippets.

@alan-andrade
Last active March 31, 2021 22:01
Show Gist options
  • Save alan-andrade/31e4d2cacb5436e33a97e1d3e424c8c7 to your computer and use it in GitHub Desktop.
Save alan-andrade/31e4d2cacb5436e33a97e1d3e424c8c7 to your computer and use it in GitHub Desktop.

Revisions

  1. alan-andrade revised this gist Mar 31, 2021. 1 changed file with 33 additions and 10 deletions.
    43 changes: 33 additions & 10 deletions next.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,34 @@
    javascript:(function() {
    document.onkeypress = (e) => {
    if (e.key === "n" || e.key === "f") {
    const xpath = "//button[contains(text(),'Save + Next Class')]";
    const element = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    element.click();
    }
    };

    alert("Hotkeys enabled. Press F or N to move forward.");
    javascript:(() => {
    const contains = (text) => `contains(text(),'${text}')`;
    const conditions = (textOrArray) => textOrArray instanceof Array ? textOrArray.map(contains) : [contains(textOrArray)];
    const btnXpath = (text) => `//button[${conditions(text).join(" or ")}]`;
    const divXpath = (text) => `//div[${contains(text)}]`;
    const findEl = (xpath) => document.evaluate(
    xpath,
    document,
    null,
    XPathResult.FIRST_ORDERED_NODE_TYPE,
    null).singleNodeValue;

    const isWriting = () => findEl(`//div[@role="listbox"]`);

    const back = () => findEl(btnXpath("Back")).click();
    const flag = () => findEl(btnXpath(["Flag Class", "Unflag Class"])).click();
    const skip = () => findEl(btnXpath("Skip")).click();
    const next = () => findEl(btnXpath("Save + Next")).click();
    const tag = () => findEl(divXpath("Choose an option")).click();

    const keyMappings = {
    "a": back,
    "s": flag,
    "d": skip,
    "f": next,
    "w": tag
    };

    const triggerFn = (key) => keyMappings[key]();
    const guard = () => isWriting() ? () => {} : triggerFn;

    document.onkeypress = (e) => guard()(e.key);
    alert("a = Back\ns = Flag/Unflag\nd = Skip\nf = Next\nw = Write");
    })()
  2. alan-andrade revised this gist Mar 31, 2021. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions next.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    javascript:(function() {
    document.onkeypress = (e) => {
    if (e.key !== "n" || e.key !== "f") { return }
    const xpath = "//button[contains(text(),'Save + Next Class')]";
    const element = document.evaluate(xpath, window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    element.click();
    if (e.key === "n" || e.key === "f") {
    const xpath = "//button[contains(text(),'Save + Next Class')]";
    const element = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    element.click();
    }
    };
    alert("Hotkeys enabled. Press F or N to move forward.");

    alert("Hotkeys enabled. Press F or N to move forward.");
    })()
  3. alan-andrade revised this gist Mar 31, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions next.js
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,6 @@ document.onkeypress = (e) => {
    const xpath = "//button[contains(text(),'Save + Next Class')]";
    const element = document.evaluate(xpath, window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    element.click();
    }
    alert("Hotkeys enabled. Press F or N to move forward.")
    };
    alert("Hotkeys enabled. Press F or N to move forward.");
    })()
  4. alan-andrade revised this gist Mar 31, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion next.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    javascript:(function() {
    document.onkeypress = (e) => {
    if (e.key !== "n") { return }
    if (e.key !== "n" || e.key !== "f") { return }
    const xpath = "//button[contains(text(),'Save + Next Class')]";
    const element = document.evaluate(xpath, window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    element.click();
    }
    alert("Hotkeys enabled. Press F or N to move forward.")
    })()
  5. alan-andrade revised this gist Mar 31, 2021. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions next.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    javascript:(function(window) {
    window.document.onkeypress = (e) => { if (e.key !== "n") { return } const xpath = "//button[contains(text(),'Save + Next Class')]"; const element = document.evaluate(xpath, window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; element.click(); }
    })(window)
    javascript:(function() {
    document.onkeypress = (e) => {
    if (e.key !== "n") { return }
    const xpath = "//button[contains(text(),'Save + Next Class')]";
    const element = document.evaluate(xpath, window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    element.click();
    }
    })()
  6. alan-andrade revised this gist Mar 31, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions next.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    function(window) {
    javascript:(function(window) {
    window.document.onkeypress = (e) => { if (e.key !== "n") { return } const xpath = "//button[contains(text(),'Save + Next Class')]"; const element = document.evaluate(xpath, window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; element.click(); }
    }(window)
    })(window)
  7. alan-andrade created this gist Mar 31, 2021.
    3 changes: 3 additions & 0 deletions next.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    function(window) {
    window.document.onkeypress = (e) => { if (e.key !== "n") { return } const xpath = "//button[contains(text(),'Save + Next Class')]"; const element = document.evaluate(xpath, window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; element.click(); }
    }(window)