Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

first working PoC

Blue Mouse3f4a89dccfa0
diff --git a/captcha.js b/captcha.js
index 8249860f4..edbbed791 100644
--- a/captcha.js
+++ b/captcha.js
@@ -1,7 +1,11 @@
const API_URL = 'http://localhost:5005/v1/captcha'
+let TOKEN = ''
+let HASH = ''
const ENDPOINTS = {
- AUTO_VERIFIABLE: '/auto-verifiable',
+ VERIFY: '/verify',
+ GENERATE: '/generate',
+ VERIFY_MANUAL: '/verify-manual',
}
let activeAction = 'checkbox'
@@ -42,14 +46,118 @@ const activateAction = (action) => {
actions[action].classList.remove('hidden')
}
+const enableManualChallenge = (svg) => {
+ const manualChallenge = document.querySelector('#manual-challenge')
+ const svgCaptcha = document.querySelector('#svg-captcha')
+
+ svgCaptcha.innerHTML = svg
+ manualChallenge.classList.remove('hidden')
+}
+
+const disableManualChallenge = () => {
+ const manualChallenge = document.querySelector('#manual-challenge')
+ const svgCaptcha = document.querySelector('#svg-captcha')
+
+ svgCaptcha.innerHTML = ''
+ manualChallenge.classList.add('hidden')
+}
+
+const generateCaptcha = async () => {
+ try {
+ const response = await fetch(`${API_URL}${ENDPOINTS.GENERATE}`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+
+ if (!response.ok) {
+ throw ''
+ }
+
+ const data = await response.json()
+ return data
+ } catch (e) {
+ return null
+ }
+}
+
+const verify = async () => {
+ try {
+ const response = await fetch(`${API_URL}${ENDPOINTS.VERIFY}`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+
+ if (!response.ok) {
+ throw ''
+ }
+
+ const data = await response.json()
+ return data
+ } catch (e) {
+ return null
+ }
+}
+
document.addEventListener('DOMContentLoaded', () => {
const captchaComponent = document.querySelector('#swetrix-captcha')
const branding = document.querySelector('#branding')
+ const svgCaptchaInput = document.querySelector('#svg-captcha-input')
+ const manualSubmitBtn = document.querySelector('#manual-submit-btn')
branding.addEventListener('click', (e) => {
e.stopPropagation()
})
+ manualSubmitBtn.addEventListener('click', async (e) => {
+ e.stopPropagation()
+
+ const code = svgCaptchaInput.value
+
+ if (!code) {
+ return
+ }
+
+ let response
+
+ try {
+ response = await fetch(`${API_URL}${ENDPOINTS.VERIFY_MANUAL}`, {
+ method: 'POST',
+ body: JSON.stringify({
+ hash: HASH,
+ code,
+ }),
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+ } catch (e) {
+ disableManualChallenge()
+ activateAction('failure')
+ svgCaptchaInput.value = ''
+ return
+ }
+
+ if (!response.ok) {
+ disableManualChallenge()
+ activateAction('failure')
+ svgCaptchaInput.value = ''
+ return
+ }
+
+ const data = await response.json()
+ console.log(data)
+
+ // TODO
+
+ svgCaptchaInput.value = ''
+ activateAction('completed')
+ disableManualChallenge()
+ })
+
captchaComponent.addEventListener('click', async () => {
if (activeAction === 'loading' || activeAction === 'completed') {
return
@@ -61,18 +169,23 @@ document.addEventListener('DOMContentLoaded', () => {
}
activateAction('loading')
- let response
try {
- response = await fetch(`${API_URL}${ENDPOINTS.AUTO_VERIFIABLE}`, {
- method: 'GET',
- })
+ const { token } = await verify()
+
+ if (!token) {
+ throw ''
+ }
+
+ TOKEN = token
+ activateAction('completed')
+ return
} catch (e) {
- activateAction('failure')
+ const { data, hash } = await generateCaptcha()
+
+ HASH = hash
+ enableManualChallenge(data)
return
}
-
- console.log(response)
- activateAction('completed')
})
})
diff --git a/index.html b/index.html
index de00be0c9..2269390a2 100644
--- a/index.html
+++ b/index.html
@@ -134,6 +134,46 @@
border-top-color: #fff;
animation: spin 1s infinite linear;
}
+
+ #manual-challenge {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: row;
+ border: 1px solid #e9e9e9;
+ border-top: none;
+ height: 130px;
+ width: 280px;
+ padding-left: 10px;
+ padding-right: 10px;
+ gap: 10px;
+ }
+
+ #input-n-captcha {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ cursor: pointer;
+ flex: 5;
+ }
+
+ #input-n-captcha > input {
+ width: 100%;
+ }
+
+ #manual-submit-btn {
+ background-color: #fff;
+ border: 1px solid #e9e9e9;
+ padding-left: 5px;
+ padding-right: 5px;
+ border-radius: 3px;
+ height: 65px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ cursor: pointer;
+ flex: 1;
+ }
</style>
<script src="captcha.js" defer></script>
</head>
@@ -172,6 +212,15 @@
</div>
</div>
</div>
+ <div id="manual-challenge" class="hidden">
+ <div id="input-n-captcha">
+ <div id="svg-captcha"></div>
+ <input type="text" id="svg-captcha-input" autocomplete="off" />
+ </div>
+ <div id="manual-submit-btn">
+ Submit
+ </div>
+ </div>
</body>
</html>
\ No newline at end of file