puppeteer-firefox.js 3.9 KB

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