single-file-browser.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, btoa, DOMParser, getComputedStyle, TextDecoder, window, fetch */
  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. const bytes = new Uint8Array(buffer);
  45. let base64Content = "";
  46. bytes.forEach(byte => base64Content += String.fromCharCode(byte));
  47. return "data:" + (contentType || "") + ";" + "base64," + btoa(base64Content);
  48. } catch (e) {
  49. return "data:base64,";
  50. }
  51. } else {
  52. const matchCharset = contentType.match(/\s*;\s*charset\s*=\s*(.*)(;|$)/i);
  53. if (matchCharset && matchCharset[1]) {
  54. const charSet = matchCharset[1].toLowerCase();
  55. if (charSet != "utf-8") {
  56. const arrayBuffer = await resourceContent.arrayBuffer();
  57. return (new TextDecoder(charSet)).decode(arrayBuffer);
  58. } else {
  59. return resourceContent.text();
  60. }
  61. } else {
  62. return resourceContent.text();
  63. }
  64. }
  65. }
  66. }
  67. // ---
  68. // DOM
  69. // ---
  70. class DOM {
  71. static create(pageContent/*, url*/) {
  72. const doc = (new DOMParser()).parseFromString(pageContent, "text/html");
  73. return {
  74. DOMParser,
  75. getComputedStyle,
  76. document: doc,
  77. serialize: () => getDoctype(doc) + doc.documentElement.outerHTML
  78. };
  79. }
  80. }
  81. function getDoctype(doc) {
  82. const docType = doc.doctype;
  83. let docTypeString;
  84. if (docType) {
  85. docTypeString = "<!DOCTYPE " + docType.nodeName;
  86. if (docType.publicId) {
  87. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  88. if (docType.systemId)
  89. docTypeString += " \"" + docType.systemId + "\"";
  90. } else if (docType.systemId)
  91. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  92. if (docType.internalSubset)
  93. docTypeString += " [" + docType.internalSubset + "]";
  94. return docTypeString + ">\n";
  95. }
  96. return "";
  97. }
  98. return SingleFileCore(Download, DOM, URL);
  99. })();