jsdom.js 3.8 KB

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