puppeteer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2010-2019 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 singlefile, require, exports, document, window */
  24. const puppeteer = require("puppeteer-core");
  25. exports.getPageData = async options => {
  26. const browserOptions = {};
  27. if (options.browserHeadless !== undefined) {
  28. browserOptions.headless = options.browserHeadless && !options.browserDebug;
  29. }
  30. browserOptions.args = options.browserArgs ? JSON.parse(options.browserArgs) : [];
  31. if (options.browserDisableWebSecurity === undefined || options.browserDisableWebSecurity) {
  32. browserOptions.args.push("--disable-web-security");
  33. }
  34. browserOptions.args.push("--no-pings");
  35. if (!options.browserHeadless && options.browserDebug) {
  36. browserOptions.args.push("--auto-open-devtools-for-tabs");
  37. }
  38. if (options.browserWidth && options.browserHeight) {
  39. browserOptions.args.push("--window-size=" + options.browserWidth + "," + options.browserHeight);
  40. }
  41. if (options.browserExecutablePath) {
  42. browserOptions.executablePath = options.browserExecutablePath || "chrome";
  43. }
  44. let browser;
  45. try {
  46. browser = await puppeteer.launch(browserOptions);
  47. const page = await browser.newPage();
  48. if (options.userAgent) {
  49. await page.setUserAgent(options.userAgent);
  50. }
  51. if (options.browserWidth && options.browserHeight) {
  52. await page.setViewport({
  53. width: options.browserWidth,
  54. height: options.browserHeight
  55. });
  56. }
  57. if (options.browserBypassCSP === undefined || options.browserBypassCSP) {
  58. await page.setBypassCSP(true);
  59. }
  60. const scripts = await require("./common/scripts.js").get(options);
  61. await page.evaluateOnNewDocument(scripts);
  62. if (options.browserDebug) {
  63. await page.waitFor(3000);
  64. }
  65. await page.goto(options.url, {
  66. timeout: 0,
  67. waitUntil: options.browserWaitUntil || "networkidle0"
  68. });
  69. try {
  70. return await page.evaluate(async options => {
  71. singlefile.lib.helper.initDoc(document);
  72. const preInitializationPromises = [];
  73. if (!options.saveRawPage) {
  74. if (!options.removeFrames) {
  75. preInitializationPromises.push(singlefile.lib.frameTree.content.frames.getAsync(options));
  76. }
  77. if (options.loadDeferredImages) {
  78. preInitializationPromises.push(singlefile.lib.lazy.content.loader.process(options));
  79. }
  80. }
  81. [options.frames] = await Promise.all(preInitializationPromises);
  82. options.doc = document;
  83. options.win = window;
  84. const pageData = await singlefile.lib.getPageData(options);
  85. if (options.includeInfobar) {
  86. await singlefile.common.ui.content.infobar.includeScript(pageData);
  87. }
  88. return pageData;
  89. }, options);
  90. } catch (error) {
  91. if (error.message.includes("Execution context was destroyed")) {
  92. const pages = await browser.pages();
  93. const page = pages[1] || pages[0];
  94. await page.waitForNavigation(options.url, {
  95. timeout: 0,
  96. waitUntil: options.browserWaitUntil || "networkidle0"
  97. });
  98. const url = page.url();
  99. if (url != options.url) {
  100. options.url = url;
  101. await browser.close();
  102. return exports.getPageData(options);
  103. } else {
  104. throw error;
  105. }
  106. } else {
  107. throw error;
  108. }
  109. }
  110. } finally {
  111. if (browser && !options.browserDebug) {
  112. await browser.close();
  113. }
  114. }
  115. };