single-file-browser.js 3.2 KB

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