client.js 4.2 KB

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