txtview.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <head>
  2. <base>
  3. <script src="https://fastly.jsdelivr.net/npm/marked/marked.min.js"></script>
  4. <script>
  5. function absFile(url) {
  6. this.name=url;
  7. this.slice = async(offset, length) =>{
  8. const headers = new Headers();
  9. headers.append('range', 'bytes=' + offset + '-' + ( offset + length -1).toString());
  10. const opts = {
  11. credentials: 'include',
  12. headers : headers
  13. };
  14. const resp = await fetch( this.name, opts );
  15. return await resp.arrayBuffer();
  16. //alert(JSON.stringify(resp.headers));
  17. //return await resp.text();
  18. }
  19. }
  20. marked.setOptions({
  21. gfm: true,
  22. });
  23. var file;
  24. var blocksize = 20480;
  25. function loadSlice(){
  26. function render(u8array){
  27. document.body.innerHTML = marked.parse(new TextDecoder().decode(u8array));
  28. scroll(0,0);
  29. }
  30. function setPageUrl(pageurl){
  31. let a = document.createElement('a');
  32. a.href = "i:5c"+pageurl;
  33. a.click();
  34. }
  35. var page = 1;
  36. var extrasize = blocksize; //extrasize should <= blocksize
  37. let lhash = location.hash;
  38. if(lhash){
  39. if("p"!==lhash.charAt(1)){//#offset=&[len]
  40. let iAmpersand = lhash.indexOf('&',8);
  41. let offset = parseInt(lhash.substring(8,iAmpersand));
  42. let len = parseInt(lhash.substring(iAmpersand+1));
  43. {//update urlInc() url
  44. let href = location.href;
  45. let iHash = href.indexOf('#');
  46. href=href.substring(0,iHash)+"#page="+Math.floor((offset+len)/blocksize);
  47. setPageUrl(href);
  48. }
  49. file.slice(offset,len).then((buffer)=>{
  50. let u8 = new Uint8Array(buffer);
  51. render(u8);
  52. });
  53. return;
  54. }
  55. //#page=
  56. page = parseInt(lhash.substring(6));
  57. }else
  58. setPageUrl(location.href+"#page=1");
  59. file.slice(blocksize*(page-1),blocksize+extrasize).then((buffer)=>{
  60. let u8 = new Uint8Array(buffer);
  61. let u8len = u8.length;
  62. let iStart = 0;
  63. let limit = Math.min(u8len,extrasize);
  64. if(1!=page){
  65. for(let i=1;i<limit;i=i+2){
  66. if(10==u8[i] && 10==u8[i-1]) {
  67. iStart = i+1;
  68. break;
  69. }
  70. }
  71. }
  72. let iEnd = Math.min(u8len,blocksize);
  73. limit = Math.min(u8len,blocksize+extrasize);
  74. for(let i=blocksize+1;i<limit;i=i+2){
  75. if(10==u8[i] && 10==u8[i-1]) {
  76. iEnd = i+1;
  77. break;
  78. }
  79. }
  80. render(u8.slice(iStart,iEnd));
  81. });
  82. }
  83. //?url=#offset=&[len]&page=
  84. {
  85. let url = location.search.substring(5);
  86. file = new absFile(url);
  87. document.getElementsByTagName('base')[0].href=url;
  88. }
  89. loadSlice();
  90. window.addEventListener('hashchange',loadSlice);
  91. </script>
  92. </head>
  93. <body>
  94. </body>