pruneHtml.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. #! /usr/bin/env node
  2. // This can be called with e.g. `test/pruneHtml.js path/to/the/file.html`
  3. // The output file should not be prettified since it would mess up with line breaks and change
  4. // the value of both "textContent" / "innerHTML".
  5. const fs = require("fs");
  6. const url = require("url");
  7. const jsdom = require("jsdom");
  8. const {JSDOM} = jsdom;
  9. const [_1, _2, filePath] = process.argv;
  10. const content = fs.readFileSync(filePath);
  11. const dom = new JSDOM(content);
  12. function removeTrackers(link) {
  13. const u = new URL(link);
  14. u.searchParams.delete("courseClaim");
  15. u.searchParams.delete("eBP");
  16. u.searchParams.delete("refId");
  17. u.searchParams.delete("trackingId");
  18. u.searchParams.delete("destRedirectURL");
  19. return url.format(u);
  20. }
  21. dom.window.document.querySelectorAll("code").forEach(el => el.remove());
  22. dom.window.document.querySelectorAll("meta").forEach(el => el.remove());
  23. dom.window.document.querySelectorAll("script").forEach(el => el.remove());
  24. dom.window.document.querySelectorAll("a").forEach(el => el.href = removeTrackers(el.href));
  25. fs.writeFileSync(filePath, dom.window.document.documentElement.innerHTML);