codeviewer.js 660 B

12345678910111213141516171819202122232425262728293031
  1. let pageH=`<!DOCTYPE html><html><head><style>pre{counter-reset: line;white-space:pre-wrap}
  2. pre>code{counter-increment: line;}
  3. pre>code:before {
  4. content: counter(line);
  5. width: 2em;
  6. margin-right: 0.5em;
  7. color: gray;
  8. }
  9. </style></head><body><pre>`;
  10. let pageT="</pre></body></html>";
  11. function gen(text) {
  12. let d=window.open('').document;
  13. d.write(pageH);
  14. let t=text.replaceAll('<','&lt;');
  15. t = t.replace(/^(.*)$/gm, '<code> $1 </code>');
  16. d.write(t);
  17. d.write(pageT);
  18. d.close();
  19. }
  20. if(document.body){
  21. var r=new XMLHttpRequest;
  22. r.onload=function(){
  23. var t=r.responseText;
  24. gen(t);
  25. }
  26. r.open("GET",location.href,!0);
  27. r.send(null);
  28. }