-
Notifications
You must be signed in to change notification settings - Fork 165
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
base: master
Are you sure you want to change the base?
Conversation
browserContext
browserContext
There was a problem hiding this 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 no suggestions.
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); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
…arity and maintainability
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; | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 abrowserContext
is provided inthis.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.
- We create a new page using
- In the
- New Logic Added:
Logic Unchanged:
-
Existing Browser Initialization Logic Remains the Same:
- If no
browserContext
is provided:- We proceed to check if a
browser
is provided inthis.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 totrue
, we add a cleanup function to close the browser after scraping.
- We log a debug message:
- If no
browser
is provided:- We launch a new browser instance as before, using options like
executablePath
,args
,showBrowser
, andtimeout
fromthis.options
. - We add a cleanup function to close the browser after scraping.
- If
prepareBrowser
is provided, we call it with the new browser instance.
- We launch a new browser instance as before, using options like
- If a
- We proceed to check if a
- If no
-
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.
- We maintain a
-
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
ininitializePage()
, allowing more flexibility in browser management.
- Added support for an external
-
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.
- Handling an external
- All existing behavior remains intact, ensuring backward compatibility.
- The existing logic for:
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
* 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.
This pull request changes the page creation logic by alowing a
browserContext
to be passes instead of abrowser
(that is currently being closed when the scraping ends)