auto-captcha-solver
Version:
Automatically detect and solve various captcha types in Playwright & Puppeteer with 2Captcha/CapMonster Cloud integration
50 lines • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCaptchaSource = getCaptchaSource;
async function getCaptchaSource(page, selector) {
// Wait for element existence and visibility
const element = await page.waitForSelector(selector, {
state: 'visible',
timeout: 15000
});
// Get element type
const tagName = (await element.getProperty('tagName')).toString().toLowerCase();
// Handle different element types
switch (tagName) {
case 'img':
return handleImageElement(element);
case 'canvas':
return handleCanvasElement(element);
default:
return handleBackgroundImage(element);
}
}
// Image element handler
async function handleImageElement(element) {
const src = await element.getAttribute('src');
// Verify image load status
await element.waitForElementState('stable');
if (!src)
throw new Error('CAPTCHA image has no src attribute');
return resolveAbsoluteUrl(element, src);
}
// Canvas element handler
async function handleCanvasElement(element) {
// Capture canvas as data URL
const buffer = await element.screenshot();
return `data:image/png;base64,${buffer.toString('base64')}`;
}
// Background image handler
async function handleBackgroundImage(element) {
const style = (await element.getAttribute('style')) || '';
const bgMatch = style.match(/url\(["']?(.*?)["']?\)/i);
if (!bgMatch)
throw new Error('No background image found');
return resolveAbsoluteUrl(element, bgMatch[1]);
}
// URL resolution helper
async function resolveAbsoluteUrl(element, relativeUrl) {
const baseUrl = await element.evaluate((el) => new URL(el.baseURI).origin);
return new URL(relativeUrl, baseUrl).href;
}
//# sourceMappingURL=find-captcha-image-url.js.map