jsdom.js 4.0 KB

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