single-file-browser.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, base64, DOMParser, TextDecoder, fetch, superFetch, parseSrcset, uglifycss, htmlmini, rulesMinifier, lazyLoader */
  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 create(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 {
  93. DOMParser,
  94. document: doc,
  95. serialize: () => getDoctype(doc) + doc.documentElement.outerHTML,
  96. parseSrcset: srcset => parseSrcset.process(srcset),
  97. uglifycss: (content, options) => uglifycss.processString(content, options),
  98. lazyLoader: {
  99. process: doc => lazyLoader.process(doc),
  100. imageSelectors: lazyLoader.imageSelectors
  101. },
  102. htmlmini: {
  103. process: (doc, options) => htmlmini.process(doc, options),
  104. postProcess: doc => htmlmini.postProcess(doc),
  105. },
  106. rulesMinifier: doc => rulesMinifier.process(doc)
  107. };
  108. }
  109. }
  110. function getDoctype(doc) {
  111. const docType = doc.doctype;
  112. let docTypeString;
  113. if (docType) {
  114. docTypeString = "<!DOCTYPE " + docType.nodeName;
  115. if (docType.publicId) {
  116. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  117. if (docType.systemId)
  118. docTypeString += " \"" + docType.systemId + "\"";
  119. } else if (docType.systemId)
  120. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  121. if (docType.internalSubset)
  122. docTypeString += " [" + docType.internalSubset + "]";
  123. return docTypeString + ">\n";
  124. }
  125. return "";
  126. }
  127. return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) };
  128. })();