content.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. disableNoscriptTags();
  53. hideNonMetadataContents();
  54. if (options.selected) {
  55. markSelectedContent(processor.SELECTED_CONTENT_ATTRIBUTE_NAME, processor.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME);
  56. }
  57. if (options.removeHiddenElements) {
  58. markRemovedElements(processor.REMOVED_CONTENT_ATTRIBUTE_NAME);
  59. }
  60. if (options.compressHTML) {
  61. markPreserveElements(processor.PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME);
  62. }
  63. options.url = options.url || document.location.href;
  64. options.content = options.content || getDoctype(document) + document.documentElement.outerHTML;
  65. await processor.initialize();
  66. if (options.shadowEnabled) {
  67. singlefile.ui.init();
  68. }
  69. enableDisabledNoscriptTags();
  70. displayHiddenNonMetadataContents();
  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 disableNoscriptTags() {
  101. document.head.querySelectorAll("noscript").forEach(element => {
  102. const disabledNoscriptElement = document.createElement("disabled-noscript");
  103. Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
  104. disabledNoscriptElement.hidden = true;
  105. element.parentElement.replaceChild(disabledNoscriptElement, element);
  106. });
  107. }
  108. function enableDisabledNoscriptTags() {
  109. document.head.querySelectorAll("disabled-noscript").forEach(element => {
  110. const noscriptElement = document.createElement("noscript");
  111. Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
  112. element.parentElement.replaceChild(noscriptElement, element);
  113. });
  114. }
  115. function hideNonMetadataContents() {
  116. document.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
  117. }
  118. function displayHiddenNonMetadataContents() {
  119. document.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  120. }
  121. function fixInlineScripts() {
  122. document.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  123. }
  124. function markPreserveElements(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME) {
  125. document.querySelectorAll("*").forEach(element => {
  126. const style = getComputedStyle(element);
  127. if (style.whiteSpace.startsWith("pre")) {
  128. element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
  129. }
  130. });
  131. }
  132. function unmarkPreserveElements(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME) {
  133. document.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
  134. }
  135. function markRemovedElements(REMOVED_CONTENT_ATTRIBUTE_NAME) {
  136. document.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object)").forEach(element => {
  137. const style = getComputedStyle(element);
  138. 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]")) {
  139. element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  140. }
  141. });
  142. }
  143. function unmarkRemovedElements(REMOVED_CONTENT_ATTRIBUTE_NAME) {
  144. document.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
  145. }
  146. function markSelectedContent(SELECTED_CONTENT_ATTRIBUTE_NAME, SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME) {
  147. const selection = getSelection();
  148. const range = selection.rangeCount ? selection.getRangeAt(0) : null;
  149. const treeWalker = document.createTreeWalker(range.commonAncestorContainer);
  150. let selectionFound = false;
  151. const ancestorElement = range.commonAncestorContainer != Node.ELEMENT_NODE ? range.commonAncestorContainer.parentElement : range.commonAncestorContainer;
  152. ancestorElement.setAttribute(SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME, "");
  153. while (treeWalker.nextNode() && treeWalker.currentNode != range.endContainer) {
  154. if (treeWalker.currentNode == range.startContainer) {
  155. selectionFound = true;
  156. }
  157. if (selectionFound) {
  158. const element = treeWalker.currentNode.nodeType == Node.ELEMENT_NODE ? treeWalker.currentNode : treeWalker.currentNode.parentElement;
  159. element.setAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME, "");
  160. }
  161. }
  162. }
  163. function unmarkSelectedContent(SELECTED_CONTENT_ATTRIBUTE_NAME, SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME) {
  164. document.querySelectorAll("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME));
  165. document.querySelectorAll("[" + SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME));
  166. }
  167. function removeWindowIdFrames(WIN_ID_ATTRIBUTE_NAME) {
  168. document.querySelectorAll("[" + WIN_ID_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(WIN_ID_ATTRIBUTE_NAME));
  169. }
  170. async function getOptions(options) {
  171. options.canvasData = getCanvasData();
  172. options.emptyStyleRulesText = getEmptyStyleRulesText();
  173. if (!options.removeFrames) {
  174. options.framesData = await FrameTree.getFramesData();
  175. }
  176. options.jsEnabled = true;
  177. options.onprogress = async event => {
  178. if (event.type == event.RESOURCES_INITIALIZED || event.type == event.RESOURCE_LOADED) {
  179. try {
  180. await browser.runtime.sendMessage({ processProgress: true, index: event.details.index, maxIndex: event.details.max });
  181. } catch (error) {
  182. // ignored
  183. }
  184. if (options.shadowEnabled) {
  185. singlefile.ui.onprogress(event);
  186. }
  187. } else if (event.type == event.PAGE_ENDED) {
  188. try {
  189. await browser.runtime.sendMessage({ processEnd: true });
  190. } catch (error) {
  191. // ignored
  192. }
  193. }
  194. };
  195. return options;
  196. }
  197. function getEmptyStyleRulesText() {
  198. const textData = [];
  199. document.querySelectorAll("style").forEach(styleElement => {
  200. if (!styleElement.textContent) {
  201. textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
  202. }
  203. });
  204. return textData;
  205. }
  206. function getCanvasData() {
  207. const canvasData = [];
  208. document.querySelectorAll("canvas").forEach(canvasElement => {
  209. try {
  210. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  211. } catch (error) {
  212. canvasData.push(null);
  213. }
  214. });
  215. return canvasData;
  216. }
  217. function getDoctype(doc) {
  218. const docType = doc.doctype;
  219. let docTypeString;
  220. if (docType) {
  221. docTypeString = "<!DOCTYPE " + docType.nodeName;
  222. if (docType.publicId) {
  223. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  224. if (docType.systemId)
  225. docTypeString += " \"" + docType.systemId + "\"";
  226. } else if (docType.systemId)
  227. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  228. if (docType.internalSubset)
  229. docTypeString += " [" + docType.internalSubset + "]";
  230. return docTypeString + ">\n";
  231. }
  232. return "";
  233. }
  234. async function downloadPage(page, options) {
  235. const response = await browser.runtime.sendMessage({ download: true, url: page.url, saveAs: options.confirmFilename, filename: page.filename });
  236. if (response.notSupported) {
  237. if (options.confirmFilename) {
  238. page.filename = prompt("File name", page.filename);
  239. }
  240. if (page.filename && page.filename.length) {
  241. const link = document.createElement("a");
  242. document.body.appendChild(link);
  243. link.download = page.filename;
  244. link.href = page.url;
  245. link.dispatchEvent(new MouseEvent("click"));
  246. link.remove();
  247. }
  248. }
  249. }
  250. })();