2012-08-12 03:22:51 +02:00
|
|
|
(function() {
|
|
|
|
function SpamBeeCaptcha(loadData) {
|
|
|
|
var that = this;
|
|
|
|
var inputCaptcha = document.getElementById("bee_captcha");
|
|
|
|
var divCaptcha = document.getElementById('serendipity_comment_beecaptcha');
|
2012-08-12 03:27:11 +02:00
|
|
|
var method = loadData.method == 'json' ? loadData.method : 'default';
|
2012-08-12 03:22:51 +02:00
|
|
|
var url = typeof loadData.url != 'undefined' ? loadData.url : null;
|
|
|
|
var answer = typeof loadData.answer != 'undefined' ? loadData.answer : null;
|
|
|
|
|
|
|
|
this.attachToLoadEvent = function() {
|
|
|
|
var handlerCalled = false;
|
|
|
|
var eventHandler = function() {
|
|
|
|
// Since we use multiple handlers, only run this function once
|
2012-08-12 03:27:11 +02:00
|
|
|
if (handlerCalled) return;
|
2012-08-12 03:22:51 +02:00
|
|
|
handlerCalled = true;
|
|
|
|
|
|
|
|
that.fillCaptcha();
|
|
|
|
|
|
|
|
// We don't need any additional load events anymore
|
|
|
|
if (document.addEventListener) {
|
|
|
|
document.removeEventListener('load', eventHandler, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.addEventListener) {
|
|
|
|
// Use DOMContentLoaded for modern browsers, load for older ones
|
|
|
|
document.addEventListener('DOMContentLoaded', eventHandler, true);
|
|
|
|
document.addEventListener('load', eventHandler, true);
|
|
|
|
} else if (window.attachEvent) {
|
|
|
|
// Internet Exploder
|
2012-08-12 19:59:34 +02:00
|
|
|
window.attachEvent('onload', eventHandler);
|
2012-08-12 03:22:51 +02:00
|
|
|
} else {
|
|
|
|
// Very, very old browsers
|
2012-08-12 03:27:11 +02:00
|
|
|
var oldOnload = typeof window.onload == 'function' ? window.onload : null;
|
|
|
|
window.onload = function() {
|
|
|
|
if (null !== oldOnload) oldOnload();
|
|
|
|
eventHandler();
|
2012-08-12 03:22:51 +02:00
|
|
|
}
|
2012-08-11 22:02:16 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-12 03:22:51 +02:00
|
|
|
|
|
|
|
this.fillCaptcha = function() {
|
|
|
|
if ('default' == method && null !== answer) {
|
|
|
|
inputCaptcha.value = answer;
|
2012-08-12 03:27:11 +02:00
|
|
|
this.hideBeeElement();
|
2012-08-12 03:22:51 +02:00
|
|
|
return;
|
|
|
|
} else if ('json' == method && null !== url) {
|
|
|
|
fetchJsonData();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-11 22:02:16 +02:00
|
|
|
}
|
2012-08-12 03:22:51 +02:00
|
|
|
|
|
|
|
this.hideBeeElement = function() {
|
2012-08-12 19:59:34 +02:00
|
|
|
var elementClass = divCaptcha.className;
|
2012-08-12 03:22:51 +02:00
|
|
|
if (null === elementClass.match(/\bspambeehidden\b/)) {
|
2012-08-12 19:59:34 +02:00
|
|
|
divCaptcha.className = elementClass + ' spambeehidden';
|
2012-08-12 03:22:51 +02:00
|
|
|
}
|
2012-08-11 22:02:16 +02:00
|
|
|
}
|
2012-08-12 03:22:51 +02:00
|
|
|
|
|
|
|
function fetchJsonData() {
|
|
|
|
if (window.XMLHttpRequest) { // Mozilla, Safari, Opera, IE7
|
|
|
|
var httpRequest = new XMLHttpRequest();
|
|
|
|
} else if (window.ActiveXObject) { // IE6, IE5
|
|
|
|
var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
|
|
|
|
}
|
|
|
|
httpRequest.onreadystatechange = function() {
|
|
|
|
fetchJsonDataReady(httpRequest);
|
2012-08-11 22:02:16 +02:00
|
|
|
}
|
2012-08-12 03:22:51 +02:00
|
|
|
httpRequest.open('POST', url, true);
|
|
|
|
httpRequest.setRequestHeader('content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
|
|
|
|
httpRequest.send();
|
2012-07-07 13:32:50 +02:00
|
|
|
}
|
|
|
|
|
2012-08-12 03:22:51 +02:00
|
|
|
function fetchJsonDataReady(httpRequest){
|
|
|
|
if (null !== httpRequest && 4 == httpRequest.readyState && 200 == httpRequest.status) {
|
|
|
|
|
|
|
|
var response = httpRequest.responseText;
|
|
|
|
var jsonResponse = (typeof JSON != 'undefined') ? JSON.parse(response) : eval('(' + response + ')');
|
|
|
|
var answer = jsonResponse.answer;
|
|
|
|
|
|
|
|
if (typeof answer != 'string' || 'ERROR' != answer.toUpperCase()) {
|
|
|
|
inputCaptcha.value = answer;
|
2012-08-12 16:05:43 +02:00
|
|
|
that.hideBeeElement();
|
2012-08-12 03:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-11 22:02:16 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-07 13:32:50 +02:00
|
|
|
|
2012-08-12 03:22:51 +02:00
|
|
|
var spamBeeObj = new SpamBeeCaptcha(spamBeeData);
|
|
|
|
spamBeeObj.attachToLoadEvent();
|
|
|
|
})();
|