jsdom.js 4.1 KB

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