diff options
author | Fire Demon <firedemon@creature.cafe> | 2020-08-26 13:56:20 -0500 |
---|---|---|
committer | Fire Demon <firedemon@creature.cafe> | 2020-08-30 05:45:20 -0500 |
commit | 1d3e5bdd24f25e1870f3d3bec1c81040a423f138 (patch) | |
tree | 4c586be72d8ebe144420bdd33e16a7e08e72a643 /public | |
parent | 0622450209274a9051c03458156a694f2274f61e (diff) |
[Spam Prevention] Add username confirmation and simple, frictionless anti-spam mechanism
Diffstat (limited to 'public')
-rw-r--r-- | public/registration.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/public/registration.js b/public/registration.js new file mode 100644 index 000000000..a82ec09c2 --- /dev/null +++ b/public/registration.js @@ -0,0 +1,54 @@ +function dragon(message) { + const msgBuffer = new TextEncoder('utf-8').encode(message); + return crypto.subtle.digest('SHA-512', msgBuffer).then(hashBuffer => { + const hashArray = Array.from(new Uint8Array(hashBuffer)); + const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join(''); + return hashHex; + }); +} + +function getForm() { + return document.getElementById('registration_new_user') || document.getElementById('new_user'); +} + +function getField(name) { + return document.getElementById(`registration_user_${name}`) || document.getElementById(`user_${name}`); +} + +function handleSubmit(e) { + e.preventDefault(); + + const form = getForm(); + const u1 = getField('account_attributes_username'); + const u2 = getField('username'); + const kobold = getField('kobold'); + + if (!!u1 && !!u2 && u1.value.toLowerCase() === u2.value.toLowerCase()) { + u2.value = u1.value; + } + + let values = []; + + for (let i = 0; i < form.elements.length; i++) { + const element = form.elements[i]; + const value = element.value; + + if (!!element && ['text', 'email', 'textarea'].includes(element.type) && !!value) { + values.push(value.trim().toLowerCase()); + } + } + + const value = values.join('\u{F0666}'); + dragon(value).then(digest => { + if (!!kobold) { kobold.value = digest.toUpperCase(); } + form.submit(); + }, _ => { form.submit(); }); +} + +function addSubmitHandler() { + const form = getForm(); + if (!!form) { form.addEventListener('submit', handleSubmit); } +} + +window.addEventListener("load", addSubmitHandler); + |