single-file-browser.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, 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. let contentType = resourceContent.headers.get("content-type");
  39. if (contentType) {
  40. contentType = contentType.match(/^([^;]*)/)[0];
  41. }
  42. if (options && options.asDataURI) {
  43. try {
  44. const buffer = await resourceContent.arrayBuffer();
  45. const dataURI = "data:" + (contentType || "") + ";" + "base64," + base64.fromByteArray(new Uint8Array(buffer));
  46. if (options.maxResourceSizeEnabled && buffer.byteLength > options.maxResourceSize * ONE_MB) {
  47. return "data:base64,";
  48. } else {
  49. return dataURI;
  50. }
  51. } catch (error) {
  52. return "data:base64,";
  53. }
  54. } else {
  55. const matchCharset = contentType && contentType.match(/\s*;\s*charset\s*=\s*"?([^";]*)"?(;|$)/i);
  56. let charSet;
  57. if (matchCharset && matchCharset[1]) {
  58. charSet = matchCharset[1].toLowerCase();
  59. }
  60. if (!charSet) {
  61. charSet = "utf-8";
  62. }
  63. try {
  64. const arrayBuffer = await resourceContent.arrayBuffer();
  65. const textContent = (new TextDecoder(charSet)).decode(arrayBuffer);
  66. if (options.maxResourceSizeEnabled && textContent.length > options.maxResourceSize * ONE_MB) {
  67. return "";
  68. } else {
  69. return textContent;
  70. }
  71. } catch (error) {
  72. return "";
  73. }
  74. }
  75. }
  76. }
  77. // ---
  78. // DOM
  79. // ---
  80. class DOM {
  81. static createDoc(pageContent, baseURI) {
  82. const doc = (new DOMParser()).parseFromString(pageContent, "text/html");
  83. let baseElement = doc.querySelector("base");
  84. if (!baseElement || !baseElement.getAttribute("href")) {
  85. if (baseElement) {
  86. baseElement.remove();
  87. }
  88. baseElement = doc.createElement("base");
  89. baseElement.setAttribute("href", baseURI);
  90. doc.head.insertBefore(baseElement, doc.head.firstChild);
  91. }
  92. return doc;
  93. }
  94. static getParser() {
  95. return DOMParser;
  96. }
  97. static getContentSize(content) {
  98. return new Blob([content]).size;
  99. }
  100. static htmlminiProcess(doc, options) {
  101. return htmlmini.process(doc, options);
  102. }
  103. static htmlminiPostProcess(doc) {
  104. return htmlmini.postProcess(doc);
  105. }
  106. static lazyLoader(doc) {
  107. return lazyLoader.process(doc);
  108. }
  109. static lazyLoaderImageSelectors() {
  110. return lazyLoader.imageSelectors;
  111. }
  112. static rulesMinifier(doc) {
  113. return rulesMinifier.process(doc);
  114. }
  115. static uglifycss(content, options) {
  116. return uglifycss.processString(content, options);
  117. }
  118. static parseSrcset(srcset) {
  119. return parseSrcset.process(srcset);
  120. }
  121. static serialize(doc, compressHTML) {
  122. return serializer.process(doc, compressHTML);
  123. }
  124. static preProcessDoc(doc, win, options) {
  125. return docHelper.preProcessDoc(doc, win, options);
  126. }
  127. static postProcessDoc(doc, options) {
  128. docHelper.postProcessDoc(doc, options);
  129. }
  130. static winIdAttributeName() {
  131. return docHelper.WIN_ID_ATTRIBUTE_NAME;
  132. }
  133. static preservedSpaceAttributeName() {
  134. return docHelper.PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME;
  135. }
  136. static removedContentAttributeName() {
  137. return docHelper.REMOVED_CONTENT_ATTRIBUTE_NAME;
  138. }
  139. }
  140. return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) };
  141. })();