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!

theming support & other improvements

Blue Mouse136780a46d28
diff --git a/captcha-loader.js b/captcha-loader.js
index 813bbe2b9..ccd17ce10 100644
--- a/captcha-loader.js
+++ b/captcha-loader.js
@@ -1,26 +1,62 @@
(() => {
const CAPTCHA_SELECTOR = '.swecaptcha'
- const CAPTCHA_IFRAME_URL = './index.html'
+ const LIGHT_CAPTCHA_IFRAME_URL = './index.html'
+ const DARK_CAPTCHA_IFRAME_URL = './dark.html'
- const renderCaptcha = (container) => {
+ const THEMES = ['light', 'dark']
+
+ const log = (status, text) => {
+ console[status](`[Swetrix Captcha] ${text}`)
+ }
+
+ const renderCaptcha = (container, params) => {
+ const { theme } = params
const captchaFrame = document.createElement('iframe')
- captchaFrame.src = CAPTCHA_IFRAME_URL
+ captchaFrame.src = theme === 'dark' ? DARK_CAPTCHA_IFRAME_URL : LIGHT_CAPTCHA_IFRAME_URL
captchaFrame.title = 'Swetrix Captcha'
+ captchaFrame.style.border = 'none'
+ captchaFrame.style.width = '302px'
+ captchaFrame.style.height = 'auto'
+ captchaFrame.style.overflow = 'visible'
container.appendChild(captchaFrame)
}
+ const validateParams = (params) => {
+ const { theme } = params
+
+ if (theme && !THEMES.includes(theme)) {
+ log('error', `Invalid data-theme parameter: ${theme}`)
+ return false
+ }
+
+ // TODO: Validate pid via regex
+
+ return true
+ }
+
+ const parseParams = (container) => ({
+ pid: container.getAttribute('data-project-id'),
+ theme: container.getAttribute('data-theme'),
+ })
+
const main = () => {
+ // TODO: Add some callbacks here
+ window.swecaptcha = true
+
const containers = document.querySelectorAll(CAPTCHA_SELECTOR)
for (const container of containers) {
- renderCaptcha(container)
- }
- }
+ const params = parseParams(container)
- const log = (status, text) => {
- console[status](`[Swetrix Captcha] ${text}`)
+ if (!validateParams(params)) {
+ log('error', 'Aborting captcha rendering due to invalid parameters.')
+ return
+ }
+
+ renderCaptcha(container, params)
+ }
}
if ('swecaptcha' in window) {
@@ -28,4 +64,4 @@
}
document.addEventListener('DOMContentLoaded', main)
-})
+})()
diff --git a/captcha.js b/captcha.js
index e2159add9..e031d9b7c 100644
--- a/captcha.js
+++ b/captcha.js
@@ -1,4 +1,5 @@
const API_URL = 'http://localhost:5005/v1/captcha'
+const DEFAULT_THEME = 'light'
let TOKEN = ''
let HASH = ''
@@ -50,7 +51,15 @@ const enableManualChallenge = (svg) => {
const manualChallenge = document.querySelector('#manual-challenge')
const svgCaptcha = document.querySelector('#svg-captcha')
- svgCaptcha.innerHTML = svg
+ if (!svg) {
+ const error = document.createElement('p')
+ error.innerText = 'Error loading captcha'
+ error.style.color = '#d6292a'
+ svgCaptcha.appendChild(error)
+ } else {
+ svgCaptcha.innerHTML = svg
+ }
+
manualChallenge.classList.remove('hidden')
}
@@ -69,6 +78,9 @@ const generateCaptcha = async () => {
headers: {
'Content-Type': 'application/json',
},
+ body: JSON.stringify({
+ theme: window.__SWETRIX_CAPTCHA_THEME || DEFAULT_THEME,
+ }),
})
if (!response.ok) {
@@ -79,7 +91,7 @@ const generateCaptcha = async () => {
return data
} catch (e) {
activateAction('failure')
- return null
+ return {}
}
}
@@ -93,14 +105,14 @@ const verify = async () => {
})
if (!response.ok) {
- throw ''
+ return {}
}
const data = await response.json()
return data
} catch (e) {
activateAction('failure')
- return null
+ return {}
}
}
diff --git a/index.html b/index.html
index b8d9229b9..7facaf701 100644
--- a/index.html
+++ b/index.html
@@ -13,6 +13,13 @@
-moz-osx-font-smoothing: grayscale;
}
+ body {
+ margin: 0;
+ padding: 0;
+ height: auto;
+ overflow: auto;
+ }
+
#swetrix-captcha {
display: flex;
justify-content: center;
@@ -61,7 +68,6 @@
#status {
font-size: 16px;
color: #0f0f0f;
- /* font-weight: 400; */
}
.hidden {
@@ -92,6 +98,7 @@
#legal>.separator::before {
content: "-";
margin: 0 2px;
+ color: #0f0f0f;
}
#action svg {
@@ -175,6 +182,9 @@
flex: 1;
}
</style>
+ <script>
+ window.__SWETRIX_CAPTCHA_THEME = 'light'
+ </script>
<script src="captcha.js" defer></script>
</head>