Runs with Node.js: node count-tags.js
// Does a page use semantic elements, or only <div>s? This program counts them in two saved pages.
// In the browser, the same check is one line in the Console (see snippet.js).
const fs = require("node:fs");
const path = require("node:path");
const tags = ["header", "nav", "main", "section", "article", "aside", "footer", "div"];
for (const file of ["semantic.html", "div-soup.html"]) {
const page = fs.readFileSync(path.join(__dirname, file), "utf8");
const counts = tags.map((tag) => `${tag}: ${(page.match(new RegExp(`<${tag}[\\s>]`, "g")) || []).length}`);
console.log(`${file} -> ${counts.join(", ")}`);
}
Recorded output$ node count-tags.js
semantic.html -> header: 1, nav: 1, main: 1, section: 0, article: 1, aside: 1, footer: 1, div: 0
div-soup.html -> header: 0, nav: 0, main: 0, section: 0, article: 0, aside: 0, footer: 0, div: 10
Paste into the browser's Console
// Paste into the Console of any website: how many of each element does it use?
console.table(["header", "nav", "main", "section", "article", "aside", "footer", "div"]
.map((tag) => ({ tag, count: document.querySelectorAll(tag).length })));