jsdom.js 4.0 KB

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