client.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, MouseEvent, getSelection */
  21. (() => {
  22. const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-single-file-selected-content";
  23. chrome.runtime.onMessage.addListener(request => {
  24. if (request.processStart) {
  25. fixInlineScripts();
  26. const options = request.options;
  27. options.url = document.location.href;
  28. if (options.selected) {
  29. markSelectedContent();
  30. }
  31. options.content = getDoctype(document) + document.documentElement.outerHTML;
  32. document.querySelectorAll("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME));
  33. options.jsEnabled = true;
  34. options.onprogress = event => {
  35. if (event.type == event.RESOURCES_INITIALIZED) {
  36. chrome.runtime.sendMessage({
  37. processStart: true,
  38. index: event.details.index,
  39. maxIndex: event.details.max
  40. });
  41. }
  42. if (event.type == event.RESOURCE_LOADED) {
  43. chrome.runtime.sendMessage({
  44. processProgress: true,
  45. index: event.details.index,
  46. maxIndex: event.details.max
  47. });
  48. }
  49. if (event.type == event.PAGE_ENDED) {
  50. chrome.runtime.sendMessage({ processEnd: true });
  51. }
  52. };
  53. singlefile.ui.start();
  54. SingleFile.process(options)
  55. .then(page => {
  56. const date = new Date();
  57. page.filename = page.title + " - (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" + ".html";
  58. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  59. downloadPage(page);
  60. singlefile.ui.end();
  61. })
  62. .catch(error => {
  63. console.error(error);
  64. chrome.runtime.sendMessage({ processError: true });
  65. });
  66. }
  67. });
  68. function downloadPage(page) {
  69. const link = document.createElement("a");
  70. document.body.appendChild(link);
  71. link.download = page.filename;
  72. link.href = page.url;
  73. link.dispatchEvent(new MouseEvent("click"));
  74. link.remove();
  75. }
  76. function getDoctype(doc) {
  77. const docType = doc.doctype;
  78. let docTypeString;
  79. if (docType) {
  80. docTypeString = "<!DOCTYPE " + docType.nodeName;
  81. if (docType.publicId) {
  82. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  83. if (docType.systemId)
  84. docTypeString += " \"" + docType.systemId + "\"";
  85. } else if (docType.systemId)
  86. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  87. if (docType.internalSubset)
  88. docTypeString += " [" + docType.internalSubset + "]";
  89. return docTypeString + ">\n";
  90. }
  91. return "";
  92. }
  93. function markSelectedContent() {
  94. const selection = getSelection();
  95. const range = selection.rangeCount ? selection.getRangeAt(0) : null;
  96. let node;
  97. if (range && range.startOffset != range.endOffset) {
  98. node = range.commonAncestorContainer;
  99. if (node.nodeType != node.ELEMENT_NODE) {
  100. node = node.parentElement;
  101. }
  102. }
  103. node.setAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME, "");
  104. }
  105. function fixInlineScripts() {
  106. document.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  107. }
  108. })();