client.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 chrome, SingleFile, singlefile, document, Blob */
  21. (() => {
  22. chrome.extension.onMessage.addListener(request => {
  23. if (request.processStart) {
  24. fixInlineScripts();
  25. const options = request.options;
  26. options.url = document.location.href;
  27. options.content = getDoctype(document) + document.documentElement.outerHTML;
  28. options.jsEnabled = true;
  29. options.onprogress = event => {
  30. if (event.type == "resources-initialized") {
  31. chrome.runtime.sendMessage({
  32. processStart: true,
  33. index: event.details.index,
  34. maxIndex: event.details.max
  35. });
  36. }
  37. if (event.type == "resource-loaded") {
  38. chrome.runtime.sendMessage({
  39. processProgress: true,
  40. index: event.details.index,
  41. maxIndex: event.details.max
  42. });
  43. }
  44. if (event.type == "page-ended") {
  45. chrome.runtime.sendMessage({ processEnd: true });
  46. }
  47. };
  48. singlefile.ui.processStart();
  49. SingleFile.process(options)
  50. .then(page => {
  51. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  52. page.filename = page.title + ".html";
  53. downloadPage(page);
  54. singlefile.ui.processEnd();
  55. })
  56. .catch(error => {
  57. console.error(error);
  58. chrome.runtime.sendMessage({ processError: true });
  59. });
  60. }
  61. });
  62. function downloadPage(page) {
  63. const link = document.createElement("a");
  64. document.body.appendChild(link);
  65. link.download = page.filename;
  66. link.href = page.url;
  67. link.click();
  68. link.remove();
  69. }
  70. function getDoctype(doc) {
  71. const docType = doc.doctype;
  72. let docTypeString;
  73. if (docType) {
  74. docTypeString = "<!DOCTYPE " + docType.nodeName;
  75. if (docType.publicId) {
  76. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  77. if (docType.systemId)
  78. docTypeString += " \"" + docType.systemId + "\"";
  79. } else if (docType.systemId)
  80. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  81. if (docType.internalSubset)
  82. docTypeString += " [" + docType.internalSubset + "]";
  83. return docTypeString + ">\n";
  84. }
  85. return "";
  86. }
  87. function fixInlineScripts() {
  88. document.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  89. }
  90. })();