Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to reuse a browser by passing a browserContext #884

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

daniel-hauser
Copy link
Contributor

This pull request changes the page creation logic by alowing a browserContext to be passes instead of a browser (that is currently being closed when the scraping ends)

@daniel-hauser daniel-hauser changed the title feat: Allow to reuse a browser by passing a browserContext feat: allow to reuse a browser by passing a browserContext Oct 3, 2024
@daniel-hauser daniel-hauser marked this pull request as ready for review October 5, 2024 20:36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no suggestions.

Comment on lines +207 to +209
async function fetchTransactions(page: Page, options: ScraperOptions, companyServiceOptions:CompanyServiceOptions, startMoment: Moment, monthMoment: Moment): Promise<ScrapedAccountsWithIndex> {
const accounts = await fetchAccounts(page, companyServiceOptions.servicesUrl, monthMoment);
const dataUrl = getTransactionsUrl(companyServiceOptions.servicesUrl, monthMoment);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have to split the companyServiceOptions from options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fields are not ScraperOptions but amex/isracard specific values that are internal to this file - defined inside fetchData and merged with the scraper options.

Comment on lines +134 to +144
private async initializePage() {
debug('initialize browser page');
if ('browserContext' in this.options) {
debug('Using the browser context provided in options');
return this.options.browserContext.newPage();
}

if ('browser' in this.options) {
debug('Using the browser instance provided in options');
const { browser } = this.options;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to review it, but I don't have a time for it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daniel-hauser sorry, I really want to review this, but I found myself playing מצא את ההבדלים from my phone when I start to review it.

Usually, this PR is OK, but since we are trying to work on it on our non-existent free time, I need you to explain to me what you did so I will understand what I see.

Copy link
Contributor Author

@daniel-hauser daniel-hauser Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baruchiro, I aggrege that reviewing large changes in GH mobile is hard.
Attached here is the o1 answer for the changes, LMK if it makes the review easier.


AI Generated:

Longe explanation:

Changes Introduced:

  • Added Support for External BrowserContext:
    • New Logic Added:
      • In the initializePage() method, we now first check if a browserContext is provided in this.options.
      • If a browserContext is provided:
        • We create a new page using browserContext.newPage().
        • This allows users to supply their own BrowserContext, offering more flexibility when the browser context is managed externally.

Logic Unchanged:

  • Existing Browser Initialization Logic Remains the Same:

    • If no browserContext is provided:
      • We proceed to check if a browser is provided in this.options (same as before).
        • If a browser is provided:
          • We log a debug message: 'Using the browser instance provided in options'.
          • We create a new page using browser.newPage().
          • For backward compatibility, if skipCloseBrowser is not set to true, we add a cleanup function to close the browser after scraping.
        • If no browser is provided:
          • We launch a new browser instance as before, using options like executablePath, args, showBrowser, and timeout from this.options.
          • We add a cleanup function to close the browser after scraping.
          • If prepareBrowser is provided, we call it with the new browser instance.
  • Resource Cleanup Logic Remains the Same:

    • We maintain a cleanups array to manage cleanup functions.
    • During termination (terminate() method), we execute all cleanup functions in reverse order to ensure proper resource disposal.
    • This includes closing pages and browsers as needed.
  • Page Creation and Return:

    • After handling the browser or browser context, we create a new page and return it.
    • This part of the logic is unchanged.

Summary:

  • What's New:

    • Added support for an external BrowserContext in initializePage(), allowing more flexibility in browser management.
  • What's Unchanged:

    • The existing logic for:
      • Handling an external Browser instance.
      • Launching a new browser if none is provided.
      • Preparing the browser with prepareBrowser if provided.
      • Managing resource cleanup with the cleanups array.
      • Creating and returning a new page for the scraper to use.
    • All existing behavior remains intact, ensuring backward compatibility.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (3)

src/scrapers/base-scraper-with-browser.ts:85

  • [nitpick] The word 'bang' might be confusing. It would be clearer to use 'exclamation mark (!)' instead.
NOTICE - it is discouraged to use bang (!) in general.

src/scrapers/base-isracard-amex.ts:33

  • The ExtendedScraperOptions interface was removed, but there was no mention of its removal in the context provided. Ensure that this interface is not used elsewhere in the codebase.
type CompanyServiceOptions = {

src/scrapers/base-isracard-amex.ts:258

  • [nitpick] The parameter name 'options' is of type 'CompanyServiceOptions'. It would be clearer to rename it to 'companyServiceOptions'.
async function getExtraScrapTransaction(page: Page, options:CompanyServiceOptions, month: Moment, accountIndex: number, transaction: Transaction): Promise<Transaction> {

const { browser } = this.options;

/**
* For backward compatibility, we will close the browser even if we didn't create it
Copy link
Preview

Copilot AI Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word 'didn't' should be 'did not' for consistency with formal writing.

Suggested change
* For backward compatibility, we will close the browser even if we didn't create it
* For backward compatibility, we will close the browser even if we did not create it

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants