jsdom.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global require, exports */
  24. const crypto = require("crypto");
  25. const { JSDOM, VirtualConsole } = require("jsdom");
  26. const dataUri = require("strong-data-uri");
  27. const iconv = require("iconv-lite");
  28. const request = require("request-promise-native");
  29. exports.getPageData = async options => {
  30. const pageContent = (await request({
  31. method: "GET",
  32. uri: options.url,
  33. resolveWithFullResponse: true,
  34. encoding: null,
  35. headers: {
  36. "User-Agent": options.userAgent
  37. }
  38. })).body.toString();
  39. const jsdomOptions = {
  40. url: options.url,
  41. virtualConsole: new VirtualConsole(),
  42. userAgent: options.userAgent,
  43. pretendToBeVisual: true,
  44. runScripts: "outside-only",
  45. resources: "usable"
  46. };
  47. if (options.browserWidth && options.browserHeight) {
  48. jsdomOptions.beforeParse = function (window) {
  49. window.outerWidth = window.innerWidth = options.browserWidth;
  50. window.outerHeight = window.innerHeight = options.browserHeight;
  51. };
  52. }
  53. let dom;
  54. try {
  55. dom = new JSDOM(pageContent, jsdomOptions);
  56. options.insertSingleFileComment = true;
  57. options.insertFaviconLink = true;
  58. const win = dom.window;
  59. const doc = win.document;
  60. const scripts = await require("./common/scripts.js").get(options);
  61. dom.window.eval(scripts);
  62. if (dom.window.document.readyState == "loading") {
  63. await new Promise(resolve => win.document.onload = resolve);
  64. }
  65. win.Element.prototype.getBoundingClientRect = undefined;
  66. executeFrameScripts(doc, scripts);
  67. if (!options.saveRawPage && !options.removeFrames) {
  68. options.frames = await win.singlefile.lib.frameTree.content.frames.getAsync(options);
  69. }
  70. options.win = win;
  71. options.doc = doc;
  72. options.removeHiddenElements = false;
  73. const SingleFile = getSingleFileClass(win, options);
  74. const singleFile = new SingleFile(options);
  75. await singleFile.run();
  76. const pageData = await singleFile.getPageData();
  77. if (options.includeInfobar) {
  78. await win.singlefile.common.ui.content.infobar.includeScript(pageData);
  79. }
  80. return pageData;
  81. } finally {
  82. if (dom && dom.window) {
  83. dom.window.close();
  84. }
  85. }
  86. };
  87. function executeFrameScripts(doc, scripts) {
  88. const frameElements = doc.querySelectorAll("iframe, frame");
  89. frameElements.forEach(frameElement => {
  90. try {
  91. frameElement.contentWindow.Element.prototype.getBoundingClientRect = undefined;
  92. frameElement.contentWindow.eval(scripts);
  93. executeFrameScripts(frameElement.contentDocument, scripts);
  94. } catch (error) {
  95. // ignored
  96. }
  97. });
  98. }
  99. function getSingleFileClass(win, options) {
  100. const helper = win.singlefile.lib.helper;
  101. const modules = {
  102. helper: helper,
  103. srcsetParser: win.singlefile.lib.vendor.srcsetParser,
  104. cssMinifier: win.singlefile.lib.vendor.cssMinifier,
  105. htmlMinifier: win.singlefile.lib.modules.htmlMinifier,
  106. serializer: win.singlefile.lib.modules.serializer,
  107. fontsMinifier: win.singlefile.lib.modules.fontsMinifier,
  108. fontsAltMinifier: win.singlefile.lib.modules.fontsAltMinifier,
  109. cssRulesMinifier: win.singlefile.lib.modules.cssRulesMinifier,
  110. matchedRules: win.singlefile.lib.modules.matchedRules,
  111. mediasAltMinifier: win.singlefile.lib.modules.mediasAltMinifier,
  112. imagesAltMinifier: win.singlefile.lib.modules.imagesAltMinifier
  113. };
  114. const domUtil = {
  115. getResourceContent: resourceURL => getResourceContent(resourceURL, options),
  116. digestText
  117. };
  118. return win.singlefile.lib.core.getClass(win.singlefile.lib.util.getInstance(modules, domUtil), win.singlefile.lib.vendor.cssTree);
  119. }
  120. async function digestText(algo, text) {
  121. const hash = crypto.createHash(algo.replace("-", "").toLowerCase());
  122. hash.update(text, "utf-8");
  123. return hash.digest("hex");
  124. }
  125. async function getResourceContent(resourceURL, options) {
  126. const resourceContent = await request({
  127. method: "GET",
  128. uri: resourceURL,
  129. resolveWithFullResponse: true,
  130. encoding: null,
  131. headers: {
  132. "User-Agent": options.userAgent
  133. }
  134. });
  135. return {
  136. getUrl() {
  137. return resourceContent.request.href;
  138. },
  139. getContentType() {
  140. return resourceContent.headers["content-type"];
  141. },
  142. getStatusCode() {
  143. return resourceContent.statusCode;
  144. },
  145. getSize() {
  146. return resourceContent.body.byteLength;
  147. },
  148. getText(charset) {
  149. return iconv.decode(resourceContent.body, charset);
  150. },
  151. async getDataUri(contentType) {
  152. return dataUri.encode(resourceContent.body, contentType || this.getContentType());
  153. }
  154. };
  155. }