-
Notifications
You must be signed in to change notification settings - Fork 645
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: browser improvements #259
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
171d669
feat: pass context script for webdriver
amhsirak cd05ddf
chore: lint
amhsirak c49e70a
chrome and chromium user agent
amhsirak b173ce3
chore: remove commented code
amhsirak 0618401
feat: args
amhsirak 7f48464
feat: add page navigation timeout
RohitR311 bdf908e
feat: add domcontentloaded wait load state
RohitR311 f38230d
feat: revert to networkidle for wait load state
RohitR311 7ce7a15
feat: check for selector visibility in getState
RohitR311 ea2c36f
Merge pull request #257 from getmaxun/item0-fix
amhsirak e22c019
feat: rotate user agents
amhsirak 320f24e
feat: shm & sandbox args
amhsirak ffe87b0
feat: user getUserAgent()
amhsirak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,7 +104,7 @@ export class RemoteBrowser { | |
} catch { | ||
return url; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Determines if a URL change is significant enough to emit | ||
|
@@ -130,11 +130,11 @@ export class RemoteBrowser { | |
}); | ||
|
||
// Handle page load events with retry mechanism | ||
page.on('load', async () => { | ||
page.on('load', async () => { | ||
const injectScript = async (): Promise<boolean> => { | ||
try { | ||
await page.waitForLoadState('networkidle', { timeout: 5000 }); | ||
|
||
await page.evaluate(getInjectableScript()); | ||
return true; | ||
} catch (error: any) { | ||
|
@@ -148,44 +148,35 @@ export class RemoteBrowser { | |
}); | ||
} | ||
|
||
private getUserAgent() { | ||
const userAgents = [ | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.140 Safari/537.36', | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:117.0) Gecko/20100101 Firefox/117.0', | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.1938.81 Safari/537.36 Edg/116.0.1938.81', | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.96 Safari/537.36 OPR/101.0.4843.25', | ||
'Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.62 Safari/537.36', | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:118.0) Gecko/20100101 Firefox/118.0', | ||
]; | ||
|
||
return userAgents[Math.floor(Math.random() * userAgents.length)]; | ||
} | ||
|
||
/** | ||
* An asynchronous constructor for asynchronously initialized properties. | ||
* Must be called right after creating an instance of RemoteBrowser class. | ||
* @param options remote browser options to be used when launching the browser | ||
* @returns {Promise<void>} | ||
*/ | ||
public initialize = async (userId: string): Promise<void> => { | ||
// const launchOptions = { | ||
// headless: true, | ||
// proxy: options.launchOptions?.proxy, | ||
// chromiumSandbox: false, | ||
// args: [ | ||
// '--no-sandbox', | ||
// '--disable-setuid-sandbox', | ||
// '--headless=new', | ||
// '--disable-gpu', | ||
// '--disable-dev-shm-usage', | ||
// '--disable-software-rasterizer', | ||
// '--in-process-gpu', | ||
// '--disable-infobars', | ||
// '--single-process', | ||
// '--no-zygote', | ||
// '--disable-notifications', | ||
// '--disable-extensions', | ||
// '--disable-background-timer-throttling', | ||
// ...(options.launchOptions?.args || []) | ||
// ], | ||
// env: { | ||
// ...process.env, | ||
// CHROMIUM_FLAGS: '--disable-gpu --no-sandbox --headless=new' | ||
// } | ||
// }; | ||
// console.log('Launch options before:', options.launchOptions); | ||
// this.browser = <Browser>(await options.browser.launch(launchOptions)); | ||
|
||
// console.log('Launch options after:', options.launchOptions) | ||
this.browser = <Browser>(await chromium.launch({ | ||
headless: true, | ||
args: [ | ||
"--disable-blink-features=AutomationControlled", | ||
"--disable-web-security", | ||
"--disable-features=IsolateOrigins,site-per-process", | ||
"--disable-site-isolation-trials", | ||
"--disable-extensions" | ||
], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review necessity of disabling security features in Chromium launch arguments. Disabling features like |
||
})); | ||
const proxyConfig = await getDecryptedProxyConfig(userId); | ||
let proxyOptions: { server: string, username?: string, password?: string } = { server: '' }; | ||
|
@@ -201,7 +192,7 @@ export class RemoteBrowser { | |
const contextOptions: any = { | ||
viewport: { height: 400, width: 900 }, | ||
// recordVideo: { dir: 'videos/' } | ||
// Force reduced motion to prevent animation issues | ||
// Force reduced motion to prevent animation issues | ||
reducedMotion: 'reduce', | ||
// Force JavaScript to be enabled | ||
javaScriptEnabled: true, | ||
|
@@ -220,18 +211,38 @@ export class RemoteBrowser { | |
password: proxyOptions.password ? proxyOptions.password : undefined, | ||
}; | ||
} | ||
const browserUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.38 Safari/537.36"; | ||
|
||
|
||
contextOptions.userAgent = browserUserAgent; | ||
contextOptions.userAgent = this.getUserAgent(); | ||
this.context = await this.browser.newContext(contextOptions); | ||
await this.context.addInitScript( | ||
`const defaultGetter = Object.getOwnPropertyDescriptor( | ||
Navigator.prototype, | ||
"webdriver" | ||
).get; | ||
defaultGetter.apply(navigator); | ||
defaultGetter.toString(); | ||
Object.defineProperty(Navigator.prototype, "webdriver", { | ||
set: undefined, | ||
enumerable: true, | ||
configurable: true, | ||
get: new Proxy(defaultGetter, { | ||
apply: (target, thisArg, args) => { | ||
Reflect.apply(target, thisArg, args); | ||
return false; | ||
}, | ||
}), | ||
}); | ||
const patchedGetter = Object.getOwnPropertyDescriptor( | ||
Navigator.prototype, | ||
"webdriver" | ||
).get; | ||
patchedGetter.apply(navigator); | ||
patchedGetter.toString();` | ||
); | ||
this.currentPage = await this.context.newPage(); | ||
|
||
await this.setupPageEventListeners(this.currentPage); | ||
|
||
// await this.currentPage.setExtraHTTPHeaders({ | ||
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' | ||
// }); | ||
const blocker = await PlaywrightBlocker.fromLists(fetch, ['https://easylist.to/easylist/easylist.txt']); | ||
await blocker.enableBlockingInPage(this.currentPage); | ||
this.client = await this.currentPage.context().newCDPSession(this.currentPage); | ||
|
@@ -456,7 +467,7 @@ export class RemoteBrowser { | |
this.currentPage = newPage; | ||
if (this.currentPage) { | ||
await this.setupPageEventListeners(this.currentPage); | ||
|
||
this.client = await this.currentPage.context().newCDPSession(this.currentPage); | ||
await this.subscribeToScreencast(); | ||
} else { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Verify the accuracy of user agent strings in
getUserAgent
.The user agent string
'Mozilla/5.0 (Windows NT 11.0; Win64; x64)...'
may not be accurate since Windows typically reports as 'Windows NT 10.0'. Ensure user agent strings are accurate to avoid detection.Apply this diff to correct the user agent string:
📝 Committable suggestion