doc-helper.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. this.docHelper = this.docHelper || (() => {
  21. const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
  22. const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-single-file-preserved-space-element";
  23. const WIN_ID_ATTRIBUTE_NAME = "data-frame-tree-win-id";
  24. const RESPONSIVE_IMAGE_ATTRIBUTE_NAME = "data-single-file-responsive-image";
  25. return {
  26. preProcessDoc,
  27. postProcessDoc,
  28. serialize,
  29. windowIdAttributeName,
  30. preservedSpaceAttributeName,
  31. removedContentAttributeName,
  32. responsiveImagesAttributeName
  33. };
  34. function preProcessDoc(doc, win, options) {
  35. doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  36. doc.querySelectorAll("noscript").forEach(element => {
  37. const disabledNoscriptElement = doc.createElement("disabled-noscript");
  38. Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
  39. disabledNoscriptElement.hidden = true;
  40. element.parentElement.replaceChild(disabledNoscriptElement, element);
  41. });
  42. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
  43. if (options.removeHiddenElements) {
  44. doc.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object):not(meta):not(title):not(meta):not(noscript):not(template)").forEach(element => {
  45. const style = win.getComputedStyle(element);
  46. if (element instanceof win.HTMLElement && style && (element.hidden || style.display == "none" || ((style.opacity === 0 || style.visibility == "hidden") && !element.clientWidth && !element.clientHeight)) && !element.querySelector("iframe, frame, object[type=\"text/html\"][data]")) {
  47. element.setAttribute(removedContentAttributeName(options.sessionId), "");
  48. }
  49. });
  50. }
  51. if (options.compressHTML) {
  52. doc.querySelectorAll("*").forEach(element => {
  53. const style = win.getComputedStyle(element);
  54. if (style && style.whiteSpace.startsWith("pre")) {
  55. element.setAttribute(preservedSpaceAttributeName(options.sessionId), "");
  56. }
  57. });
  58. }
  59. return {
  60. canvasData: getCanvasData(doc),
  61. stylesheetContents: getStylesheetContents(doc),
  62. imageData: getImageData(doc, options)
  63. };
  64. }
  65. function postProcessDoc(doc, options) {
  66. doc.querySelectorAll("disabled-noscript").forEach(element => {
  67. const noscriptElement = doc.createElement("noscript");
  68. Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
  69. element.parentElement.replaceChild(noscriptElement, element);
  70. });
  71. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  72. if (options.removeHiddenElements) {
  73. doc.querySelectorAll("[" + removedContentAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(removedContentAttributeName(options.sessionId)));
  74. }
  75. if (options.compressHTML) {
  76. doc.querySelectorAll("[" + preservedSpaceAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(preservedSpaceAttributeName(options.sessionId)));
  77. }
  78. doc.querySelectorAll("[" + responsiveImagesAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(responsiveImagesAttributeName(options.sessionId)));
  79. }
  80. function responsiveImagesAttributeName(sessionId) {
  81. return RESPONSIVE_IMAGE_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  82. }
  83. function preservedSpaceAttributeName(sessionId) {
  84. return PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  85. }
  86. function removedContentAttributeName(sessionId) {
  87. return REMOVED_CONTENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  88. }
  89. function windowIdAttributeName(sessionId) {
  90. return WIN_ID_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  91. }
  92. function getCanvasData(doc) {
  93. if (doc) {
  94. const canvasData = [];
  95. doc.querySelectorAll("canvas").forEach(canvasElement => {
  96. try {
  97. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  98. } catch (error) {
  99. canvasData.push(null);
  100. }
  101. });
  102. return canvasData;
  103. }
  104. }
  105. function getStylesheetContents(doc) {
  106. if (doc) {
  107. const contents = [];
  108. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  109. try {
  110. contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n");
  111. } catch (error) {
  112. /* ignored */
  113. }
  114. });
  115. return contents;
  116. }
  117. }
  118. function getImageData(doc, options) {
  119. if (doc) {
  120. const data = [];
  121. doc.querySelectorAll("picture, img[srcset]").forEach((element, elementIndex) => {
  122. const tagName = element.tagName.toLowerCase();
  123. let imageData = {}, imageElement;
  124. element.setAttribute(responsiveImagesAttributeName(options.sessionId), elementIndex);
  125. if (tagName == "picture") {
  126. const sources = Array.from(element.querySelectorAll("source")).map(sourceElement => (
  127. { src: sourceElement.src, srcset: sourceElement.srcset }
  128. ));
  129. imageElement = element.querySelector("img");
  130. imageData.sources = sources;
  131. }
  132. if (tagName == "img") {
  133. imageElement = element;
  134. }
  135. imageData.source = {
  136. clientWidth: imageElement.clientWidth,
  137. clientHeight: imageElement.clientHeight,
  138. naturalWidth: imageElement.naturalWidth,
  139. naturalHeight: imageElement.naturalHeight,
  140. width: imageElement.width,
  141. height: imageElement.height,
  142. src: (!imageElement.currentSrc.startsWith("data:") && imageElement.currentSrc) || (!imageElement.src.startsWith("data:") && imageElement.src)
  143. };
  144. data.push(imageData);
  145. });
  146. return data;
  147. }
  148. }
  149. function serialize(doc) {
  150. const docType = doc.doctype;
  151. let docTypeString = "";
  152. if (docType) {
  153. docTypeString = "<!DOCTYPE " + docType.nodeName;
  154. if (docType.publicId) {
  155. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  156. if (docType.systemId) {
  157. docTypeString += " \"" + docType.systemId + "\"";
  158. }
  159. } else if (docType.systemId) {
  160. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  161. } if (docType.internalSubset) {
  162. docTypeString += " [" + docType.internalSubset + "]";
  163. }
  164. docTypeString += "> ";
  165. }
  166. return docTypeString + doc.documentElement.outerHTML;
  167. }
  168. })();