rep1y.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const sketch = document.getElementById('sketch');
  2. const htmlDraft = document.getElementById('htmlCode');
  3. const cssDraft = document.getElementById('cssCode');
  4. const logger = document.getElementById('logger');
  5. const sterilizer = DOMPurify;
  6. const a11y = axe;
  7. function saveSketch() {
  8. let skt = {html: sterilizer.sanitize(htmlDraft.value), css: cssDraft.value};
  9. location.hash = encodeURIComponent(window.btoa(JSON.stringify(skt)));
  10. }
  11. function openSketch() {
  12. if(location.hash.length > 1) {
  13. let savedSketch = JSON.parse(window.atob(decodeURIComponent(location.hash.substr(1))));
  14. htmlDraft.value = sterilizer.sanitize(savedSketch.html);
  15. cssDraft.value = savedSketch.css;
  16. }
  17. }
  18. function renderSketch() {
  19. sketch.srcdoc = ("<!DOCTYPE html><html lang='en'><head>" +
  20. "<meta charset='utf-8'/><style>" +
  21. cssDraft.value +
  22. "</style></head><body>" +
  23. sterilizer.sanitize(htmlDraft.value) +
  24. "<\/body><\/html>");
  25. }
  26. function printA11yResults() {
  27. a11y.run(sketch.contentWindow.document, function(err, skt) {
  28. logger.value = "a11y> ";
  29. skt.violations.map(v => logger.value += JSON.stringify(v, null, 2));
  30. });
  31. }
  32. window.onload = function() {
  33. openSketch();
  34. renderSketch();
  35. printA11yResults();
  36. };
  37. document.body.oninput = function() {
  38. renderSketch();
  39. printA11yResults();
  40. saveSketch();
  41. };