epub.ftl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>${file.name}文件预览</title>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
  7. <#include "*/commonHeader.ftl">
  8. <script src="js/base64.min.js" type="text/javascript"></script>
  9. <script src="js/jszip.min.js"></script>
  10. <script src="epub/epub.js"></script>
  11. <link rel="stylesheet" type="text/css" href="epub/examples.css">
  12. </head>
  13. <#if currentUrl?contains("http://") || currentUrl?contains("https://") || currentUrl?contains("file://")>
  14. <#assign finalUrl="${currentUrl}">
  15. <#elseif currentUrl?contains("ftp://") >
  16. <#assign finalUrl="${currentUrl}">
  17. <#else>
  18. <#assign finalUrl="${baseUrl}${currentUrl}">
  19. </#if>
  20. <body>
  21. <select id="toc"></select>
  22. <div id="viewer" class="scrolled"></div>
  23. <div id="prev" class="arrow">‹</div>
  24. <div id="next" class="arrow">›</div>
  25. </body>
  26. <script type="text/javascript">
  27. var url = '${finalUrl}';
  28. var baseUrl = '${baseUrl}'.endsWith('/') ? '${baseUrl}' : '${baseUrl}' + '/';
  29. if (!url.startsWith(baseUrl)) {
  30. url = baseUrl + 'getCorsFile?urlPath=' + encodeURIComponent(Base64.encode(url));
  31. }
  32. function blobToArrayBuffer(blob) {
  33. return new Promise((resolve, reject) => {
  34. const reader = new FileReader();
  35. reader.onerror = reject;
  36. reader.onload = () => {
  37. resolve(reader.result);
  38. };
  39. reader.readAsArrayBuffer(blob);
  40. });
  41. }
  42. let xhr = new XMLHttpRequest();
  43. xhr.open('GET',url); //文件所在地址
  44. xhr.responseType = 'blob';
  45. xhr.onload = () => {
  46. var currentSectionIndex = 100;
  47. var book = ePub();
  48. let content = xhr.response;
  49. let blob = new Blob([content]);
  50. var bookData = blobToArrayBuffer(blob);
  51. book.open(bookData, "binary");
  52. var rendition = book.renderTo("viewer", {
  53. flow: "scrolled-doc",
  54. width: "100%",
  55. allowScriptedContent: true
  56. // height: 600
  57. });
  58. var displayed = rendition.display();
  59. var params = URLSearchParams && new URLSearchParams(document.location.search.substring(1));
  60. var currentSectionIndex = (params && params.get("loc")) ? params.get("loc") : undefined;
  61. rendition.display(currentSectionIndex);
  62. var next = document.getElementById("next");
  63. next.addEventListener("click", function(e){
  64. rendition.next();
  65. e.preventDefault();
  66. }, false);
  67. var prev = document.getElementById("prev");
  68. prev.addEventListener("click", function(e){
  69. rendition.prev();
  70. e.preventDefault();
  71. }, false);
  72. rendition.on("rendered", function(section){
  73. var nextSection = section.next();
  74. var prevSection = section.prev();
  75. if(nextSection) {
  76. nextNav = book.navigation.get(nextSection.href);
  77. if(nextNav) {
  78. nextLabel = nextNav.label;
  79. } else {
  80. nextLabel = "next";
  81. }
  82. next.textContent = " »";
  83. } else {
  84. next.textContent = "";
  85. }
  86. if(prevSection) {
  87. prevNav = book.navigation.get(prevSection.href);
  88. if(prevNav) {
  89. prevLabel = prevNav.label;
  90. } else {
  91. prevLabel = "previous";
  92. }
  93. prev.textContent = "« " ;
  94. } else {
  95. prev.textContent = "";
  96. }
  97. });
  98. rendition.on("rendered", function(section){
  99. var current = book.navigation && book.navigation.get(section.href);
  100. if (current) {
  101. var $select = document.getElementById("toc");
  102. var $selected = $select.querySelector("option[selected]");
  103. if ($selected) {
  104. $selected.removeAttribute("selected");
  105. }
  106. var $options = $select.querySelectorAll("option");
  107. for (var i = 0; i < $options.length; ++i) {
  108. let selected = $options[i].getAttribute("ref") === current.href;
  109. if (selected) {
  110. $options[i].setAttribute("selected", "");
  111. }
  112. }
  113. }
  114. });
  115. book.loaded.navigation.then(function(toc){
  116. var $select = document.getElementById("toc"),
  117. docfrag = document.createDocumentFragment();
  118. toc.forEach(function(chapter) {
  119. var option = document.createElement("option");
  120. option.textContent = chapter.label;
  121. option.setAttribute("ref", chapter.href);
  122. docfrag.appendChild(option);
  123. });
  124. $select.appendChild(docfrag);
  125. $select.onchange = function(){
  126. var index = $select.selectedIndex,
  127. url = $select.options[index].getAttribute("ref");
  128. rendition.display(url);
  129. return false;
  130. };
  131. });
  132. }
  133. xhr.send();
  134. /*初始化水印*/
  135. if (!!window.ActiveXObject || "ActiveXObject" in window)
  136. {
  137. }else{
  138. initWaterMark();
  139. }
  140. </script>
  141. </html>