txtview.html 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <head>
  2. <base>
  3. <script src="https://fastly.jsdelivr.net/npm/marked@3.0.7/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. var file;
  21. var blocksize = 1024;
  22. function loadSlice(){
  23. function render(u8array){
  24. document.body.innerHTML = marked(new TextDecoder().decode(u8array));
  25. scroll(0,0);
  26. }
  27. function setPageUrl(pageurl){
  28. let a = document.createElement('a');
  29. a.href = "i:5c"+pageurl;
  30. a.click();
  31. }
  32. var page = 1;
  33. var extrasize = blocksize; //extrasize should <= blocksize
  34. let lhash = location.hash;
  35. if(lhash){
  36. if("p"!==lhash.charAt(1)){//#offset=&[len]
  37. let iAmpersand = lhash.indexOf('&',8);
  38. let offset = parseInt(lhash.substring(8,iAmpersand));
  39. let len = parseInt(lhash.substring(iAmpersand+1));
  40. {//update urlInc() url
  41. let href = location.href;
  42. let iHash = href.indexOf('#');
  43. href=href.substring(0,iHash)+"#page="+Math.floor((offset+len)/blocksize);
  44. setPageUrl(href);
  45. }
  46. file.slice(offset,len).then((buffer)=>{
  47. let u8 = new Uint8Array(buffer);
  48. render(u8);
  49. });
  50. return;
  51. }
  52. //#page=
  53. page = parseInt(lhash.substring(6));
  54. }else
  55. setPageUrl(location.href+"#page=1");
  56. file.slice(blocksize*(page-1),blocksize+extrasize).then((buffer)=>{
  57. let u8 = new Uint8Array(buffer);
  58. let u8len = u8.length;
  59. let iStart = 0;
  60. let limit = Math.min(u8len,extrasize);
  61. if(1!=page){
  62. for(let i=1;i<limit;i=i+2){
  63. if(10==u8[i] && 10==u8[i-1]) {
  64. iStart = i+1;
  65. break;
  66. }
  67. }
  68. }
  69. let iEnd = Math.min(u8len,blocksize);
  70. limit = Math.min(u8len,blocksize+extrasize);
  71. for(let i=blocksize+1;i<limit;i=i+2){
  72. if(10==u8[i] && 10==u8[i-1]) {
  73. iEnd = i+1;
  74. break;
  75. }
  76. }
  77. render(u8.slice(iStart,iEnd));
  78. });
  79. }
  80. //?url=#offset=&[len]&page=
  81. {
  82. let url = location.search.substring(5);
  83. file = new absFile(url);
  84. document.getElementsByTagName('base')[0].href=url;
  85. }
  86. window.addEventListener('hashchange',loadSlice);
  87. </script>
  88. </head>
  89. <body>
  90. <script>loadSlice();</script>
  91. </body>