content.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 browser, SingleFile, singlefile, FrameTree, document, Blob, MouseEvent, getSelection, getComputedStyle, prompt, addEventListener, Node, HTMLElement */
  21. this.singlefile.top = this.singlefile.top || (() => {
  22. let processing = false;
  23. browser.runtime.onMessage.addListener(async message => {
  24. savePage(message);
  25. return {};
  26. });
  27. addEventListener("message", event => {
  28. if (typeof event.data === "string" && event.data.startsWith("__SingleFile__::")) {
  29. const message = JSON.parse(event.data.substring("__SingleFile__".length + 2));
  30. savePage(message);
  31. }
  32. });
  33. return true;
  34. async function savePage(message) {
  35. if (message.processStart && !processing && !message.options.frameId) {
  36. processing = true;
  37. try {
  38. const page = await processPage(message.options);
  39. await downloadPage(page, message.options);
  40. revokeDownloadURL(page);
  41. } catch (error) {
  42. console.error(error); // eslint-disable-line no-console
  43. browser.runtime.sendMessage({ processError: true, error });
  44. }
  45. processing = false;
  46. }
  47. }
  48. async function processPage(options) {
  49. options = await getOptions(options);
  50. const processor = new (SingleFile.getClass())(options);
  51. fixInlineScripts();
  52. fixHeadNoScripts();
  53. if (options.selected) {
  54. markSelectedContent(processor.SELECTED_CONTENT_ATTRIBUTE_NAME, processor.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME);
  55. }
  56. if (!options.removeFrames) {
  57. hideElementFrames();
  58. }
  59. if (options.removeHiddenElements) {
  60. markRemovedElements(processor.REMOVED_CONTENT_ATTRIBUTE_NAME);
  61. }
  62. if (options.compressHTML) {
  63. markPreserveElements(processor.PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME);
  64. }
  65. options.url = options.url || document.location.href;
  66. options.content = options.content || getDoctype(document) + document.documentElement.outerHTML;
  67. await processor.initialize();
  68. if (options.shadowEnabled) {
  69. singlefile.ui.init();
  70. }
  71. if (options.removeHiddenElements) {
  72. unmarkRemovedElements(processor.REMOVED_CONTENT_ATTRIBUTE_NAME);
  73. }
  74. if (options.compressHTML) {
  75. unmarkPreserveElements(processor.PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME);
  76. }
  77. if (!options.removeFrames) {
  78. removeWindowIdFrames(processor.WIN_ID_ATTRIBUTE_NAME);
  79. }
  80. await processor.preparePageData();
  81. const page = processor.getPageData();
  82. if (options.selected) {
  83. unmarkSelectedContent(processor.SELECTED_CONTENT_ATTRIBUTE_NAME, processor.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME);
  84. }
  85. const date = new Date();
  86. page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
  87. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  88. if (options.shadowEnabled) {
  89. singlefile.ui.end();
  90. }
  91. if (options.displayStats) {
  92. console.log("SingleFile stats"); // eslint-disable-line no-console
  93. console.table(page.stats); // eslint-disable-line no-console
  94. }
  95. return page;
  96. }
  97. function revokeDownloadURL(page) {
  98. URL.revokeObjectURL(page.url);
  99. }
  100. function fixInlineScripts() {
  101. document.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  102. }
  103. function hideElementFrames() {
  104. document.head.querySelectorAll("*:not(meta):not(title):not(link):not(style):not(script)").forEach(element => element.hidden = true);
  105. }
  106. function fixHeadNoScripts() {
  107. document.head.querySelectorAll("noscript").forEach(noscriptElement => document.body.insertBefore(noscriptElement, document.body.firstChild));
  108. }
  109. function markPreserveElements(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME) {
  110. document.querySelectorAll("*").forEach(element => {
  111. const style = getComputedStyle(element);
  112. if (style.whiteSpace.startsWith("pre")) {
  113. element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
  114. }
  115. });
  116. }
  117. function unmarkPreserveElements(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME) {
  118. document.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
  119. }
  120. function markRemovedElements(REMOVED_CONTENT_ATTRIBUTE_NAME) {
  121. document.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object)").forEach(element => {
  122. const style = getComputedStyle(element);
  123. if (element instanceof HTMLElement && (element.hidden || style.display == "none" || ((style.opacity === 0 || style.visibility == "hidden") && !element.clientWidth && !element.clientHeight)) && !element.querySelector("iframe, frame, object[type=\"text/html\"][data]")) {
  124. element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  125. }
  126. });
  127. }
  128. function unmarkRemovedElements(REMOVED_CONTENT_ATTRIBUTE_NAME) {
  129. document.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
  130. }
  131. function markSelectedContent(SELECTED_CONTENT_ATTRIBUTE_NAME, SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME) {
  132. const selection = getSelection();
  133. const range = selection.rangeCount ? selection.getRangeAt(0) : null;
  134. const treeWalker = document.createTreeWalker(range.commonAncestorContainer);
  135. let selectionFound = false;
  136. const ancestorElement = range.commonAncestorContainer != Node.ELEMENT_NODE ? range.commonAncestorContainer.parentElement : range.commonAncestorContainer;
  137. ancestorElement.setAttribute(SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME, "");
  138. while (treeWalker.nextNode() && treeWalker.currentNode != range.endContainer) {
  139. if (treeWalker.currentNode == range.startContainer) {
  140. selectionFound = true;
  141. }
  142. if (selectionFound) {
  143. const element = treeWalker.currentNode.nodeType == Node.ELEMENT_NODE ? treeWalker.currentNode : treeWalker.currentNode.parentElement;
  144. element.setAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME, "");
  145. }
  146. }
  147. }
  148. function unmarkSelectedContent(SELECTED_CONTENT_ATTRIBUTE_NAME, SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME) {
  149. document.querySelectorAll("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME));
  150. document.querySelectorAll("[" + SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME));
  151. }
  152. function removeWindowIdFrames(WIN_ID_ATTRIBUTE_NAME) {
  153. document.querySelectorAll("[" + WIN_ID_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(WIN_ID_ATTRIBUTE_NAME));
  154. }
  155. async function getOptions(options) {
  156. options.canvasData = getCanvasData();
  157. if (!options.removeFrames) {
  158. options.framesData = await FrameTree.getFramesData();
  159. }
  160. options.jsEnabled = true;
  161. options.onprogress = async event => {
  162. if (event.type == event.RESOURCES_INITIALIZED || event.type == event.RESOURCE_LOADED) {
  163. try {
  164. await browser.runtime.sendMessage({ processProgress: true, index: event.details.index, maxIndex: event.details.max });
  165. } catch (error) {
  166. // ignored
  167. }
  168. if (options.shadowEnabled) {
  169. singlefile.ui.onprogress(event);
  170. }
  171. } else if (event.type == event.PAGE_ENDED) {
  172. try {
  173. await browser.runtime.sendMessage({ processEnd: true });
  174. } catch (error) {
  175. // ignored
  176. }
  177. }
  178. };
  179. return options;
  180. }
  181. function getCanvasData() {
  182. const canvasData = [];
  183. document.querySelectorAll("canvas").forEach(canvasElement => {
  184. try {
  185. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  186. } catch (error) {
  187. canvasData.push(null);
  188. }
  189. });
  190. return canvasData;
  191. }
  192. function getDoctype(doc) {
  193. const docType = doc.doctype;
  194. let docTypeString;
  195. if (docType) {
  196. docTypeString = "<!DOCTYPE " + docType.nodeName;
  197. if (docType.publicId) {
  198. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  199. if (docType.systemId)
  200. docTypeString += " \"" + docType.systemId + "\"";
  201. } else if (docType.systemId)
  202. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  203. if (docType.internalSubset)
  204. docTypeString += " [" + docType.internalSubset + "]";
  205. return docTypeString + ">\n";
  206. }
  207. return "";
  208. }
  209. async function downloadPage(page, options) {
  210. const response = await browser.runtime.sendMessage({ download: true, url: page.url, saveAs: options.confirmFilename, filename: page.filename });
  211. if (response.notSupported) {
  212. if (options.confirmFilename) {
  213. page.filename = prompt("File name", page.filename);
  214. }
  215. if (page.filename && page.filename.length) {
  216. const link = document.createElement("a");
  217. document.body.appendChild(link);
  218. link.download = page.filename;
  219. link.href = page.url;
  220. link.dispatchEvent(new MouseEvent("click"));
  221. link.remove();
  222. }
  223. }
  224. }
  225. })();