1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const puppeteer_1 = require("puppeteer");
|
7 | const signale_1 = require("signale");
|
8 | const got_1 = __importDefault(require("got"));
|
9 | const fs_extra_1 = require("fs-extra");
|
10 | const path_1 = require("path");
|
11 | const navigation_1 = require("./commands/navigation");
|
12 | const save_page_as_1 = require("./commands/save-page-as");
|
13 | const files_1 = require("./commands/files");
|
14 | const timers_1 = require("./commands/timers");
|
15 | const mouse_1 = require("./commands/mouse");
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | exports.puppet = async (commandsOrFile) => {
|
24 | if (typeof commandsOrFile === "string") {
|
25 | if (commandsOrFile.startsWith("https://") ||
|
26 | commandsOrFile.startsWith("http://")) {
|
27 | const commands = await got_1.default.get(commandsOrFile);
|
28 | return _puppet(commands.body.split("\n"));
|
29 | }
|
30 | const commands = await fs_extra_1.readFile(path_1.join(".", commandsOrFile), "utf8");
|
31 | return _puppet(commands.split("\n"));
|
32 | }
|
33 | else if (Array.isArray(commandsOrFile)) {
|
34 | return _puppet(commandsOrFile);
|
35 | }
|
36 | throw new Error("Argument must be a string or an array of strings");
|
37 | };
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const _puppet = async (commands) => {
|
43 | commands = commands.map((i) => i.toLocaleLowerCase().trim()).filter((i) => i);
|
44 | signale_1.start("Starting Puppet");
|
45 | const browser = await puppeteer_1.launch();
|
46 | const page = await browser.newPage();
|
47 | let lastResult = undefined;
|
48 | for await (const command of commands) {
|
49 | lastResult = await _command(command, page, lastResult);
|
50 | }
|
51 | const result = { commands, url: page.url() };
|
52 | await browser.close();
|
53 | signale_1.success("Completed Puppet commands");
|
54 | return result;
|
55 | };
|
56 | const _command = async (command, page, lastResult) => {
|
57 | signale_1.debug("Running command", command);
|
58 | if (command.startsWith("go") ||
|
59 | command.startsWith("open") ||
|
60 | command.startsWith("navigate"))
|
61 | return navigation_1.navigateTo(command, page, lastResult);
|
62 | if ((command.startsWith("save") || command.startsWith("get")) &&
|
63 | command.endsWith(" pdf"))
|
64 | return save_page_as_1.saveAsPdf(command, page, lastResult);
|
65 | if ((command.startsWith("save") || command.startsWith("get")) &&
|
66 | command.endsWith(" html"))
|
67 | return save_page_as_1.saveAsHtml(command, page, lastResult);
|
68 | if (command.startsWith("save"))
|
69 | return files_1.saveToFile(command, page, lastResult);
|
70 | if (command.includes("screenshot"))
|
71 | return save_page_as_1.screenshot(command, page, lastResult);
|
72 | if (command.startsWith("wait for"))
|
73 | if (command.includes("navigation"))
|
74 | return timers_1.waitForNavigation(command, page, lastResult);
|
75 | else
|
76 | return timers_1.waitForTime(command, page, lastResult);
|
77 | if (command.startsWith("move") ||
|
78 | (command.startsWith("click") &&
|
79 | (command.includes("point") ||
|
80 | command.includes("coordinate") ||
|
81 | command.includes("mouse"))))
|
82 | return mouse_1.triggerMouseClickMove(command, page, lastResult);
|
83 | throw new Error(`Command not understood: ${command}`);
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
\ | No newline at end of file |