honeypot.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. (function ($) {
  2. Drupal.honeypot = {};
  3. Drupal.honeypot.timestampJS = new Date();
  4. Drupal.behaviors.honeypotJS = {
  5. attach: function (context, settings) {
  6. $('form.honeypot-timestamp-js').once('honeypot-timestamp').bind('submit', function() {
  7. var $honeypotTime = $(this).find('input[name="honeypot_time"]');
  8. $honeypotTime.attr('value', Drupal.behaviors.honeypotJS.getIntervalTimestamp());
  9. });
  10. },
  11. getIntervalTimestamp: function() {
  12. var now = new Date();
  13. var interval = Math.floor((now - Drupal.honeypot.timestampJS) / 1000);
  14. return Drupal.settings.honeypot.jsToken + '|' + interval;
  15. }
  16. };
  17. if (Drupal.ajax && Drupal.ajax.prototype && Drupal.ajax.prototype.beforeSubmit) {
  18. Drupal.ajax.prototype.honeypotOriginalBeforeSubmit = Drupal.ajax.prototype.beforeSubmit;
  19. Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) {
  20. if (this.form && $(this.form).hasClass('honeypot-timestamp-js')) {
  21. for (key in form_values) {
  22. // Inject the right interval timestamp.
  23. if (form_values[key].name == 'honeypot_time' && form_values[key].value == 'no_js_available') {
  24. form_values[key].value = Drupal.behaviors.honeypotJS.getIntervalTimestamp();
  25. }
  26. }
  27. }
  28. // Call the original function in case someone else has overridden it.
  29. return Drupal.ajax.prototype.honeypotOriginalBeforeSubmit(form_values, element, options);
  30. }
  31. }
  32. }(jQuery));