jsdom.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. exports.closeBrowser = () => { };
  51. async function getPageData(win, options) {
  52. const doc = win.document;
  53. const scripts = await require("./common/scripts.js").get(options);
  54. win.TextDecoder = class {
  55. constructor(utfLabel) {
  56. this.utfLabel = utfLabel;
  57. }
  58. decode(buffer) {
  59. return iconv.decode(buffer, this.utfLabel);
  60. }
  61. };
  62. win.crypto = {
  63. subtle: {
  64. digest: async function digestText(algo, text) {
  65. const hash = crypto.createHash(algo.replace("-", "").toLowerCase());
  66. hash.update(text, "utf-8");
  67. return hash.digest();
  68. }
  69. }
  70. };
  71. win.Element.prototype.getBoundingClientRect = undefined;
  72. win.getComputedStyle = () => { };
  73. win.eval(scripts);
  74. if (win.document.readyState == "loading" || win.document.readyState == "interactive") {
  75. await new Promise(resolve => win.onload = resolve);
  76. }
  77. executeFrameScripts(doc, scripts);
  78. options.removeHiddenElements = false;
  79. const pageData = await win.singlefile.lib.getPageData(options, { fetch: url => fetchResource(url, options) }, doc, win);
  80. if (options.includeInfobar) {
  81. await win.singlefile.common.ui.content.infobar.includeScript(pageData);
  82. }
  83. return pageData;
  84. }
  85. function getBrowserOptions(options) {
  86. const jsdomOptions = {
  87. url: options.url,
  88. virtualConsole: new VirtualConsole(),
  89. userAgent: options.userAgent,
  90. pretendToBeVisual: true,
  91. runScripts: "outside-only",
  92. resources: "usable"
  93. };
  94. if (options.browserWidth && options.browserHeight) {
  95. jsdomOptions.beforeParse = function (window) {
  96. window.outerWidth = window.innerWidth = options.browserWidth;
  97. window.outerHeight = window.innerHeight = options.browserHeight;
  98. };
  99. }
  100. return jsdomOptions;
  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. }