single-file-browser.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 fetch, document, btoa, DOMParser, getComputedStyle, FileReader, SingleFileCore */
  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 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 ? 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 fileReader = new FileReader();
  55. const blob = await resourceContent.blob();
  56. fileReader.readAsText(blob, matchCharset[1]);
  57. return new Promise((resolve, reject) => {
  58. fileReader.onload = event => resolve(event.target.result);
  59. fileReader.onerror = reject;
  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 = document.implementation.createHTMLDocument();
  73. doc.open();
  74. doc.write(pageContent);
  75. doc.close();
  76. return {
  77. DOMParser,
  78. getComputedStyle,
  79. document: doc,
  80. serialize: () => getDoctype(doc) + doc.documentElement.outerHTML
  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. })();