puppeteer-firefox.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, infobar, require, exports */
  24. const puppeteer = require("puppeteer-core");
  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;
  29. exports.initialize = async options => {
  30. browser = await puppeteer.launch(getBrowserOptions(options));
  31. };
  32. exports.getPageData = async options => {
  33. let page;
  34. try {
  35. page = await browser.newPage();
  36. await setPageOptions(page, options);
  37. return await getPageData(browser, page, options);
  38. } finally {
  39. if (page) {
  40. await page.close();
  41. }
  42. }
  43. };
  44. exports.closeBrowser = () => {
  45. if (browser) {
  46. return browser.close();
  47. }
  48. };
  49. function getBrowserOptions(options) {
  50. const browserOptions = {};
  51. if (options.browserHeadless !== undefined) {
  52. browserOptions.headless = options.browserHeadless && !options.browserDebug;
  53. }
  54. browserOptions.args = options.browserArgs ? JSON.parse(options.browserArgs) : [];
  55. if (options.browserExecutablePath) {
  56. browserOptions.executablePath = options.browserExecutablePath || "firefox";
  57. }
  58. browserOptions.product = "firefox";
  59. return browserOptions;
  60. }
  61. async function setPageOptions(page, options) {
  62. if (options.browserWidth && options.browserHeight) {
  63. await page.setViewport({
  64. width: options.browserWidth,
  65. height: options.browserHeight
  66. });
  67. }
  68. if ((options.browserBypassCSP === undefined || options.browserBypassCSP) && page.setBypassCSP) {
  69. try {
  70. await page.setBypassCSP(true);
  71. } catch (error) {
  72. // ignored
  73. }
  74. }
  75. if (options.httpHeaders) {
  76. try {
  77. await page.setExtraHTTPHeaders(options.httpHeaders);
  78. } catch (error) {
  79. // ignored
  80. }
  81. }
  82. if (options.browserCookies && options.browserCookies.length) {
  83. await page.setCookie(...options.browserCookies);
  84. }
  85. if (options.emulateMediaFeatures) {
  86. try {
  87. await page.emulateMediaFeatures(options.emulateMediaFeatures);
  88. } catch (error) {
  89. // ignored
  90. }
  91. }
  92. }
  93. async function getPageData(browser, page, options) {
  94. const injectedScript = await scripts.get(options);
  95. await page.evaluateOnNewDocument(injectedScript);
  96. if (options.browserDebug) {
  97. await page.waitForTimeout(3000);
  98. }
  99. await pageGoto(page, options);
  100. try {
  101. await page.evaluate(injectedScript);
  102. if (options.browserWaitDelay) {
  103. await page.waitForTimeout(options.browserWaitDelay);
  104. }
  105. return await page.evaluate(async options => {
  106. const pageData = await singlefile.getPageData(options);
  107. if (options.includeInfobar) {
  108. await infobar.includeScript(pageData);
  109. }
  110. return pageData;
  111. }, options);
  112. } catch (error) {
  113. if (error.message && error.message.includes(EXECUTION_CONTEXT_DESTROYED_ERROR)) {
  114. const pageData = await handleJSRedirect(browser, options);
  115. if (pageData) {
  116. return pageData;
  117. } else {
  118. throw error;
  119. }
  120. } else if (error.name != "TimeoutError") {
  121. throw error;
  122. }
  123. }
  124. }
  125. async function handleJSRedirect(browser, options) {
  126. const pages = await browser.pages();
  127. const page = pages[1] || pages[0];
  128. try {
  129. await pageGoto(page, options);
  130. } catch (error) {
  131. if (error.name != "TimeoutError") {
  132. throw error;
  133. }
  134. }
  135. const url = page.url();
  136. if (url != options.url) {
  137. options.url = url;
  138. await browser.close();
  139. return exports.getPageData(options);
  140. }
  141. }
  142. async function pageGoto(page, options) {
  143. try {
  144. await page.goto(options.url, {
  145. timeout: options.browserLoadMaxTime || 0,
  146. waitUntil: options.browserWaitUntil || NETWORK_IDLE_STATE
  147. });
  148. } catch (error) {
  149. if (error.message.includes("Unknown waitUntil condition")) {
  150. await page.goto(options.url, {
  151. timeout: options.browserLoadMaxTime || 0,
  152. waitUntil: "load"
  153. });
  154. } else {
  155. throw error;
  156. }
  157. }
  158. }