client.js 2.8 KB

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