Skip to content

Instantly share code, notes, and snippets.

@sleemanj
Last active August 8, 2024 10:04
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);
}
});
@tinodj
Copy link

tinodj commented May 9, 2024

Now this error appears:

TypeError: Properties can only be defined on Objects.

@Magnum5234
Copy link

In case someone needs because of TypeError: undefined is not an object

  bind: function (bind) {
    const args = Array.from(arguments)
    if ((new Error()).stack.match(/recaptcha/)) {
      return this._polybind.apply(this, args)
    }

    return this._compatbind.apply(this, args)
  }

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