Upgraded to Recaptcha v3

This commit is contained in:
斟酌 鵬兄 2024-11-15 08:23:32 +08:00
parent 217c7efb88
commit b014148c31
3 changed files with 51 additions and 148 deletions

View File

@ -30,6 +30,7 @@
/** @type {_3rdParty_.Recaptcha} */ /** @type {_3rdParty_.Recaptcha} */
var Recaptcha; var Recaptcha;
var RECAPTCHA_SITE_KEY = null;
/** @type {_AstConf_.UserInfo} */ /** @type {_AstConf_.UserInfo} */
var currentUser = Conf.get( "UserInfo" ); var currentUser = Conf.get( "UserInfo" );
@ -84,7 +85,6 @@
// If nameField exists, we assume logged in // If nameField exists, we assume logged in
, loggedIn = !nameField , loggedIn = !nameField
, contentHadHaveText = false , contentHadHaveText = false
, commentInput = function () , commentInput = function ()
{ {
@ -137,7 +137,6 @@
if( rf && rf.hasChildNodes() ) if( rf && rf.hasChildNodes() )
{ {
// if capcha opened // if capcha opened
closeCaptcha();
hideSubmit(); hideSubmit();
Cycle.delay( function() { Cycle.delay( function() {
closeInputPanel( reviveInputpanel.bind( c_reply.parentNode.parentNode ) ) closeInputPanel( reviveInputpanel.bind( c_reply.parentNode.parentNode ) )
@ -188,7 +187,6 @@
// Create capcha // Create capcha
if(!fieldReady && contentHaveText && nameHaveText) if(!fieldReady && contentHaveText && nameHaveText)
{ {
createCaptcha();
// show submit button // show submit button
showSubmit(); showSubmit();
fieldReady = true; fieldReady = true;
@ -243,12 +241,13 @@
if( getFieldsValidated( p ) ) if( getFieldsValidated( p ) )
{ {
popBubble(); popBubble();
captcha_wrapper.readOnly = true; Recaptcha.execute( RECAPTCHA_SITE_KEY, { "action": "submit" } )
.then(function( token )
closeCaptcha(); {
// Post the data, bind obj to Handler p[ "token" ] = token;
postData( Config.processor, p, commentSuccess.bind(p), commentFailed ); // Post the data, bind obj to Handler
postData( Config.processor, p, commentSuccess.bind(p), commentFailed );
});
} }
else else
{ {
@ -340,7 +339,6 @@
if(obj.reload) if(obj.reload)
{ {
// reload the recaptcha // reload the recaptcha
createCaptcha();
Cycle.delay(showSubmit, 1500); Cycle.delay(showSubmit, 1500);
// Wrong answer. // Wrong answer.
@ -368,7 +366,6 @@
else else
{ {
// reload the recaptcha // reload the recaptcha
createCaptcha();
Cycle.delay(showSubmit, 1500); Cycle.delay(showSubmit, 1500);
Cycle.delay(function (){cBubble.blurp();}, 500); Cycle.delay(function (){cBubble.blurp();}, 500);
@ -390,7 +387,7 @@
, captchaKey = new EventKey("KeyDown", function(e) { if(e.keyCode == 13) submitComment(); }) , captchaKey = new EventKey("KeyDown", function(e) { if(e.keyCode == 13) submitComment(); })
, _showSubmit = function () , showSubmit = function ()
{ {
// prevent multiple prop assign // prevent multiple prop assign
if( !submit_button.hasListener( submitKey ) ) if( !submit_button.hasListener( submitKey ) )
@ -402,7 +399,7 @@
submit_button.addEventListener( submitKey ); submit_button.addEventListener( submitKey );
} }
, _hideSubmit = function () , hideSubmit = function ()
{ {
// prevent multiple prop assign // prevent multiple prop assign
if( submit_button.hasListener( submitKey ) ) if( submit_button.hasListener( submitKey ) )
@ -414,21 +411,6 @@
submit_button.removeEventListener( submitKey ); submit_button.removeEventListener( submitKey );
} }
, showSubmit = loggedIn
? /* Overload */ _showSubmit
: /* Overload */ function ()
{
_showSubmit();
captcha_wrapper.addEventListener(captchaKey);
}
, hideSubmit = loggedIn
? /* Overload */ _hideSubmit
: /* Overload */ function ()
{
_hideSubmit();
captcha_wrapper.removeEventListener(captchaKey);
}
, __createContentText = function( text ) , __createContentText = function( text )
{ {
var e = wrapc( "c_text", text ); var e = wrapc( "c_text", text );
@ -514,54 +496,9 @@
}.bind(c_stack), 600); }.bind(c_stack), 600);
} }
//// Captcha
, createCaptcha = loggedIn
? function() {}
: function ()
{
// Remove capcha
Dand.id( "recaptcha_field", true ).clear();
try
{
Recaptcha.render( "recaptcha_field", {
"theme": "light"
, "sitekey": Config.siteKey
} );
}
catch( ex ) { }
openCaptcha();
}
, openCaptcha = loggedIn
? function() {}
:function ()
{
var rf = Dand.id( "recaptcha_field" );
var k = function () {
return ( 0 < this.a.clientHeight );
}.bind({ a: rf });
Trigger.register(
k , function() {
captcha_wrapper.style.height = rf.clientHeight + "px";
}
, 50 );
}
, closeCaptcha = loggedIn
? function() {}
:function ()
{
captcha_wrapper.style.height = "0";
}
// Validation // Validation
, fields = loggedIn ? ["content"] : [ "content", "name", "email", "website", "email_notify" ] , fields = loggedIn ? ["content"] : [ "content", "name", "email", "website", "email_notify" ]
, _getFieldsValidated = function (obj) , getFieldsValidated = function (obj)
{ {
var name, field, falsefield, f; var name, field, falsefield, f;
@ -631,29 +568,6 @@
return true; return true;
} }
, getFieldsValidated = loggedIn
? /* Overload */ _getFieldsValidated
: /* Overload */ function(obj)
{
var f = Dand.id("false_capcha");
f.style.height = 0;
// get the response field
if(!( obj["recaptcha_response_field"] = Recaptcha.getResponse().trim() ))
{
Cycle.delay(
function()
{
this.innerHTML = "You need to prove that you are a human";
this.style.height = elmH;
}.bind(f)
, 500 );
}
// get the challenge field
// else obj["recaptcha_challenge_field"] = Recaptcha.get_challenge();
return _getFieldsValidated(obj);
}
, showReplyButton = function () , showReplyButton = function ()
{ {
this.style.width = "1.2em"; this.style.width = "1.2em";
@ -708,6 +622,7 @@
var limit = 5; var limit = 5;
var i = 0; var i = 0;
var reUUID = Perf.uuid; var reUUID = Perf.uuid;
RECAPTCHA_SITE_KEY = Dand.id( "recaptcha-js" ).src.split( "?render=" )[1];
Cycle.perma( Cycle.perma(
reUUID, function() reUUID, function()
{ {
@ -717,7 +632,7 @@
if( mesg.length ) mesg[0].style.display = "block"; if( mesg.length ) mesg[0].style.display = "block";
} }
if( Recaptcha = window["grecaptcha"] ) if(( Recaptcha = window["grecaptcha"] ))
{ {
Cycle.permaRemove( reUUID ); Cycle.permaRemove( reUUID );
debug.Info( "Recaptcha instance is up" ); debug.Info( "Recaptcha instance is up" );
@ -725,6 +640,9 @@
} , 500 } , 500
); );
} }
Cycle.next( nameInput );
Cycle.next( commentInput );
}; };
Bootstrap.regInit( init ); Bootstrap.regInit( init );

View File

@ -158,6 +158,7 @@
/** @type {_3rdParty_.Recaptcha} */ /** @type {_3rdParty_.Recaptcha} */
var Recaptcha; var Recaptcha;
var RECAPTCHA_SITE_KEY = null;
if( !cBubble ) if( !cBubble )
{ {
@ -168,36 +169,35 @@
var i = 0; var i = 0;
var errMesg = Dand.id( "contact-errmesg" ); var errMesg = Dand.id( "contact-errmesg" );
var submitMessage = function( action, name, email, mesg ) var submitMessage = function( action, data )
{ {
postData( action, { postData(
"name": name, "email": email, "mesg": mesg action, data
, "recaptcha_response_field": Recaptcha.getResponse().trim() , function ( e ) {
} doCollapse();
, function ( e ) {
doCollapse();
if( e.status ) if( e.status )
{ {
// pop message // pop message
cBubble.setColor( "cornflowerblue" ); cBubble.setColor( "cornflowerblue" );
Cycle.next( function () { cBubble.pop( "Message sent" ); } ); Cycle.next( function () { cBubble.pop( "Message sent" ); } );
// blurp after 1 sec // blurp after 1 sec
Cycle.delay( function () { cBubble.blurp(); }, 3500 ); Cycle.delay( function () { cBubble.blurp(); }, 3500 );
}
} }
} , function( err )
, function( err )
{
errMesg.innerHTML = "Error occurred";
if( !err.status )
{ {
errMesg.innerHTML = err[ "mesg" ]; errMesg.innerHTML = "Error occurred";
}
errMesg.removeAttribute( "data-disabled" ); if( !err.status )
} ); {
errMesg.innerHTML = err[ "mesg" ];
}
errMesg.removeAttribute( "data-disabled" );
}
);
}; };
mesgform.addEventListener( "Submit", function( e ) { mesgform.addEventListener( "Submit", function( e ) {
@ -223,24 +223,16 @@
return; return;
} }
submitMessage( action, name, email, mesg ); Recaptcha.execute( RECAPTCHA_SITE_KEY, { "action": "submit" } )
.then(function( token ) {
} ); submitMessage( action, {
"name": name
var CaptchaReady = function() , "email": email
{ , "mesg": mesg
captcha.clear(); , "token": token
try });
{
Recaptcha.render( "contact-recaptcha", {
"theme": "light", "sitekey": sitekey
} ); } );
} } );
catch( ex )
{
debug.Error( ex );
}
};
Cycle.perma( Cycle.perma(
reUUID, function() reUUID, function()
@ -253,8 +245,8 @@
if( Recaptcha = window["grecaptcha"] ) if( Recaptcha = window["grecaptcha"] )
{ {
RECAPTCHA_SITE_KEY = Dand.id( "recaptcha-js" ).src.split( "?render=" )[1];
Cycle.permaRemove( reUUID ); Cycle.permaRemove( reUUID );
CaptchaReady();
} }
} , 500 } , 500
); );

View File

@ -1,12 +1,5 @@
/** @constructor */ /** @constructor */
_3rdParty_.Recaptcha = function() {}; _3rdParty_.Recaptcha = function() {};
/** @type {Number} */
_3rdParty_.Recaptcha.timer_id;
/** @type {Function} */ /** @type {Function} */
_3rdParty_.render; _3rdParty_.execute;
/** @type {Function} */
_3rdParty_.reset;
/** @type {Function} */
_3rdParty_.getResponse;