-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgai.js
More file actions
executable file
·54 lines (44 loc) · 1.49 KB
/
Copy pathgai.js
File metadata and controls
executable file
·54 lines (44 loc) · 1.49 KB
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
#!/usr/bin/env node
const { NodeHtmlMarkdown } = require('node-html-markdown');
const { diffWords } = require('diff');
const url = 'https://www.google.com/search?udm=50&hl=en&q=' + process.argv[2];
const textMessage = '.pWvJNd';
const footer = '[data-xid="Gd7Hsc"]';
const timer = 500;
const timeout = 30000;
function getDiffMarkdown(previous, currrent) {
const diffResult = diffWords(previous, currrent);
let output = '';
diffResult.forEach((part) => {
if (part.added) {
output += part.value;
}
});
return output
}
async function main() {
const { launch } = await import('cloakbrowser');
const browser = await launch({ headless: true });
const page = await browser.newPage();
// Start page
await page.goto(url, { waitUntil: 'domcontentloaded' });
// Get reply
let previousMarkdown= '';
while (true) {
const stop = page.locator(footer);
const currentHtml = await page.locator(textMessage).evaluate((el) => {
const clone = el.cloneNode(true);
clone.querySelectorAll('[data-xid="Gd7Hsc"], .rBl3me').forEach(node => node.remove());
return clone.innerHTML;
});
const currentMarkdown = NodeHtmlMarkdown.translate(currentHtml);
if (currentMarkdown !== previousMarkdown ) {
markdown = getDiffMarkdown(previousMarkdown, currentMarkdown)
markdown && console.log(markdown);
previousMarkdown= currentMarkdown;
}
await stop.count() > 0 && process.exit(0)
await page.waitForTimeout(timer);
}
}
main().catch(console.error);