jsdom.js 4.1 KB

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