content.js 4.9 KB

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