single-file-browser.js 3.5 KB

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