txt.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <!DOCTYPE html>
  2. <html dir="ltr" mozdisallowselectionprint>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>预览txt</title>
  6. <script src="./utils/flexible.js"></script>
  7. <script src="./utils/getQueryVariable.js"></script>
  8. </head>
  9. <style type="text/css">
  10. *{padding: 0;margin: 0;}
  11. body {
  12. height: 100vh;
  13. width: 100vw;
  14. }
  15. #container {
  16. width: 100%;
  17. height: 100%;
  18. font-size: 0.28rem;
  19. }
  20. </style>
  21. <script>
  22. function fetchBlobFromUrl(blobUrl) {
  23. return new Promise((resolve, reject) => {
  24. const xhr = new XMLHttpRequest();
  25. xhr.open('GET', blobUrl, true);
  26. xhr.responseType = 'blob';
  27. xhr.onload = function () {
  28. if (xhr.status === 200) {
  29. resolve(xhr.response);
  30. } else {
  31. reject(new Error(`Failed to fetch blob: ${xhr.statusText}`));
  32. }
  33. };
  34. xhr.onerror = function (err) {
  35. reject(new Error(err));
  36. };
  37. xhr.send();
  38. });
  39. }
  40. (async function () {
  41. const fileUrl = decodeURIComponent(getQueryVariable('file') || '')
  42. const blob = await fetchBlobFromUrl(fileUrl);
  43. console.log('txt', blob)
  44. const reader = new FileReader();
  45. reader.onload = () => {
  46. // console.log('reader.result', reader.result)
  47. document.getElementById('container').innerHTML = reader.result
  48. };
  49. reader.readAsText(blob);
  50. }())
  51. </script>
  52. <body id="body">
  53. <pre id="container"></pre>
  54. </body>
  55. </html>