mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-10 17:26:17 +00:00
New structure
This commit is contained in:
60
src/util/browser/BrowserFactory.ts
Normal file
60
src/util/browser/BrowserFactory.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Browser Factory Utility
|
||||
* Eliminates code duplication between Desktop and Mobile flows
|
||||
*
|
||||
* Centralized browser instance creation and cleanup logic
|
||||
*/
|
||||
|
||||
import type { BrowserContext } from 'rebrowser-playwright'
|
||||
import type { MicrosoftRewardsBot } from '../../index'
|
||||
import type { AccountProxy } from '../../interface/Account'
|
||||
|
||||
/**
|
||||
* Create a browser instance for the given account
|
||||
* IMPROVEMENT: Extracted from DesktopFlow and MobileFlow to eliminate duplication
|
||||
*
|
||||
* @param bot Bot instance
|
||||
* @param proxy Account proxy configuration
|
||||
* @param email Account email for session naming
|
||||
* @returns Browser context ready to use
|
||||
*
|
||||
* @example
|
||||
* const browser = await createBrowserInstance(bot, account.proxy, account.email)
|
||||
*/
|
||||
export async function createBrowserInstance(
|
||||
bot: MicrosoftRewardsBot,
|
||||
proxy: AccountProxy,
|
||||
email: string
|
||||
): Promise<BrowserContext> {
|
||||
const browserModule = await import('../../browser/Browser')
|
||||
const Browser = browserModule.default
|
||||
const browserInstance = new Browser(bot)
|
||||
return await browserInstance.createBrowser(proxy, email)
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely close browser context with error handling
|
||||
* IMPROVEMENT: Extracted from DesktopFlow and MobileFlow to eliminate duplication
|
||||
*
|
||||
* @param bot Bot instance
|
||||
* @param browser Browser context to close
|
||||
* @param email Account email for logging
|
||||
* @param isMobile Whether this is a mobile browser context
|
||||
*
|
||||
* @example
|
||||
* await closeBrowserSafely(bot, browser, account.email, false)
|
||||
*/
|
||||
export async function closeBrowserSafely(
|
||||
bot: MicrosoftRewardsBot,
|
||||
browser: BrowserContext,
|
||||
email: string,
|
||||
isMobile: boolean
|
||||
): Promise<void> {
|
||||
try {
|
||||
await bot.browser.func.closeBrowser(browser, email)
|
||||
} catch (closeError) {
|
||||
const message = closeError instanceof Error ? closeError.message : String(closeError)
|
||||
const platform = isMobile ? 'mobile' : 'desktop'
|
||||
bot.log(isMobile, `${platform.toUpperCase()}-FLOW`, `Failed to close ${platform} context: ${message}`, 'warn')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user