Furry / 2captcha

A wrapper around the 2captcha api
71 stars 23 forks source link

Feature: callback for progress bar #30

Open NabiKAZ opened 2 years ago

NabiKAZ commented 2 years ago

It would be nice to send a callback to run it while waiting for the captcha to return. To notice the progress of the operation.

Furry commented 2 years ago

Could you give an example of what you mean by this? In pseudocode or something similar, having trouble picturing it.

NabiKAZ commented 2 years ago

In normal mode, see the output, the user is waiting and sees no anything in output, so it does not know if the operation is in progress or if the application is frozen! If it was done quickly, maybe it would not be a problem. But this operation may take up to 1 minute to get the reCaptcha.

https://user-images.githubusercontent.com/246721/165078464-7cbbee6a-64b8-4e9f-914c-8ab508488480.mp4

But I changed the code like this: A function can now be sent as a callback so that something can be displayed in the output while receiving captcha results.

(async function() {
const Captcha = require("2captcha");

// Callback for show progress bar
const progressBarCallback = function() {
    process.stdout.write(".");
};

// A new 'solver' instance with our API key
const solver = new Captcha.Solver("...", 1000, true, progressBarCallback);

// Example ReCaptcha Website
console.log('Getting captcha...');
await solver.recaptcha("6Ld2sf4SAAAAAKSgzs0Q13IZhY02Pyo31S2jgOB5", "https://patrickhlauke.github.io/recaptcha/")
.then((res) => {
    console.log('res:', res);
})
.catch((err) => {
    console.error('err:', err);
});

console.log('Finish.');

})();

See result:

https://user-images.githubusercontent.com/246721/165078537-641cb2fb-364f-4425-a0b8-8eddf51c906f.mp4

I will try to send it in the form of PR.

In a more advanced way, these projects can be used to show the progress bar: https://github.com/npkgz/cli-progress https://github.com/visionmedia/node-progress

Or define it as an event instead of a callback function. For example:

solver.on('progress', function() {
    process.stdout.write('.');
});