single-file-browser.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global SingleFileCore, DOMParser, TextDecoder, Blob, fetch, base64, superFetch, parseSrcset, uglifycss, htmlmini, rulesMinifier, fontsMinifier, lazyLoader, serializer, docHelper */
  21. this.SingleFile = this.SingleFile || (() => {
  22. const ONE_MB = 1024 * 1024;
  23. // --------
  24. // Download
  25. // --------
  26. let fetchResource;
  27. class Download {
  28. static async getContent(resourceURL, options) {
  29. let resourceContent;
  30. if (!fetchResource) {
  31. fetchResource = typeof superFetch == "undefined" ? fetch : superFetch.fetch;
  32. }
  33. try {
  34. resourceContent = await fetchResource(resourceURL);
  35. } catch (error) {
  36. return options && options.asDataURI ? "data:base64," : "";
  37. }
  38. if (resourceContent.status >= 400) {
  39. resourceContent = options && options.asDataURI ? "data:base64," : "";
  40. }
  41. let contentType = resourceContent.headers && resourceContent.headers.get("content-type");
  42. if (contentType) {
  43. contentType = contentType.match(/^([^;]*)/)[0];
  44. }
  45. if (options && options.asDataURI) {
  46. try {
  47. const buffer = await resourceContent.arrayBuffer();
  48. const dataURI = "data:" + (contentType || "") + ";" + "base64," + base64.fromByteArray(new Uint8Array(buffer));
  49. if (options.maxResourceSizeEnabled && buffer.byteLength > options.maxResourceSize * ONE_MB) {
  50. return "data:base64,";
  51. } else {
  52. return dataURI;
  53. }
  54. } catch (error) {
  55. return "data:base64,";
  56. }
  57. } else {
  58. const matchCharset = contentType && contentType.match(/\s*;\s*charset\s*=\s*"?([^";]*)"?(;|$)/i);
  59. let charSet;
  60. if (matchCharset && matchCharset[1]) {
  61. charSet = matchCharset[1].toLowerCase();
  62. }
  63. if (!charSet) {
  64. charSet = "utf-8";
  65. }
  66. try {
  67. const arrayBuffer = await resourceContent.arrayBuffer();
  68. const textContent = (new TextDecoder(charSet)).decode(arrayBuffer);
  69. if (options.maxResourceSizeEnabled && textContent.length > options.maxResourceSize * ONE_MB) {
  70. return "";
  71. } else {
  72. return textContent;
  73. }
  74. } catch (error) {
  75. return "";
  76. }
  77. }
  78. }
  79. }
  80. // ---
  81. // DOM
  82. // ---
  83. class DOM {
  84. static createDoc(pageContent, baseURI) {
  85. const doc = (new DOMParser()).parseFromString(pageContent, "text/html");
  86. let baseElement = doc.querySelector("base");
  87. if (!baseElement || !baseElement.getAttribute("href")) {
  88. if (baseElement) {
  89. baseElement.remove();
  90. }
  91. baseElement = doc.createElement("base");
  92. baseElement.setAttribute("href", baseURI);
  93. doc.head.insertBefore(baseElement, doc.head.firstChild);
  94. }
  95. return doc;
  96. }
  97. static getParser() {
  98. return DOMParser;
  99. }
  100. static getContentSize(content) {
  101. return new Blob([content]).size;
  102. }
  103. static htmlminiProcess(doc, options) {
  104. return htmlmini.process(doc, options);
  105. }
  106. static htmlminiPostProcess(doc) {
  107. return htmlmini.postProcess(doc);
  108. }
  109. static lazyLoader(doc) {
  110. return lazyLoader.process(doc);
  111. }
  112. static lazyLoaderImageSelectors() {
  113. return lazyLoader.imageSelectors;
  114. }
  115. static rulesMinifier(doc) {
  116. return rulesMinifier.process(doc);
  117. }
  118. static fontsMinifier(doc, secondPass) {
  119. return fontsMinifier.process(doc, secondPass);
  120. }
  121. static uglifycss(content, options) {
  122. return uglifycss.processString(content, options);
  123. }
  124. static parseSrcset(srcset) {
  125. return parseSrcset.process(srcset);
  126. }
  127. static serialize(doc, compressHTML) {
  128. return serializer.process(doc, compressHTML);
  129. }
  130. static preProcessDoc(doc, win, options) {
  131. return docHelper.preProcessDoc(doc, win, options);
  132. }
  133. static postProcessDoc(doc, options) {
  134. docHelper.postProcessDoc(doc, options);
  135. }
  136. static windowIdAttributeName(sessionId) {
  137. return docHelper.windowIdAttributeName(sessionId);
  138. }
  139. static preservedSpaceAttributeName(sessionId) {
  140. return docHelper.preservedSpaceAttributeName(sessionId);
  141. }
  142. static removedContentAttributeName(sessionId) {
  143. return docHelper.removedContentAttributeName(sessionId);
  144. }
  145. }
  146. return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) };
  147. })();