single-file-browser.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const contentType = resourceContent.headers.get("content-type");
  39. if (options && options.asDataURI) {
  40. try {
  41. const buffer = await resourceContent.arrayBuffer();
  42. const dataURI = "data:" + (contentType || "") + ";" + "base64," + base64.fromByteArray(new Uint8Array(buffer));
  43. if (options.maxResourceSizeEnabled && buffer.byteLength > options.maxResourceSize * ONE_MB) {
  44. return "data:base64,";
  45. } else {
  46. return dataURI;
  47. }
  48. } catch (error) {
  49. return "data:base64,";
  50. }
  51. } else {
  52. const matchCharset = contentType && contentType.match(/\s*;\s*charset\s*=\s*"?([^";]*)"?(;|$)/i);
  53. let charSet;
  54. if (matchCharset && matchCharset[1]) {
  55. charSet = matchCharset[1].toLowerCase();
  56. }
  57. if (!charSet) {
  58. charSet = "utf-8";
  59. }
  60. try {
  61. const arrayBuffer = await resourceContent.arrayBuffer();
  62. const textContent = (new TextDecoder(charSet)).decode(arrayBuffer);
  63. if (options.maxResourceSizeEnabled && textContent.length > options.maxResourceSize * ONE_MB) {
  64. return "";
  65. } else {
  66. return textContent;
  67. }
  68. } catch (error) {
  69. return "";
  70. }
  71. }
  72. }
  73. }
  74. // ---
  75. // DOM
  76. // ---
  77. class DOM {
  78. static create(pageContent, baseURI) {
  79. const doc = (new DOMParser()).parseFromString(pageContent, "text/html");
  80. let baseElement = doc.querySelector("base");
  81. if (!baseElement || !baseElement.getAttribute("href")) {
  82. if (baseElement) {
  83. baseElement.remove();
  84. }
  85. baseElement = doc.createElement("base");
  86. baseElement.setAttribute("href", baseURI);
  87. doc.head.insertBefore(baseElement, doc.head.firstChild);
  88. }
  89. return {
  90. DOMParser,
  91. document: doc,
  92. serialize: () => getDoctype(doc) + doc.documentElement.outerHTML,
  93. parseSrcset: srcset => parseSrcset.process(srcset),
  94. uglifycss: (content, options) => uglifycss.processString(content, options),
  95. lazyLoader: {
  96. process: doc => lazyLoader.process(doc),
  97. imageSelectors: lazyLoader.imageSelectors
  98. },
  99. htmlmini: {
  100. process: doc => htmlmini.process(doc),
  101. postProcess: doc => htmlmini.postProcess(doc),
  102. },
  103. rulesMinifier: doc => rulesMinifier.process(doc)
  104. };
  105. }
  106. }
  107. function getDoctype(doc) {
  108. const docType = doc.doctype;
  109. let docTypeString;
  110. if (docType) {
  111. docTypeString = "<!DOCTYPE " + docType.nodeName;
  112. if (docType.publicId) {
  113. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  114. if (docType.systemId)
  115. docTypeString += " \"" + docType.systemId + "\"";
  116. } else if (docType.systemId)
  117. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  118. if (docType.internalSubset)
  119. docTypeString += " [" + docType.internalSubset + "]";
  120. return docTypeString + ">\n";
  121. }
  122. return "";
  123. }
  124. return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) };
  125. })();