content.js 4.5 KB

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