-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselector.mjs
53 lines (52 loc) · 1.55 KB
/
selector.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import puppeteer from "puppeteer";
import iterator from "./iterator.js";
var docs = [];
iterator(function (srcname, name, variant) {
docs.push(name);
});
var browser = await puppeteer.launch({ headless: "new" });
var exit_code = 0;
for (var name of docs) {
var heading = undefined;
console.group(name);
var page = await browser.newPage();
await page.goto(`${import.meta.dirname}/../docs/${name}/${name}.html`, {
waitUntil: "networkidle2",
});
for (var r of await Promise.all(
(
await Promise.all(
(await page.$$(process.argv[2])).map(async function (elem) {
var elems = process.argv[3]
? await elem.$$("xpath/" + process.argv[3])
: [elem];
if (elems.length > 0) exit_code = 1;
return elems.map((elem) =>
elem.evaluate(function (e) {
return {
heading: document.evaluate(
"preceding::*[self::h1|self::h2|self::h3|self::h4|self::h5|self::h6][1]",
e,
() => {},
XPathResult.FIRST_ORDERED_NODE_TYPE,
).singleNodeValue.textContent,
match:
e.nodeType === Node.ELEMENT_NODE ? e.outerHTML : e.nodeValue,
};
}),
);
}),
)
).flat(),
)) {
if (r.heading !== heading) {
if (heading) console.groupEnd();
console.group(r.heading);
heading = r.heading;
}
console.log(r.match);
}
if (heading) console.groupEnd();
console.groupEnd();
}
process.exit(exit_code);