puppeteer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. options = await singlefile.lib.initializeOptions(options);
  72. const pageData = await singlefile.lib.getPageData(options);
  73. if (options.includeInfobar) {
  74. await singlefile.common.ui.content.infobar.includeScript(pageData);
  75. }
  76. return pageData;
  77. }, options);
  78. } catch (error) {
  79. if (error.message.includes("Execution context was destroyed")) {
  80. const pages = await browser.pages();
  81. const page = pages[1] || pages[0];
  82. await page.waitForNavigation(options.url, {
  83. timeout: 0,
  84. waitUntil: options.browserWaitUntil || "networkidle0"
  85. });
  86. const url = page.url();
  87. if (url != options.url) {
  88. options.url = url;
  89. await browser.close();
  90. return exports.getPageData(options);
  91. } else {
  92. throw error;
  93. }
  94. } else {
  95. throw error;
  96. }
  97. }
  98. } finally {
  99. if (browser && !options.browserDebug) {
  100. await browser.close();
  101. }
  102. }
  103. };