puppeteer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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-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. const NETWORK_STATES = ["networkidle0", "networkidle2", "load", "domcontentloaded"];
  29. let browser;
  30. exports.initialize = async options => {
  31. browser = await puppeteer.launch(getBrowserOptions(options));
  32. };
  33. exports.getPageData = async options => {
  34. let page;
  35. try {
  36. page = await browser.newPage();
  37. await setPageOptions(page, options);
  38. return await getPageData(browser, page, options);
  39. } finally {
  40. if (page) {
  41. await page.close();
  42. }
  43. }
  44. };
  45. exports.closeBrowser = () => {
  46. if (browser) {
  47. return browser.close();
  48. }
  49. };
  50. function getBrowserOptions(options) {
  51. const browserOptions = {};
  52. if (options.browserHeadless !== undefined) {
  53. browserOptions.headless = options.browserHeadless && !options.browserDebug;
  54. }
  55. browserOptions.args = options.browserArgs ? JSON.parse(options.browserArgs) : [];
  56. if (options.browserDisableWebSecurity === undefined || options.browserDisableWebSecurity) {
  57. browserOptions.args.push("--disable-web-security");
  58. }
  59. browserOptions.args.push("--no-pings");
  60. if (options.browserDebug) {
  61. browserOptions.args.push("--auto-open-devtools-for-tabs");
  62. }
  63. if (options.browserWidth && options.browserHeight) {
  64. browserOptions.args.push("--window-size=" + options.browserWidth + "," + options.browserHeight);
  65. }
  66. if (options.browserExecutablePath) {
  67. browserOptions.executablePath = options.browserExecutablePath || "chrome";
  68. }
  69. if (options.userAgent) {
  70. browserOptions.args.push("--user-agent=" + options.userAgent);
  71. }
  72. return browserOptions;
  73. }
  74. async function setPageOptions(page, options) {
  75. if (options.browserWidth && options.browserHeight) {
  76. await page.setViewport({
  77. width: options.browserWidth,
  78. height: options.browserHeight
  79. });
  80. }
  81. if (options.browserBypassCSP === undefined || options.browserBypassCSP) {
  82. await page.setBypassCSP(true);
  83. }
  84. }
  85. async function getPageData(browser, page, options) {
  86. const injectedScript = await scripts.get(options);
  87. await page.evaluateOnNewDocument(injectedScript);
  88. if (options.browserDebug) {
  89. await page.waitFor(3000);
  90. }
  91. try {
  92. await pageGoto(page, options);
  93. } catch (error) {
  94. if (options.browserWaitUntilFallback && error.name == "TimeoutError") {
  95. const browserWaitUntil = NETWORK_STATES[(NETWORK_STATES.indexOf(options.browserWaitUntil) + 1)];
  96. if (browserWaitUntil) {
  97. options.browserWaitUntil = browserWaitUntil;
  98. return getPageData(browser, page, options);
  99. } else {
  100. throw error;
  101. }
  102. } else {
  103. throw error;
  104. }
  105. }
  106. try {
  107. return await page.evaluate(async options => {
  108. const pageData = await singlefile.lib.getPageData(options);
  109. if (options.includeInfobar) {
  110. await singlefile.common.ui.content.infobar.includeScript(pageData);
  111. }
  112. return pageData;
  113. }, options);
  114. } catch (error) {
  115. if (error.message && error.message.includes(EXECUTION_CONTEXT_DESTROYED_ERROR)) {
  116. const pageData = await handleJSRedirect(browser, options);
  117. if (pageData) {
  118. return pageData;
  119. } else {
  120. throw error;
  121. }
  122. } else {
  123. throw error;
  124. }
  125. }
  126. }
  127. async function handleJSRedirect(browser, options) {
  128. const pages = await browser.pages();
  129. const page = pages[1] || pages[0];
  130. await pageGoto(page, options);
  131. const url = page.url();
  132. if (url != options.url) {
  133. options.url = url;
  134. await browser.close();
  135. return exports.getPageData(options);
  136. }
  137. }
  138. async function pageGoto(page, options) {
  139. await page.goto(options.url, {
  140. timeout: options.browserLoadMaxTime || 0,
  141. waitUntil: options.browserWaitUntil || NETWORK_IDLE_STATE
  142. });
  143. }