Skip to content

Commit

Permalink
✨ Add remove words helper
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Apr 15, 2020
1 parent 372442e commit 3fb4840
Showing 1 changed file with 51 additions and 38 deletions.
89 changes: 51 additions & 38 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import got from "got";
import { readFile } from "fs-extra";
import { join } from "path";
import ms from "ms";
import natural from "natural";
// import finder from "@medv/finder";

/**
Expand Down Expand Up @@ -51,6 +50,13 @@ const _puppet = async (commands: string[]) => {

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const lastWord = (text: string) => text.split(" ")[text.split(" ").length - 1];
const removeWords = (text: string, ...words: string[]) =>
text
.split(" ")
.filter((i) => !words.includes(i.trim()))
.join(" ")
.replace(/\s\s+/g, " ")
.trim();
const waitForNavigationOrTimeout = (
page: Page,
timeout: number
Expand All @@ -68,52 +74,59 @@ const _command = async (command: string, page: Page): Promise<any> => {
if (command === "wait for navigation")
return waitForNavigationOrTimeout(page, 10000);
if (command.startsWith("wait"))
return wait(ms(command.replace(/wait|for/gi, "").trim()));
return wait(ms(removeWords(command, "wait", "for")));
if (
command.startsWith("navigate") ||
command.startsWith("go") ||
command.startsWith("open")
) {
const query = command.replace(/navigate|go|open|to|page|url/gi, "").trim();
const query = removeWords(
command,
"navigate",
"go",
"open",
"to",
"page",
"url"
);
const url = query.startsWith("http") ? query : `http://${query}`;
return page.goto(url);
}
if (command.includes("screenshot")) return;
if (command.includes("save")) return;
// for await (const event of ["click"]) {
// if (command.startsWith(event)) {
// const query = command.replace(/click| on /gi, "").trim();
// let selector = "*";
// const type = lastWord(query);
// if (type === "link") selector = "a";
// if (type === "button") selector = "button";
// if (type === "input") selector = "input";
// if (type === "area") selector = "area";
// if (type === "label") selector = "label";
// if (type === "textarea") selector = "textarea";
// if (type === "image") selector = "img";
// const text = query
// .replace(/link|button|input|area|label|textarea|image/gi, "")
// .trim();
// console.log("GOT ELEMENT 1", text);
// let elementToClick: HTMLElement | undefined = undefined;
// await page.$$eval(selector, (elements) => {
// elements.forEach((element) => {
// if (
// (element as HTMLElement).innerText
// .toLocaleLowerCase()
// .includes(text)
// )
// elementToClick = element as HTMLElement;
// });
// });
// if (elementToClick) {
// console.log("GOT ELEMENT", elementToClick);
// (elementToClick as HTMLElement).click();
// }
// return;
// }
// throw new Error(`Element not found: ${text}`);
// }
for await (const event of ["click"]) {
if (command.startsWith(event)) {
// const query = command.replace(/click| on /gi, "").trim();
// let selector = "*";
// const type = lastWord(query);
// if (type === "link") selector = "a";
// if (type === "button") selector = "button";
// if (type === "input") selector = "input";
// if (type === "area") selector = "area";
// if (type === "label") selector = "label";
// if (type === "textarea") selector = "textarea";
// if (type === "image") selector = "img";
// const text = query
// .replace(/link|button|input|area|label|textarea|image/gi, "")
// .trim();
// console.log("GOT ELEMENT 1", text);
// let elementToClick: HTMLElement | undefined = undefined;
// await page.$$eval(selector, (elements) => {
// elements.forEach((element) => {
// if (
// (element as HTMLElement).innerText
// .toLocaleLowerCase()
// .includes(text)
// )
// elementToClick = element as HTMLElement;
// });
// });
// if (elementToClick) {
// console.log("GOT ELEMENT", elementToClick);
// (elementToClick as HTMLElement).click();
// }
// return;
}
}
throw new Error(`Command not understood: ${command}`);
};

0 comments on commit 3fb4840

Please sign in to comment.