jsdom.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 iconv = require("iconv-lite");
  27. const request = require("request-promise-native");
  28. exports.getPageData = async options => {
  29. const pageContent = (await request({
  30. method: "GET",
  31. uri: options.url,
  32. resolveWithFullResponse: true,
  33. encoding: null,
  34. headers: {
  35. "User-Agent": options.userAgent
  36. }
  37. })).body.toString();
  38. const jsdomOptions = {
  39. url: options.url,
  40. virtualConsole: new VirtualConsole(),
  41. userAgent: options.userAgent,
  42. pretendToBeVisual: true,
  43. runScripts: "outside-only",
  44. resources: "usable"
  45. };
  46. if (options.browserWidth && options.browserHeight) {
  47. jsdomOptions.beforeParse = function (window) {
  48. window.outerWidth = window.innerWidth = options.browserWidth;
  49. window.outerHeight = window.innerHeight = options.browserHeight;
  50. };
  51. }
  52. let dom;
  53. try {
  54. dom = new JSDOM(pageContent, jsdomOptions);
  55. options.insertSingleFileComment = true;
  56. options.insertFaviconLink = true;
  57. const win = dom.window;
  58. const doc = win.document;
  59. const scripts = await require("./common/scripts.js").get(options);
  60. win.TextDecoder = class {
  61. constructor(utfLabel) {
  62. this.utfLabel = utfLabel;
  63. }
  64. decode(buffer) {
  65. return iconv.decode(buffer, this.utfLabel);
  66. }
  67. };
  68. win.crypto = {
  69. subtle: {
  70. digest: async function digestText(algo, text) {
  71. const hash = crypto.createHash(algo.replace("-", "").toLowerCase());
  72. hash.update(text, "utf-8");
  73. return hash.digest();
  74. }
  75. }
  76. };
  77. dom.window.eval(scripts);
  78. if (dom.window.document.readyState == "loading") {
  79. await new Promise(resolve => win.document.onload = resolve);
  80. }
  81. win.Element.prototype.getBoundingClientRect = undefined;
  82. executeFrameScripts(doc, scripts);
  83. if (!options.saveRawPage && !options.removeFrames) {
  84. options.frames = await win.singlefile.lib.frameTree.content.frames.getAsync(options);
  85. }
  86. options.win = win;
  87. options.doc = doc;
  88. options.removeHiddenElements = false;
  89. const SingleFile = win.singlefile.lib.SingleFile.getClass({ fetch: url => fetchResource(url, options) });
  90. const singleFile = new SingleFile(options);
  91. await singleFile.run();
  92. const pageData = await singleFile.getPageData();
  93. if (options.includeInfobar) {
  94. await win.singlefile.common.ui.content.infobar.includeScript(pageData);
  95. }
  96. return pageData;
  97. } finally {
  98. if (dom && dom.window) {
  99. dom.window.close();
  100. }
  101. }
  102. };
  103. function executeFrameScripts(doc, scripts) {
  104. const frameElements = doc.querySelectorAll("iframe, frame");
  105. frameElements.forEach(frameElement => {
  106. try {
  107. frameElement.contentWindow.Element.prototype.getBoundingClientRect = undefined;
  108. frameElement.contentWindow.eval(scripts);
  109. executeFrameScripts(frameElement.contentDocument, scripts);
  110. } catch (error) {
  111. // ignored
  112. }
  113. });
  114. }
  115. async function fetchResource(resourceURL, options) {
  116. const response = await request({
  117. method: "GET",
  118. uri: resourceURL,
  119. resolveWithFullResponse: true,
  120. encoding: null,
  121. headers: {
  122. "User-Agent": options.userAgent
  123. }
  124. });
  125. return {
  126. status: response.statusCode,
  127. headers: {
  128. get: name => response.headers[name]
  129. },
  130. arrayBuffer: async () => response.body
  131. };
  132. }