1const Apify = require('apify');
2
3Apify.main(async () => {
4 const input = await Apify.getValue('INPUT');
5
6 if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
7
8 console.log('Launching Puppeteer...');
9 const browser = await Apify.launchPuppeteer();
10
11 console.log(`Opening URL: ${input.url}`);
12 const page = await browser.newPage();
13 await page.goto(input.url);
14
15 console.log(`Starting a CDP session`);
16 const client = await page.target().createCDPSession();
17 await client.send('DOM.enable');
18 await client.send('CSS.enable');
19
20 console.log('Fetching list of DOM nodes');
21 const nodes = (await client.send('DOM.getFlattenedDocument')).nodes;
22
23 console.log(`Analyzing CSS for each of the ${nodes.length} node`);
24 for (let i=0; i<nodes.length; i++) {
25 const node = nodes[i];
26 if (node.nodeType === 1) {
27 node.matchedStyle = await client.send('CSS.getMatchedStylesForNode', {
28 nodeId: node.nodeId
29 });
30 }
31 };
32
33 await Apify.setValue('OUTPUT', nodes);
34});