Skip to content

Commit 29febe8

Browse files
committed
minor refactoring
1 parent 84ea20b commit 29febe8

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

src/background/providers/cloudflare.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,7 @@ export class CloudflareProvider extends Provider {
208208
(this.issueInfo.requestId === details.requestId)
209209
) {
210210
if (this.matchesIssuingHeadersCriteria(details)) {
211-
const issueInfo: IssueInfo = { ...this.issueInfo };
212-
213-
// Clear the issue info.
214-
this.issueInfo = null;
215-
216-
setTimeout(
217-
(): void => {
218-
this.sendIssueRequest(issueInfo.url, issueInfo.formData);
219-
},
220-
0
221-
);
211+
this.triggerIssueRequest(details.requestId);
222212

223213
// cancel the request with captcha solution.
224214
return { cancel: true };
@@ -306,6 +296,26 @@ export class CloudflareProvider extends Provider {
306296
return false;
307297
}
308298

299+
private triggerIssueRequest(requestId: string): void {
300+
// Is the current (cancelled) request a trigger to initiate a secondary request to the provider for the issuing of signed tokens?
301+
if (
302+
(this.issueInfo !== null) &&
303+
(this.issueInfo.requestId === requestId)
304+
) {
305+
const issueInfo: IssueInfo = { ...this.issueInfo };
306+
307+
// Clear the issue info.
308+
this.issueInfo = null;
309+
310+
setTimeout(
311+
(): void => {
312+
this.sendIssueRequest(issueInfo.url, issueInfo.formData);
313+
},
314+
0
315+
);
316+
}
317+
}
318+
309319
private async sendIssueRequest(
310320
url: string,
311321
formData: { [key: string]: string[] | string },

src/background/providers/hcaptcha.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,40 +281,44 @@ export class HcaptchaProvider extends Provider {
281281
handleOnCompleted(
282282
details: chrome.webRequest.WebResponseHeadersDetails,
283283
): void {
284-
this.sendIssueRequest(details.requestId);
284+
this.triggerIssueRequest(details.requestId);
285285
}
286286

287287
handleOnErrorOccurred(
288288
details: chrome.webRequest.WebResponseErrorDetails,
289289
): void {
290-
this.sendIssueRequest(details.requestId);
290+
this.triggerIssueRequest(details.requestId);
291291
}
292292

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

302-
// Clear the issue info.
303-
this.issueInfo = null;
301+
// Clear the issue info.
302+
this.issueInfo = null;
304303

305-
// Issue tokens.
306-
const tokens = await this.issue(url);
304+
this.sendIssueRequest(url);
305+
}
306+
}
307307

308-
// Store tokens.
309-
const cached = this.getStoredTokens();
310-
this.setStoredTokens(cached.concat(tokens));
311-
}
312-
catch(error: any) {
313-
console.error(error.message);
314-
}
308+
private async sendIssueRequest(url: string): Promise<void> {
309+
try {
310+
// Issue tokens.
311+
const tokens = await this.issue(url);
315312

316-
this.callbacks.navigateUrl(HcaptchaProvider.EARNED_TOKEN_COOKIE.url);
313+
// Store tokens.
314+
const cached = this.getStoredTokens();
315+
this.setStoredTokens(cached.concat(tokens));
316+
}
317+
catch(error: any) {
318+
console.error(error.message);
317319
}
320+
321+
this.callbacks.navigateUrl(HcaptchaProvider.EARNED_TOKEN_COOKIE.url);
318322
}
319323

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

0 commit comments

Comments
 (0)