client.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. document.querySelectorAll("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME));
  51. options.jsEnabled = true;
  52. options.onprogress = onProgress;
  53. return options;
  54. }
  55. function markSelectedContent() {
  56. const selection = getSelection();
  57. const range = selection.rangeCount ? selection.getRangeAt(0) : null;
  58. let node;
  59. if (range && range.startOffset != range.endOffset) {
  60. node = range.commonAncestorContainer;
  61. if (node.nodeType != node.ELEMENT_NODE) {
  62. node = node.parentElement;
  63. }
  64. }
  65. node.setAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME, "");
  66. }
  67. function getDoctype(doc) {
  68. const docType = doc.doctype;
  69. let docTypeString;
  70. if (docType) {
  71. docTypeString = "<!DOCTYPE " + docType.nodeName;
  72. if (docType.publicId) {
  73. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  74. if (docType.systemId)
  75. docTypeString += " \"" + docType.systemId + "\"";
  76. } else if (docType.systemId)
  77. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  78. if (docType.internalSubset)
  79. docTypeString += " [" + docType.internalSubset + "]";
  80. return docTypeString + ">\n";
  81. }
  82. return "";
  83. }
  84. function onProgress(event) {
  85. if (event.type == event.RESOURCES_INITIALIZED) {
  86. chrome.runtime.sendMessage({
  87. processStart: true,
  88. index: event.details.index,
  89. maxIndex: event.details.max
  90. });
  91. }
  92. if (event.type == event.RESOURCE_LOADED) {
  93. chrome.runtime.sendMessage({
  94. processProgress: true,
  95. index: event.details.index,
  96. maxIndex: event.details.max
  97. });
  98. }
  99. if (event.type == event.PAGE_ENDED) {
  100. chrome.runtime.sendMessage({ processEnd: true });
  101. }
  102. }
  103. function downloadPage(page) {
  104. const link = document.createElement("a");
  105. document.body.appendChild(link);
  106. link.download = page.filename;
  107. link.href = page.url;
  108. link.dispatchEvent(new MouseEvent("click"));
  109. link.remove();
  110. }
  111. })();