-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha-sucks.js
132 lines (104 loc) · 2.96 KB
/
captcha-sucks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Captcha-Sucks
*
* @module CAPTCHA_SUCKS
*/
var CAPTCHA_SUCKS = CAPTCHA_SUCKS || (function(){
// private properties
var quizes = [
{ question: 'Which should be burned?',
answers: ['dog', 'wood', 'knife', 'pretzel'],
correct: 'wood'},
{ question: 'What is a taco?',
answers: ['four', 'pasta', 'universe', 'food'],
correct: 'food'},
{ question: 'People live in which?',
answers: ['holes', 'boats', 'houses', 'coconut', 'lemon', 'catapult'],
correct: 'houses'}],
response = 'Try again, Sally.';
// private methods
function pickQuiz(){
if(quizes.length === 0){
throw {
name: 'Error',
message: 'no quizes are available'
};
};
var quiz = quizes[Math.floor(Math.random() * quizes.length)];
quiz.answers = mixElements(quiz.answers);
return quiz;
};
function mixElements(array){
return Array.prototype.sort.call(array, function (a, b){
return (Math.random() < .5) ? -1 : 1;
});
};
function showQuiz(holder){
// clear old quiz
holder.innerHTML = '';
// add new quiz
var q = pickQuiz();
// build quiz elements
var answerGroup = 'CAPTCHA-SUCKS_' + Math.random();
// question
var question = document.createElement('label');
question.innerHTML = q.question;
holder.appendChild(question);
// answers
for(var i = 0; i < q.answers.length; i += 1){
holder.appendChild(createAnswerNode(q.answers[i], answerGroup));
};
};
function createAnswerNode(answer, group){
var answerNode = document.createElement('div'),
input = document.createElement('input'),
label = document.createElement('label'),
id = answer + Math.random();
// configure input
input.setAttribute('type', 'radio');
input.setAttribute('name', group);
input.setAttribute('value', answer);
input.setAttribute('id', id);
input.style.display = 'inline';
// configure label
label.innerHTML = answer;
label.setAttribute('for', id);
label.style.display = 'inline';
// append input and label to answerNode
answerNode.appendChild(input);
answerNode.appendChild(label);
return answerNode;
};
return {
// getters/setters
getQuizes: function(){
return quizes;
},
setQuizes: function(new_quizes){
// TODO: verify new_quizes have all reqired data
// TODO: verify they have a matching correct answer
quizes = new_quizes;
},
// big fat API
form: function(form_selector){
// identify form
var form = document.getElementById(form_selector);
// add holder for our elements
var holder = document.createElement('div');
holder.setAttribute('class', 'captcha-sucks');
// TODO: this may not work on older browsers
form.insertBefore(holder, form.lastElementChild);
showQuiz(holder);
// TODO: watch for submission and check answer
}
};
})();
var form;
$(function(){
CAPTCHA_SUCKS.form('my_form');
form = document.getElementById('my_form');
$('form').submit(function(){
console.log($(this).serialize());
return false;
});
});