-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomcheck.js
100 lines (87 loc) · 2.8 KB
/
domcheck.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const puppeteer = require("puppeteer");
const fs = require("fs-extra");
const path = require("path");
const csv = require("fast-csv");
const some = require("lodash/some");
const last = require("lodash/last");
const isEmpty = require("lodash/isEmpty");
const isNil = require("lodash/isNil");
const merge = require("lodash/merge");
function query(url, waitForSelector, onDocument) {
return puppeteer.launch({ headless: true }).then(browser => {
return browser.newPage().then(page => {
return page
.goto(url)
.then(() => page.waitForSelector(waitForSelector))
.then(() => page.evaluate(onDocument, waitForSelector))
.finally(() => browser.close());
});
});
}
function getHistory(dir, filename) {
const filepath = path.join(__dirname, dir, filename);
console.log("loading logs from", filepath);
if (!fs.existsSync(filepath)) return Promise.resolve([]);
return new Promise((resolve, reject) => {
const rows = [];
csv
.parseFile(filepath, { headers: true })
.on("error", error => reject(error))
.on("data", row => rows.push(row))
.on("end", () => resolve(rows));
});
}
function setHistory(dir, filename, rows) {
const filepath = path.join(__dirname, dir, filename);
return fs.ensureFile(filepath).then(
() =>
new Promise((resolve, reject) => {
csv
.writeToPath(filepath, rows, { headers: true })
.on("error", error => reject(error))
.on("end", () => resolve());
})
);
}
const defaultConfig = {
historyDir: "history",
onDocument: (selector) => {
const nodeList = document.querySelectorAll(selector);
return nodeList[0] && nodeList[0].innerText.trim();
}
};
function domcheck(config) {
const {
name,
url,
waitForSelector,
onDocument,
history,
historyDir,
notify
} = merge(defaultConfig, config);
const historyPath = history || `${name}.csv`;
if (some([name, url, onDocument, waitForSelector, notify], x => isNil(x))) {
throw new Error("missing parameters");
}
return query(url, waitForSelector, onDocument)
.then(text => {
if (isEmpty(text)) throw new Error(`query result is empty`);
return getHistory(historyDir, historyPath).then(entries => {
const lastEntry = last(entries);
console.log("last entry:", lastEntry);
entries.push({ text, timestamp: Date.now() });
const promises = [setHistory(historyDir, historyPath, entries)];
if (!lastEntry || (lastEntry && lastEntry.text !== text)) {
console.log("notifying change:", text);
promises.unshift(notify(name, text, null));
}
return Promise.all(promises);
});
})
.catch(error => {
console.log("Error:", error);
return notify(name, null, error);
});
}
module.exports = domcheck;