single-file-browser.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // --------
  23. // Download
  24. // --------
  25. let fetchResource;
  26. class Download {
  27. static async getContent(resourceURL, asDataURI) {
  28. const requestOptions = { method: "GET" };
  29. let resourceContent;
  30. if (!fetchResource) {
  31. fetchResource = typeof superFetch == "undefined" ? fetch : superFetch;
  32. }
  33. try {
  34. resourceContent = await fetchResource(resourceURL, requestOptions);
  35. } catch (e) {
  36. return asDataURI ? "data:base64," : "";
  37. }
  38. const contentType = resourceContent.headers.get("content-type");
  39. if (asDataURI) {
  40. try {
  41. const buffer = await resourceContent.arrayBuffer();
  42. return "data:" + (contentType || "") + ";" + "base64," + base64.fromByteArray(new Uint8Array(buffer));
  43. } catch (e) {
  44. return "data:base64,";
  45. }
  46. } else {
  47. const matchCharset = contentType && contentType.match(/\s*;\s*charset\s*=\s*"?([^";]*)"?(;|$)/i);
  48. if (matchCharset && matchCharset[1]) {
  49. const charSet = matchCharset[1].toLowerCase();
  50. if (charSet != "utf-8") {
  51. const arrayBuffer = await resourceContent.arrayBuffer();
  52. return (new TextDecoder(charSet)).decode(arrayBuffer);
  53. } else {
  54. return resourceContent.text();
  55. }
  56. } else {
  57. return resourceContent.text();
  58. }
  59. }
  60. }
  61. }
  62. // ---
  63. // DOM
  64. // ---
  65. class DOM {
  66. static create(pageContent, baseURI) {
  67. const doc = (new DOMParser()).parseFromString(pageContent, "text/html");
  68. let baseElement = doc.querySelector("base");
  69. if (!baseElement) {
  70. baseElement = doc.createElement("base");
  71. baseElement.href = encodeURIComponent(baseURI);
  72. doc.head.insertBefore(baseElement, doc.head.firstChild);
  73. }
  74. return {
  75. DOMParser,
  76. getComputedStyle,
  77. document: doc,
  78. serialize: () => getDoctype(doc) + doc.documentElement.outerHTML,
  79. parseSrcset: srcset => parseSrcset(srcset),
  80. uglifycss: (content, options) => uglifycss.processString(content, options)
  81. };
  82. }
  83. }
  84. function getDoctype(doc) {
  85. const docType = doc.doctype;
  86. let docTypeString;
  87. if (docType) {
  88. docTypeString = "<!DOCTYPE " + docType.nodeName;
  89. if (docType.publicId) {
  90. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  91. if (docType.systemId)
  92. docTypeString += " \"" + docType.systemId + "\"";
  93. } else if (docType.systemId)
  94. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  95. if (docType.internalSubset)
  96. docTypeString += " [" + docType.internalSubset + "]";
  97. return docTypeString + ">\n";
  98. }
  99. return "";
  100. }
  101. return SingleFileCore(Download, DOM, URL);
  102. })();