client.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, FrameTree, 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. getOptions(request.options)
  27. .then(options => SingleFile.initialize(options))
  28. .then(process => {
  29. singlefile.ui.init();
  30. return process();
  31. })
  32. .then(page => {
  33. const date = new Date();
  34. page.filename = page.title + " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" + ".html";
  35. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  36. downloadPage(page);
  37. singlefile.ui.end();
  38. })
  39. .catch(error => {
  40. chrome.runtime.sendMessage({ processError: true });
  41. throw error;
  42. });
  43. }
  44. });
  45. function fixInlineScripts() {
  46. document.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  47. }
  48. async function getOptions(options) {
  49. options.url = document.location.href;
  50. if (options.selected) {
  51. markSelectedContent();
  52. }
  53. options.content = getDoctype(document) + document.documentElement.outerHTML;
  54. options.canvasData = getCanvasData();
  55. if (!options.removeFrames) {
  56. options.framesData = await FrameTree.getFramesData();
  57. }
  58. document.querySelectorAll("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME));
  59. options.jsEnabled = true;
  60. options.onprogress = onProgress;
  61. return options;
  62. }
  63. function getCanvasData() {
  64. const canvasData = [];
  65. document.querySelectorAll("canvas").forEach(canvasElement => {
  66. try {
  67. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  68. } catch (e) {
  69. canvasData.push(null);
  70. }
  71. });
  72. return canvasData;
  73. }
  74. function markSelectedContent() {
  75. const selection = getSelection();
  76. const range = selection.rangeCount ? selection.getRangeAt(0) : null;
  77. let node;
  78. if (range && range.startOffset != range.endOffset) {
  79. node = range.commonAncestorContainer;
  80. if (node.nodeType != node.ELEMENT_NODE) {
  81. node = node.parentElement;
  82. }
  83. }
  84. node.setAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME, "");
  85. }
  86. function getDoctype(doc) {
  87. const docType = doc.doctype;
  88. let docTypeString;
  89. if (docType) {
  90. docTypeString = "<!DOCTYPE " + docType.nodeName;
  91. if (docType.publicId) {
  92. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  93. if (docType.systemId)
  94. docTypeString += " \"" + docType.systemId + "\"";
  95. } else if (docType.systemId)
  96. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  97. if (docType.internalSubset)
  98. docTypeString += " [" + docType.internalSubset + "]";
  99. return docTypeString + ">\n";
  100. }
  101. return "";
  102. }
  103. function onProgress(event) {
  104. if (event.type == event.RESOURCES_INITIALIZED) {
  105. chrome.runtime.sendMessage({
  106. processStart: true,
  107. index: event.details.index,
  108. maxIndex: event.details.max
  109. });
  110. }
  111. if (event.type == event.RESOURCE_LOADED) {
  112. chrome.runtime.sendMessage({
  113. processProgress: true,
  114. index: event.details.index,
  115. maxIndex: event.details.max
  116. });
  117. }
  118. if (event.type == event.PAGE_ENDED) {
  119. chrome.runtime.sendMessage({ processEnd: true });
  120. }
  121. }
  122. function downloadPage(page) {
  123. const link = document.createElement("a");
  124. document.body.appendChild(link);
  125. link.download = page.filename;
  126. link.href = page.url;
  127. link.dispatchEvent(new MouseEvent("click"));
  128. link.remove();
  129. }
  130. })();