forked from plainblack/Lacuna-Web-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha.js
140 lines (132 loc) · 5.08 KB
/
captcha.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
133
134
135
136
137
138
139
140
YAHOO.namespace("lacuna");
if (typeof YAHOO.lacuna.Captcha == "undefined" || !YAHOO.lacuna.Captcha) {
(function(){
var Lang = YAHOO.lang,
Util = YAHOO.util,
Dom = Util.Dom,
Event = Util.Event,
Lacuna = YAHOO.lacuna,
Game = Lacuna.Game,
Lib = Lacuna.Library;
var Captcha = function() {
// this.createEvent("onRpc");
this.id = "captcha";
var container = document.createElement("div");
container.id = this.id;
Dom.addClass(container, Lib.Styles.HIDDEN);
container.innerHTML = this._getHtml();
document.body.insertBefore(container, document.body.firstChild);
this.Dialog = new YAHOO.widget.Dialog(this.id, {
constraintoviewport:true,
postmethod:"none",
hideaftersubmit:false,
buttons:[
{ text:"Solve", handler:{fn:function(){ this.submit(); } }, isDefault:true },
{ text:"Cancel", handler:{fn:function(){ this.hide(); } } }
],
fixedcenter:true,
visible:false,
draggable:true,
effect:Game.GetContainerEffect(),
underlay:false,
modal:true,
close:true,
width:"390px",
zIndex:9999
});
this.Dialog.renderEvent.subscribe(function(){
this.captchaImage = Dom.get("captchaImage");
this.captchaSolution = Dom.get("captchaSolution");
this.captchaMessage = Dom.get("captchaMessage");
Event.on('captchaRefresh', 'click', this.refreshCaptcha, this, true);
Dom.removeClass(this.id, Lib.Styles.HIDDEN);
}, this, true);
this.Dialog.hideEvent.subscribe(function(){
this._fail();
}, this, true);
this.Dialog.submitEvent.subscribe(function(){
this.solveCaptcha();
}, this, true);
this.Dialog.render();
Game.OverlayManager.register(this.Dialog);
};
Captcha.prototype = {
_getHtml : function() {
return [
'<div class="hd">Verify Your Humanity</div>',
'<div class="bd">',
' <form>',
' <ul id="captchaList">',
' <li>Solve this problem to continue:</li>',
' <li>',
' <img width="300" height="80" id="captchaImage" />',
' <button id="captchaRefresh" type="button"><img alt="Refresh" src="'+Lib.AssetUrl+'ui/s/refresh.png" width="20" height="22" /></button>',
' </li>',
' <li><label for="captchaSolution">Answer:</label><input type="text" id="captchaSolution" /></li>',
' </ul>',
' <div id="captchaMessage" class="alert"> </div>',
' </form>',
'</div>'
].join('');
},
show : function(retry, fail) {
this._retry = retry;
this._fail = fail;
this.refreshCaptcha();
},
solveCaptcha : function() {
Lacuna.Pulser.Show();
Game.Services.Captcha.solve({
session_id : Game.GetSession(),
captcha_guid : this._captcha_guid,
captcha_solution : this.captchaSolution.value
},{
success : function(o) {
Lacuna.Pulser.Hide();
this.Dialog.hide();
this._retry();
},
failure : function(o) {
this.setError(o.error.message);
return true;
},
scope : this
});
},
refreshCaptcha: function() {
Lacuna.Pulser.Show();
Game.Services.Captcha.fetch({session_id:Game.GetSession()},{
success : function(o) {
var t = this;
var image = new Image();
image.onload = function() {
t._captcha_guid = o.result.guid;
t.captchaImage.src = o.result.url;
t.captchaSolution.value = '';
Lacuna.Pulser.Hide();
t.Dialog.show();
};
image.src = o.result.url;
},
scope : this
});
},
cancel : function() {
this.Dialog.hide();
},
setError : function(msg) {
this.captchaSolution.value = "";
this.captchaSolution.focus();
this.captchaMessage.innerHTML = msg;
var a = new Util.Anim(this.captchaMessage, {opacity:{from:1,to:0}}, 4);
a.onComplete.subscribe(function(){
this.captchaMessage.innerHTML = " ";
}, this, true);
a.animate();
}
};
Lacuna.Captcha = new Captcha();
})();
YAHOO.register("captcha", YAHOO.lacuna.Captcha, {version: "1", build: "0"});
}
// vim: noet:ts=4:sw=4