Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Jan 30, 2022
1 parent 84ea20b commit 29febe8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
32 changes: 21 additions & 11 deletions src/background/providers/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,7 @@ export class CloudflareProvider extends Provider {
(this.issueInfo.requestId === details.requestId)
) {
if (this.matchesIssuingHeadersCriteria(details)) {
const issueInfo: IssueInfo = { ...this.issueInfo };

// Clear the issue info.
this.issueInfo = null;

setTimeout(
(): void => {
this.sendIssueRequest(issueInfo.url, issueInfo.formData);
},
0
);
this.triggerIssueRequest(details.requestId);

// cancel the request with captcha solution.
return { cancel: true };
Expand Down Expand Up @@ -306,6 +296,26 @@ export class CloudflareProvider extends Provider {
return false;
}

private triggerIssueRequest(requestId: string): void {
// Is the current (cancelled) request a trigger to initiate a secondary request to the provider for the issuing of signed tokens?
if (
(this.issueInfo !== null) &&
(this.issueInfo.requestId === requestId)
) {
const issueInfo: IssueInfo = { ...this.issueInfo };

// Clear the issue info.
this.issueInfo = null;

setTimeout(
(): void => {
this.sendIssueRequest(issueInfo.url, issueInfo.formData);
},
0
);
}
}

private async sendIssueRequest(
url: string,
formData: { [key: string]: string[] | string },
Expand Down
40 changes: 22 additions & 18 deletions src/background/providers/hcaptcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,40 +281,44 @@ export class HcaptchaProvider extends Provider {
handleOnCompleted(
details: chrome.webRequest.WebResponseHeadersDetails,
): void {
this.sendIssueRequest(details.requestId);
this.triggerIssueRequest(details.requestId);
}

handleOnErrorOccurred(
details: chrome.webRequest.WebResponseErrorDetails,
): void {
this.sendIssueRequest(details.requestId);
this.triggerIssueRequest(details.requestId);
}

private async sendIssueRequest(requestId: string): Promise<void> {
// Is the completed request a trigger to initiate a secondary request to the provider for the issuing of signed tokens?
private triggerIssueRequest(requestId: string): void {
// Is the current (completed) request a trigger to initiate a secondary request to the provider for the issuing of signed tokens?
if (
(this.issueInfo !== null) &&
(this.issueInfo.requestId === requestId)
) {
try {
const url: string = this.issueInfo!.url;
const url: string = this.issueInfo!.url;

// Clear the issue info.
this.issueInfo = null;
// Clear the issue info.
this.issueInfo = null;

// Issue tokens.
const tokens = await this.issue(url);
this.sendIssueRequest(url);
}
}

// Store tokens.
const cached = this.getStoredTokens();
this.setStoredTokens(cached.concat(tokens));
}
catch(error: any) {
console.error(error.message);
}
private async sendIssueRequest(url: string): Promise<void> {
try {
// Issue tokens.
const tokens = await this.issue(url);

this.callbacks.navigateUrl(HcaptchaProvider.EARNED_TOKEN_COOKIE.url);
// Store tokens.
const cached = this.getStoredTokens();
this.setStoredTokens(cached.concat(tokens));
}
catch(error: any) {
console.error(error.message);
}

this.callbacks.navigateUrl(HcaptchaProvider.EARNED_TOKEN_COOKIE.url);
}

private async issue(url: string): Promise<Token[]> {
Expand Down

0 comments on commit 29febe8

Please sign in to comment.