Super-Easy ReCaptcha 2.0

Share your advanced knowledge/code with the community.
tj1010
Posts: 201
Joined: Wed May 27, 2015 1:36 pm
Contact:

Super-Easy ReCaptcha 2.0

Post by tj1010 »

Code: Select all

Procedure DrawReCaptcha(x,y,sitekey$)
  If Len(sitekey$)>0
    !try{
    !var rcptcha=document.createElement('div');
    !rcptcha.setAttribute('class','g-recaptcha');
    !rcptcha.setAttribute('data-sitekey',v_sitekey$);
    !rcptcha.setAttribute('style','position:fixed;z-index:9999999999999;top:'+v_y+';left:'+v_x+';');
    !document.body.appendChild(rcptcha);
    !var gscript=document.createElement('script');
    !gscript.setAttribute('type','text/javascript');
    !gscript.setAttribute('src','https://www.google.com/recaptcha/api.js');
    !gscript.setAttribute('async','');
    !gscript.setAttribute('defer','');
    !document.getElementsByTagName('head')[0].appendChild(gscript);
    !}catch(err){
    !}
  EndIf
EndProcedure

Procedure DeleteReCaptcha()
  !try{
  !document.body.removeChild(document.getElementByClass('g-recaptcha')[0]);
  !}catch(err){
  !}
EndProcedure

Procedure.s GetReCaptcha()
  Protected answ$=""
  !try{
  !v_answ=grecaptcha.getResponse();
  !}catch(err){
  !}
  ProcedureReturn answ$
EndProcedure

;demo
ExamineDesktops()
DrawReCaptcha((DesktopWidth(0)/2)-200,(DesktopHeight(0)/2)-200,"YOURSITEKEYHERE")
Repeat
  ;do stuff
Until Len(GetReCaptcha())>0
;send it to your server for verification with google api
NOTES:
  • I haven't tested it just going by up to date docs and my experience writing it. You check GetReCaptcha() string on your back-end using cURL or whatever with the private-key and google returns some JSON you check.
  • To use it in SB gadgets you calculate it's desktop x,y(not window-relative) and set this relative to it's x and y.
  • You can use it in mobile apps by removing domain check in Google control panel(when SB team actually fixes http on Android and IOS)
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Super-Easy ReCaptcha 2.0

Post by Peter »

Browser wrote:ReferenceError: v_sitekey is not defined
change:

Code: Select all

    !rcptcha.setAttribute('data-sitekey',v_sitekey);
to:

Code: Select all

    !rcptcha.setAttribute('data-sitekey',v_sitekey$);
Greetings ... Peter
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: Super-Easy ReCaptcha 2.0

Post by Fred »

Changed
Post Reply