This is a minimalist example showing how to implement Genuine Captcha in a completely offline-usable HTML file.
Loading CAPTCHA...
Follow these steps to implement Genuine Captcha in your own projects:
Genuine Captcha provides two main endpoints:
First, make a request to generate a new CAPTCHA challenge:
// Store the captcha secret
let captchaSecret = '';
// Function to load a new captcha
function loadCaptcha() {
document.getElementById('captcha-loading').style.display = 'block';
document.getElementById('captcha-image').style.display = 'none';
document.getElementById('captcha-input-container').style.display = 'none';
document.getElementById('captcha-result').style.display = 'none';
document.getElementById('refresh-captcha').style.display = 'none';
fetch('https://api.genuine-captcha.io/api/captcha/create')
.then(response => response.json())
.then(data => {
const imageType = 'image/png';
const base64Image = `data:${imageType};base64,${data.ImageAsBase64}`;
document.getElementById('captcha-image').src = base64Image;
// Store the secret for verification later
captchaSecret = data.SecretAsBase64;
// Show the captcha and input field
document.getElementById('captcha-image').style.display = 'block';
document.getElementById('captcha-loading').style.display = 'none';
document.getElementById('captcha-input-container').style.display = 'block';
document.getElementById('refresh-captcha').style.display = 'inline-block';
})
.catch(error => {
console.error("Error loading captcha:", error);
document.getElementById('captcha-loading').innerHTML =
"Error loading CAPTCHA. Please try again.";
});
}
After the user enters their solution, verify it with the API:
// Function to verify the captcha solution
function verifyCaptcha() {
const solution = document.getElementById('captcha-solution').value.trim();
if (!solution) {
alert('Please enter your answer to the CAPTCHA');
return;
}
const resultElement = document.getElementById('captcha-result');
resultElement.style.display = 'block';
fetch(`https://api.genuine-captcha.io/api/captcha/verify?captchaSolution=${solution}&captchaSecret=${encodeURIComponent(captchaSecret)}`, {
mode: 'cors'
})
.then(response => {
if (response.ok) {
resultElement.className = 'success';
resultElement.innerHTML = '<strong>Success!</strong> CAPTCHA verified correctly.';
document.getElementById('captcha-input-container').style.display = 'none';
} else {
resultElement.className = 'error';
resultElement.innerHTML = '<strong>Error:</strong> Incorrect solution. Please try again.';
}
})
.catch(error => {
console.error("Error verifying captcha:", error);
resultElement.className = 'error';
resultElement.innerHTML = '<strong>Error:</strong> Failed to verify. Please try again.';
});
}
Set up the CAPTCHA when the page loads:
// Initialize when the page loads
document.addEventListener('DOMContentLoaded', () => {
// Load the initial captcha
loadCaptcha();
// Add event listeners
document.getElementById('verify-captcha').addEventListener('click', verifyCaptcha);
document.getElementById('refresh-captcha').addEventListener('click', loadCaptcha);
document.getElementById('captcha-solution').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
verifyCaptcha();
}
});
});
<img>
tag.