From accb2d4f3a21c8d7a9145a2981ce44933680a754 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 17 Feb 2023 17:26:20 +0100 Subject: [PATCH] Working on a hierarchical analyser --- index2.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 index2.js diff --git a/index2.js b/index2.js new file mode 100644 index 0000000..fc27b87 --- /dev/null +++ b/index2.js @@ -0,0 +1,66 @@ +#!/usr/bin/env node + +const fs = require('fs').promises; +const process = require('process'); +const puppeteer = require('puppeteer'); + +async function main() { + + if (process.argv[2] === undefined) { + console.error("This program expects an argument."); + console.error("USAGE: locator "); + process.exit(1); + } + + // Path to the HTML file to analyse (given as relative path from current directory) + // We need the full path so that puppeteer is able to access it + const path = process.argv[2].startsWith('/') ? process.argv[2] : process.cwd() + '/' + process.argv[2]; + + // Check that the file exists + try { + await fs.access(path, fs.constants.F_OK); + } catch (e) { + console.error("No such file: " + path); + process.exit(1); + } + + // Size of the rendering of the web page + const size = { width: 1280, height: 720 }; + + // Initialize browser + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.setViewport(size); + await page.goto("file://" + path); + + let root = await page.$("#\\31"); + let hierarchy = {}; + + await analyseElement(root); + + await browser.close(); + +} + + +async function analyseElement(element, hierarchy, tabs = '', stop = false) { + let tagAttr = await element.getProperty("tagName"); + let tagName = await tagAttr.jsonValue(); + + let classAttr = await element.getProperty("className"); + let className = await classAttr.jsonValue(); + + let box = await element.boundingBox(); + + console.log(tabs + tagName + ' "' + className + '" ' + JSON.stringify(box)); + + let children = await element.$$('> *'); + + for (let child of children) { + await analyseElement(child, hierarchy, tabs + ' ', true); + } +} + + + +main();