puppeteer-firefox.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 */
  24. const puppeteer = require("puppeteer-firefox");
  25. const scripts = require("./common/scripts.js");
  26. const EXECUTION_CONTEXT_DESTROYED_ERROR = "Execution context was destroyed";
  27. const NETWORK_IDLE_STATE = "networkidle0";
  28. exports.getPageData = async options => {
  29. let browser;
  30. try {
  31. browser = await puppeteer.launch(getBrowserOptions(options));
  32. const page = await browser.newPage();
  33. await setPageOptions(page, options);
  34. return await getPageData(browser, page, options);
  35. } finally {
  36. if (browser && !options.browserDebug) {
  37. await browser.close();
  38. }
  39. }
  40. };
  41. function getBrowserOptions(options) {
  42. const browserOptions = {};
  43. if (options.browserHeadless !== undefined) {
  44. browserOptions.headless = options.browserHeadless && !options.browserDebug;
  45. }
  46. browserOptions.args = options.browserArgs ? JSON.parse(options.browserArgs) : [];
  47. if (options.browserExecutablePath) {
  48. browserOptions.executablePath = options.browserExecutablePath || "firefox";
  49. }
  50. return browserOptions;
  51. }
  52. async function setPageOptions(page, options) {
  53. if (options.browserWidth && options.browserHeight) {
  54. await page.setViewport({
  55. width: options.browserWidth,
  56. height: options.browserHeight
  57. });
  58. }
  59. if ((options.browserBypassCSP === undefined || options.browserBypassCSP) && page.setBypassCSP) {
  60. await page.setBypassCSP(true);
  61. }
  62. }
  63. async function getPageData(browser, page, options) {
  64. const injectedScript = await scripts.get(options);
  65. await page.evaluateOnNewDocument(injectedScript);
  66. if (options.browserDebug) {
  67. await page.waitFor(3000);
  68. }
  69. await pageGoto(page, options);
  70. try {
  71. return await page.evaluate(async 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 && error.message.includes(EXECUTION_CONTEXT_DESTROYED_ERROR)) {
  80. const pageData = await handleJSRedirect(browser, options);
  81. if (pageData) {
  82. return pageData;
  83. } else {
  84. throw error;
  85. }
  86. } else {
  87. throw error;
  88. }
  89. }
  90. }
  91. async function handleJSRedirect(browser, options) {
  92. const pages = await browser.pages();
  93. const page = pages[1] || pages[0];
  94. await pageGoto(page, options);
  95. const url = page.url();
  96. if (url != options.url) {
  97. options.url = url;
  98. await browser.close();
  99. return exports.getPageData(options);
  100. }
  101. }
  102. async function pageGoto(page, options) {
  103. try {
  104. await page.goto(options.url, {
  105. timeout: options.browserLoadMaxTime || 0,
  106. waitUntil: options.browserWaitUntil || NETWORK_IDLE_STATE
  107. });
  108. } catch (error) {
  109. if (error.name != "TimeoutError") {
  110. if (error.message.includes("Unknown waitUntil condition")) {
  111. await page.goto(options.url, {
  112. timeout: options.browserLoadMaxTime || 0,
  113. waitUntil: "load"
  114. });
  115. } else {
  116. throw error;
  117. }
  118. }
  119. }
  120. }