forked from raylexlee/SchoolProgramHKSSF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetlinks.js
More file actions
executable file
·52 lines (42 loc) · 1.36 KB
/
Copy pathgetlinks.js
File metadata and controls
executable file
·52 lines (42 loc) · 1.36 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
#!/usr/bin/env node
const CDP = require('chrome-remote-interface');
const chromeLauncher = require('chrome-launcher');
function launchChrome(headless=true) {
return chromeLauncher.launch({
// port: 9222, // Uncomment to force a specific port of your choice.
chromeFlags: [
'--window-size=412,732',
'--disable-gpu',
headless ? '--headless' : ''
]
});
}
(async function() {
const chrome = await launchChrome();
const protocol = await CDP({port: chrome.port});
// Extract the DevTools protocol domains we need and enable them.
// See API docs: https://chromedevtools.github.io/devtools-protocol/
const {Page, Runtime} = protocol;
await Promise.all([Page.enable(), Runtime.enable()]);
Page.navigate({url: process.argv[2]});
// Wait for window.onload before doing stuff.
Page.loadEventFired(async () => {
// const js = "document.getElementsByTagName('a')";
const js = `
b=document.getElementsByTagName('a').length;
`;
// Evaluate the JS expression in the page.
const res_js = await Runtime.evaluate({expression: js});
let i;
for (i=0; i<res_js.result.value; i++) {
const js = `
b=document.getElementsByTagName('a')[${i}];
c=b.innerText+' '+b.href;
`;
const result = await Runtime.evaluate({expression: js});
console.log(result.result.value);
}
protocol.close();
chrome.kill(); // Kill Chrome.
});
})();