locator/index2.js

67 lines
1.8 KiB
JavaScript

#!/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 <path-to-HTML-file>");
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();