Skip to content

Commit 6ea59bb

Browse files
committed
Fixes #321 by adding honeypot spam prevention
Remove unnecessary steps in the readme by including honeypot spam prevention by default in the script. Works just fine if no honeypot field is present, since the falsey or empty behavior also means the form is submitted. This just makes it easier for users to take our example and get this feature for free rather than needing to go through steps and having commented out code in our sample script. Also fixes a bug since filtering out the honeypot meant that the honeypot data was no longer being used and was thus broken.
1 parent 7009d70 commit 6ea59bb

File tree

4 files changed

+19
-44
lines changed

4 files changed

+19
-44
lines changed

README.md

-29
Original file line numberDiff line numberDiff line change
@@ -340,35 +340,6 @@ e.g:
340340

341341
Let us know if you have any questions!
342342

343-
## SPAM prevention
344-
345-
In order to avoid getting spammed and fill up google apps usage quota, we will be implementing a simple SPAM prevention technique that's known as Honeypot where it essentially creates a hidden text field that if filled up is assumed as a spam bot and prevents the form from submit.
346-
347-
```html
348-
<form action="https://script.google.com/macros/s/..." method="post">
349-
<!--input id must be honeypot or else it wont work-->
350-
<label class="sr-only">Keep this field blank</label>
351-
<input id="honeypot" type="text" name="honeypot" value="" />
352-
<!--the rest of your form-->
353-
</form>
354-
```
355-
356-
```css
357-
#honeypot {
358-
display: none; /*makes the field not visible to humans*/
359-
}
360-
```
361-
362-
```javascript
363-
/* form-submission-handler.js */
364-
/* remove the comment from this if statement */
365-
366-
if (validateHuman(data.honeypot)) { //if form is filled, form will not be submitted
367-
return false;
368-
}
369-
370-
```
371-
372343

373344
## Uploading Files
374345

form-submission-handler.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44
return re.test(email);
55
}
66

7-
function validateHuman(honeypot) {
8-
if (honeypot) { //if hidden form filled up
9-
console.log("Robot Detected!");
10-
return true;
11-
} else {
12-
console.log("Welcome Human!");
13-
}
14-
}
15-
167
// get all data in form and return object
178
function getFormData(form) {
189
var elements = form.elements;
10+
var honeypot;
1911

2012
var fields = Object.keys(elements).filter(function(k) {
21-
return (elements[k].name !== "honeypot");
13+
if (elements[k].name === "honeypot") {
14+
honeypot = elements[k].value;
15+
return false;
16+
}
17+
return true;
2218
}).map(function(k) {
2319
if(elements[k].name !== undefined) {
2420
return elements[k].name;
@@ -56,19 +52,19 @@
5652
formData.formGoogleSendEmail = form.dataset.email || ""; // no email by default
5753

5854
console.log(formData);
59-
return formData;
55+
return {data: formData, honeypot};
6056
}
6157

6258
function handleFormSubmit(event) { // handles form submit without any jquery
6359
event.preventDefault(); // we are submitting via xhr below
6460
var form = event.target;
65-
var data = getFormData(form); // get the values submitted in the form
61+
var formData = getFormData(form);
62+
var data = formData.data;
6663

67-
/* OPTION: Remove this comment to enable SPAM prevention, see README.md
68-
if (validateHuman(data.honeypot)) { //if form is filled, form will not be submitted
64+
// If a honeypot field is filled, assume it was done so by a spam bot.
65+
if (formData.honeypot) {
6966
return false;
7067
}
71-
*/
7268

7369
if( data.email && !validEmail(data.email) ) { // if email is not valid show error
7470
var invalidEmail = form.querySelector(".email-invalid");

index.html

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ <h2 class="content-head is-center">Contact Us!</h2>
5252
<input id="color" name="color" placeholder="green" />
5353
</fieldset>
5454

55+
<fieldset class="pure-group honeypot-field">
56+
<label for="honeypot">To help avoid spam, utilize a Honeypot technique with a hidden text field; must be empty to submit the form! Otherwise, we assume the user is a spam bot.</label>
57+
<input id="honeypot" type="text" name="honeypot" value="" />
58+
</fieldset>
59+
5560
<button class="button-success pure-button button-xlarge">
5661
<i class="fa fa-paper-plane"></i>&nbsp;Send</button>
5762
</div>

style.css

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ button {
3535
#name, #email {
3636
width: 50%;
3737
}
38+
.honeypot-field {
39+
display: none;
40+
}

0 commit comments

Comments
 (0)