Skip to content

Instantly share code, notes, and snippets.

@sleemanj
Last active November 16, 2023 10:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sleemanj/f076ed2c0b887ab08074b55dad2fd636 to your computer and use it in GitHub Desktop.
Save sleemanj/f076ed2c0b887ab08074b55dad2fd636 to your computer and use it in GitHub Desktop.
Fix for Mootools 1.3 with 1.2 compatibility layer to work with Recaptcha due to bind being incorrect with 1.2 compatibility enabled.
/* Quick Dirty Patch for Recaptcha with Mootools 1.2 Compatability Layer
*
* Make sure that all your mootools core/more source is loaded before this
* fix is loaded.
*
* @author James Sleeman <james@gogo.co.nz>
* @see https://github.com/google/recaptcha/issues/374
*
*/
// Grab the Mootools 1.2 Compatability bind which mootools replaced
Function.prototype._compatbind = Function.prototype.bind;
// Remove it from the Function prototype
delete Function.prototype.bind;
Function.implement({
// This is the "polyfill" bind from Mootools 1.3 it should work the same
// as the actual native bind
_polybind: function(bind){
var self = this,
args = (arguments.length > 1) ? Array.slice(arguments, 1) : null;
return function(){
if (!args && !arguments.length) return self.call(bind);
if (args && arguments.length) return self.apply(bind, args.concat(Array.from(arguments)));
return self.apply(bind, args || arguments);
};
},
// Now if recaptcha calls bind, delegate to the polyfill one
// and if anything else calls bind delegate o the 1.2 compatability bind
// as was previously the case
bind: function(bind, args){
if( (new Error()).stack.match(/recaptcha/) )
{
return this._polybind(bind, args);
}
return this._compatbind(bind, args);
}
});
@jacksleight
Copy link

For anyone still supporting IE 11 this doesn't quite work as IE 11 doesn't initialise the Error().stack until the error is thrown. This change works:

  bind: function(bind, args){    
    var stack = new Error().stack;
    if (!stack) {
        try {
            throw new Error();
        } catch (e) {
            stack = e.stack;
        }
    }
    if( stack.match(/recaptcha/) )
    {
      return this._polybind(bind, args);
    }
    return this._compatbind(bind, args);
  }

@rasho62
Copy link

rasho62 commented Aug 6, 2021

Hi Sleemanj,
Thank you for posting the fix for mootools with recaptcha. It worked to get recpatcha to load. Only problem is now I am unable to submit form. Do you know what might cause this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment