single-file-browser.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, getComputedStyle, TextDecoder, fetch, superFetch, parseSrcset, uglifycss */
  21. 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. const requestOptions = { method: "GET" };
  30. let resourceContent;
  31. if (!fetchResource) {
  32. fetchResource = typeof superFetch == "undefined" ? fetch : superFetch;
  33. }
  34. try {
  35. resourceContent = await fetchResource(resourceURL, requestOptions);
  36. } catch (e) {
  37. return options && options.asDataURI ? "data:base64," : "";
  38. }
  39. const contentType = resourceContent.headers.get("content-type");
  40. if (options && options.asDataURI) {
  41. try {
  42. const buffer = await resourceContent.arrayBuffer();
  43. const dataURI = "data:" + (contentType || "") + ";" + "base64," + base64.fromByteArray(new Uint8Array(buffer));
  44. if (options.maxSize && buffer.size > options.maxSize * ONE_MB) {
  45. return "data:base64,";
  46. } else {
  47. return dataURI;
  48. }
  49. } catch (e) {
  50. return "data:base64,";
  51. }
  52. } else {
  53. const matchCharset = contentType && contentType.match(/\s*;\s*charset\s*=\s*"?([^";]*)"?(;|$)/i);
  54. let textContent;
  55. if (matchCharset && matchCharset[1]) {
  56. const charSet = matchCharset[1].toLowerCase();
  57. if (charSet != "utf-8") {
  58. const arrayBuffer = await resourceContent.arrayBuffer();
  59. textContent = (new TextDecoder(charSet)).decode(arrayBuffer);
  60. } else {
  61. textContent = resourceContent.text();
  62. }
  63. } else {
  64. textContent = resourceContent.text();
  65. }
  66. if (options.maxSize && textContent.length > options.maxSize * ONE_MB) {
  67. return "";
  68. } else {
  69. return textContent;
  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) {
  82. baseElement = doc.createElement("base");
  83. baseElement.href = encodeURIComponent(baseURI);
  84. doc.head.insertBefore(baseElement, doc.head.firstChild);
  85. }
  86. return {
  87. DOMParser,
  88. getComputedStyle,
  89. document: doc,
  90. serialize: () => getDoctype(doc) + doc.documentElement.outerHTML,
  91. parseSrcset: srcset => parseSrcset(srcset),
  92. uglifycss: (content, options) => uglifycss.processString(content, options)
  93. };
  94. }
  95. }
  96. function getDoctype(doc) {
  97. const docType = doc.doctype;
  98. let docTypeString;
  99. if (docType) {
  100. docTypeString = "<!DOCTYPE " + docType.nodeName;
  101. if (docType.publicId) {
  102. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  103. if (docType.systemId)
  104. docTypeString += " \"" + docType.systemId + "\"";
  105. } else if (docType.systemId)
  106. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  107. if (docType.internalSubset)
  108. docTypeString += " [" + docType.internalSubset + "]";
  109. return docTypeString + ">\n";
  110. }
  111. return "";
  112. }
  113. return SingleFileCore(Download, DOM, URL);
  114. })();