pruneHtml.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  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 options = {encoding: "utf8"};
  10. const [_1, _2, filePath] = process.argv;
  11. const content = fs.readFileSync(filePath, options);
  12. const dom = new JSDOM(content);
  13. function removeTrackers(link) {
  14. const u = new URL(link);
  15. u.searchParams.delete("courseClaim");
  16. u.searchParams.delete("eBP");
  17. u.searchParams.delete("refId");
  18. u.searchParams.delete("trackingId");
  19. u.searchParams.delete("destRedirectURL");
  20. return url.format(u);
  21. }
  22. dom.window.document.querySelectorAll("code").forEach(el => el.remove());
  23. dom.window.document.querySelectorAll("meta").forEach(el => el.remove());
  24. dom.window.document.querySelectorAll("script").forEach(el => el.remove());
  25. dom.window.document.querySelectorAll("a").forEach(el => el.href = removeTrackers(el.href));
  26. fs.writeFileSync(filePath, dom.window.document.documentElement.innerHTML, options);