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(plugin-recaptcha) Allow custom data to be sent #701

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/puppeteer-extra-plugin-recaptcha/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ export class PuppeteerExtraPluginRecaptcha extends PuppeteerExtraPlugin {

async getRecaptchaSolutions(
captchas: types.CaptchaInfo[],
provider?: types.SolutionProvider
provider?: types.SolutionProvider,
extraData: { [key: string]: any } = {},
) {
this.debug('getRecaptchaSolutions', { captchaNum: captchas.length })
provider = provider || this.opts.provider
Expand All @@ -222,7 +223,8 @@ export class PuppeteerExtraPluginRecaptcha extends PuppeteerExtraPlugin {
this,
captchas,
provider.token,
provider.opts || {}
provider.opts || {},
extraData
)
response.error =
response.error ||
Expand Down Expand Up @@ -276,7 +278,8 @@ export class PuppeteerExtraPluginRecaptcha extends PuppeteerExtraPlugin {
}

async solveRecaptchas(
page: Page | Frame
page: Page | Frame,
extraData: { [key: string]: any } = {},
): Promise<types.SolveRecaptchasResult> {
this.debug('solveRecaptchas')
const response: types.SolveRecaptchasResult = {
Expand All @@ -301,7 +304,7 @@ export class PuppeteerExtraPluginRecaptcha extends PuppeteerExtraPlugin {
const {
solutions,
error: solutionsError
} = await this.getRecaptchaSolutions(response.captchas)
} = await this.getRecaptchaSolutions(response.captchas, undefined, extraData)
response.solutions = solutions

const {
Expand Down Expand Up @@ -331,7 +334,7 @@ export class PuppeteerExtraPluginRecaptcha extends PuppeteerExtraPlugin {
prop.enterRecaptchaSolutions = async (solutions: types.CaptchaSolution[]) =>
this.enterRecaptchaSolutions(prop, solutions)
// Add convenience methods that wraps all others
prop.solveRecaptchas = async () => this.solveRecaptchas(prop)
prop.solveRecaptchas = this.solveRecaptchas.bind(this, prop)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i dont know if this syntax works with async. bind() here makes sure that the first parameter is bound to the page object. Further calls to prop.solveRecaptchas should then call the solveRecaptchas with just 1 argument (the extraData) since the page has been bound thus disappeared

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tested this locally; works correctly (and properly assigns the function so that any additional arguments in the future will also work)

}

async onPageCreated(page: Page) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ async function decodeRecaptchaAsync(
export async function getSolutions(
captchas: types.CaptchaInfo[] = [],
token: string = '',
opts: TwoCaptchaProviderOpts = {}
opts: TwoCaptchaProviderOpts = {},
extraData: { [key: string]: any } = {},
): Promise<types.GetSolutionsResult> {
opts = { ...providerOptsDefaults, ...opts }
const solutions = await Promise.all(
captchas.map(c => getSolution(c, token, opts))
captchas.map(c => getSolution(c, token, opts, extraData))
)
return { solutions, error: solutions.find(s => !!s.error) }
}

async function getSolution(
captcha: types.CaptchaInfo,
token: string,
opts: TwoCaptchaProviderOpts
opts: TwoCaptchaProviderOpts,
extraData: { [key: string]: any } = {},
): Promise<types.CaptchaSolution> {
const solution: types.CaptchaSolution = {
_vendor: captcha._vendor,
Expand All @@ -79,7 +81,6 @@ async function getSolution(
solution.id = captcha.id
solution.requestAt = new Date()
debug('Requesting solution..', solution)
const extraData = {}
if (captcha.s) {
extraData['data-s'] = captcha.s // google site specific property
}
Expand Down