[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/plugins/akismet/_inc/ -> akismet-frontend.js (source)

   1  /**
   2   * Observe how the user enters content into the comment form in order to determine whether it's a bot or not.
   3   *
   4   * Note that no actual input is being saved here, only counts and timings between events.
   5   */
   6  
   7  ( function() {
   8      // Passive event listeners are guaranteed to never call e.preventDefault(),
   9      // but they're not supported in all browsers.  Use this feature detection
  10      // to determine whether they're available for use.
  11      var supportsPassive = false;
  12  
  13      try {
  14          var opts = Object.defineProperty( {}, 'passive', {
  15              get : function() {
  16                  supportsPassive = true;
  17              }
  18          } );
  19  
  20          window.addEventListener( 'testPassive', null, opts );
  21          window.removeEventListener( 'testPassive', null, opts );
  22      } catch ( e ) {}
  23  
  24  	function init() {
  25          var input_begin = '';
  26  
  27          var keydowns = {};
  28          var lastKeyup = null;
  29          var lastKeydown = null;
  30          var keypresses = [];
  31  
  32          var modifierKeys = [];
  33          var correctionKeys = [];
  34  
  35          var lastMouseup = null;
  36          var lastMousedown = null;
  37          var mouseclicks = [];
  38          var mouseclickCoordinates = [];
  39  
  40          var mousemoveTimer = null;
  41          var lastMousemoveX = null;
  42          var lastMousemoveY = null;
  43          var lastMousemoveTime = null;
  44          var mousemoveStart = null;
  45          var mousemoves = [];
  46          var intervalsBetweenMousemovesAndClicks = [];
  47  
  48          var touchmoveCountTimer = null;
  49          var touchmoveCount = 0;
  50  
  51          var lastTouchEnd = null;
  52          var lastTouchStart = null;
  53          var touchEvents = [];
  54  
  55          var scrollCountTimer = null;
  56          var scrollCount = 0;
  57  
  58          var correctionKeyCodes = [ 'Backspace', 'Delete', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'PageUp', 'PageDown' ];
  59          var modifierKeyCodes = [ 'Shift', 'CapsLock' ];
  60  
  61          var forms = document.querySelectorAll( 'form[method=post]' );
  62  
  63          for ( var i = 0; i < forms.length; i++ ) {
  64              var form = forms[i];
  65  
  66              var formAction = form.getAttribute( 'action' );
  67  
  68              // Ignore forms that POST directly to other domains; these could be things like payment forms.
  69              if ( formAction ) {
  70                  // Check that the form is posting to an external URL, not a path.
  71                  if ( formAction.indexOf( 'http://' ) == 0 || formAction.indexOf( 'https://' ) == 0 ) {
  72                      if ( formAction.indexOf( 'http://' + window.location.hostname + '/' ) != 0 && formAction.indexOf( 'https://' + window.location.hostname + '/' ) != 0 ) {
  73                          continue;
  74                      }
  75                  }
  76              }
  77  
  78              form.addEventListener( 'submit', function () {
  79                  var ak_bkp = prepare_array_for_request( keypresses );
  80                  var ak_bmc = prepare_array_for_request( mouseclicks );
  81                  var ak_bte = prepare_array_for_request( touchEvents );
  82                  var ak_bmm = prepare_array_for_request( mousemoves );
  83                  var ak_bcc = prepare_array_for_request( mouseclickCoordinates );
  84                  var ak_bibmac = intervalsBetweenMousemovesAndClicks.slice( 0, 100 ).join( ';' );
  85  
  86                  var input_fields = {
  87                      // When did the user begin entering any input?
  88                      'bib': input_begin,
  89  
  90                      // When was the form submitted?
  91                      'bfs': Date.now(),
  92  
  93                      // How many keypresses did they make?
  94                      'bkpc': keypresses.length,
  95  
  96                      // How quickly did they press a sample of keys, and how long between them?
  97                      'bkp': ak_bkp,
  98  
  99                      // How quickly did they click the mouse, and how long between clicks?
 100                      'bmc': ak_bmc,
 101  
 102                      // How many mouseclicks did they make?
 103                      'bmcc': mouseclicks.length,
 104  
 105                      // When did they press modifier keys (like Shift or Capslock)?
 106                      'bmk': modifierKeys.join( ';' ),
 107  
 108                      // When did they correct themselves? e.g., press Backspace, or use the arrow keys to move the cursor back
 109                      'bck': correctionKeys.join( ';' ),
 110  
 111                      // How many times did they move the mouse?
 112                      'bmmc': mousemoves.length,
 113  
 114                      // How many times did they move around using a touchscreen?
 115                      'btmc': touchmoveCount,
 116  
 117                      // How many times did they scroll?
 118                      'bsc': scrollCount,
 119  
 120                      // How quickly did they perform touch events, and how long between them?
 121                      'bte': ak_bte,
 122  
 123                      // How many touch events were there?
 124                      'btec' : touchEvents.length,
 125  
 126                      // How quickly did they move the mouse, and how long between moves?
 127                      'bmm' : ak_bmm,
 128  
 129                      // Click coordinates
 130                      'bcc' : ak_bcc,
 131  
 132                      // Milliseconds between last mouse movement and each click
 133                      'bibmac' : ak_bibmac
 134                  };
 135  
 136                  var akismet_field_prefix = 'ak_';
 137  
 138                  if ( this.getElementsByClassName ) {
 139                      // Check to see if we've used an alternate field name prefix. We store this as an attribute of the container around some of the Akismet fields.
 140                      var possible_akismet_containers = this.getElementsByClassName( 'akismet-fields-container' );
 141  
 142                      for ( var containerIndex = 0; containerIndex < possible_akismet_containers.length; containerIndex++ ) {
 143                          var container = possible_akismet_containers.item( containerIndex );
 144  
 145                          if ( container.getAttribute( 'data-prefix' ) ) {
 146                              akismet_field_prefix = container.getAttribute( 'data-prefix' );
 147                              break;
 148                          }
 149                      }
 150                  }
 151  
 152                  for ( var field_name in input_fields ) {
 153                      var field = document.createElement( 'input' );
 154                      field.setAttribute( 'type', 'hidden' );
 155                      field.setAttribute( 'name', akismet_field_prefix + field_name );
 156                      field.setAttribute( 'value', input_fields[ field_name ] );
 157                      this.appendChild( field );
 158                  }
 159              }, supportsPassive ? { passive: true } : false  );
 160  
 161              form.addEventListener( 'keydown', function ( e ) {
 162                  // If you hold a key down, some browsers send multiple keydown events in a row.
 163                  // Ignore any keydown events for a key that hasn't come back up yet.
 164                  if ( e.key in keydowns ) {
 165                      return;
 166                  }
 167  
 168                  var keydownTime = ( new Date() ).getTime();
 169                  keydowns[ e.key ] = [ keydownTime ];
 170  
 171                  if ( ! input_begin ) {
 172                      input_begin = keydownTime;
 173                  }
 174  
 175                  // In some situations, we don't want to record an interval since the last keypress -- for example,
 176                  // on the first keypress, or on a keypress after focus has changed to another element. Normally,
 177                  // we want to record the time between the last keyup and this keydown. But if they press a
 178                  // key while already pressing a key, we want to record the time between the two keydowns.
 179  
 180                  var lastKeyEvent = Math.max( lastKeydown, lastKeyup );
 181  
 182                  if ( lastKeyEvent ) {
 183                      keydowns[ e.key ].push( keydownTime - lastKeyEvent );
 184                  }
 185  
 186                  lastKeydown = keydownTime;
 187              }, supportsPassive ? { passive: true } : false  );
 188  
 189              form.addEventListener( 'keyup', function ( e ) {
 190                  if ( ! ( e.key in keydowns ) ) {
 191                      // This key was pressed before this script was loaded, or a mouseclick happened during the keypress, or...
 192                      return;
 193                  }
 194  
 195                  var keyupTime = ( new Date() ).getTime();
 196  
 197                  if ( 'TEXTAREA' === e.target.nodeName || 'INPUT' === e.target.nodeName ) {
 198                      if ( -1 !== modifierKeyCodes.indexOf( e.key ) ) {
 199                          modifierKeys.push( keypresses.length - 1 );
 200                      } else if ( -1 !== correctionKeyCodes.indexOf( e.key ) ) {
 201                          correctionKeys.push( keypresses.length - 1 );
 202                      } else {
 203                          // ^ Don't record timings for keys like Shift or backspace, since they
 204                          // typically get held down for longer than regular typing.
 205  
 206                          var keydownTime = keydowns[ e.key ][0];
 207  
 208                          var keypress = [];
 209  
 210                          // Keypress duration.
 211                          keypress.push( keyupTime - keydownTime );
 212  
 213                          // Amount of time between this keypress and the previous keypress.
 214                          if ( keydowns[ e.key ].length > 1 ) {
 215                              keypress.push( keydowns[ e.key ][1] );
 216                          }
 217  
 218                          keypresses.push( keypress );
 219                      }
 220                  }
 221  
 222                  delete keydowns[ e.key ];
 223  
 224                  lastKeyup = keyupTime;
 225              }, supportsPassive ? { passive: true } : false  );
 226  
 227              form.addEventListener( "focusin", function ( e ) {
 228                  lastKeydown = null;
 229                  lastKeyup = null;
 230                  keydowns = {};
 231              }, supportsPassive ? { passive: true } : false  );
 232  
 233              form.addEventListener( "focusout", function ( e ) {
 234                  lastKeydown = null;
 235                  lastKeyup = null;
 236                  keydowns = {};
 237              }, supportsPassive ? { passive: true } : false  );
 238          }
 239  
 240          document.addEventListener( 'mousedown', function ( e ) {
 241              lastMousedown = ( new Date() ).getTime();
 242  
 243              if ( lastMousemoveTime ) {
 244                  intervalsBetweenMousemovesAndClicks.push( lastMousedown - lastMousemoveTime );
 245              }
 246  
 247              var mouseclickCoordinate = [];
 248  
 249              var rect = e.target.getBoundingClientRect();
 250              var relativeX = e.clientX - rect.left;
 251              var relativeY = e.clientY - rect.top;
 252  
 253              // Pixel offset of the click within the target element.
 254              mouseclickCoordinate.push( Math.round( relativeX ) );
 255              mouseclickCoordinate.push( Math.round( relativeY ) );
 256  
 257              // Percentage offset of the click within the target element.
 258              mouseclickCoordinate.push( rect.width > 0 ? Math.round( relativeX / rect.width * 100 ) : 0 );
 259              mouseclickCoordinate.push( rect.height > 0 ? Math.round( relativeY / rect.height * 100 ) : 0 );
 260  
 261              mouseclickCoordinates.push( mouseclickCoordinate );
 262          }, supportsPassive ? { passive: true } : false  );
 263  
 264          document.addEventListener( 'mouseup', function ( e ) {
 265              if ( ! lastMousedown ) {
 266                  // If the mousedown happened before this script was loaded, but the mouseup happened after...
 267                  return;
 268              }
 269  
 270              var now = ( new Date() ).getTime();
 271  
 272              var mouseclick = [];
 273              mouseclick.push( now - lastMousedown );
 274  
 275              if ( lastMouseup ) {
 276                  mouseclick.push( lastMousedown - lastMouseup );
 277              }
 278  
 279              mouseclicks.push( mouseclick );
 280  
 281              lastMouseup = now;
 282  
 283              // If the mouse has been clicked, don't record this time as an interval between keypresses.
 284              lastKeydown = null;
 285              lastKeyup = null;
 286              keydowns = {};
 287          }, supportsPassive ? { passive: true } : false  );
 288  
 289          document.addEventListener( 'mousemove', function ( e ) {
 290              lastMousemoveTime = ( new Date() ).getTime();
 291  
 292              if ( mousemoveTimer ) {
 293                  clearTimeout( mousemoveTimer );
 294                  mousemoveTimer = null;
 295              }
 296              else {
 297                  mousemoveStart = lastMousemoveTime;
 298                  lastMousemoveX = e.offsetX;
 299                  lastMousemoveY = e.offsetY;
 300              }
 301  
 302              mousemoveTimer = setTimeout( function ( theEvent, originalMousemoveStart ) {
 303                  var now = ( new Date() ).getTime() - 500; // To account for the timer delay.
 304  
 305                  var mousemove = [];
 306                  mousemove.push( now - originalMousemoveStart );
 307                  mousemove.push(
 308                      Math.round(
 309                          Math.sqrt(
 310                              Math.pow( theEvent.offsetX - lastMousemoveX, 2 ) +
 311                              Math.pow( theEvent.offsetY - lastMousemoveY, 2 )
 312                          )
 313                      )
 314                  );
 315  
 316                  if ( mousemove[1] > 0 ) {
 317                      // If there was no measurable distance, then it wasn't really a move.
 318                      mousemoves.push( mousemove );
 319                  }
 320  
 321                  mousemoveStart = null;
 322                  mousemoveTimer = null;
 323              }, 500, e, mousemoveStart );
 324          }, supportsPassive ? { passive: true } : false  );
 325  
 326          document.addEventListener( 'touchmove', function ( e ) {
 327              if ( touchmoveCountTimer ) {
 328                  clearTimeout( touchmoveCountTimer );
 329              }
 330  
 331              touchmoveCountTimer = setTimeout( function () {
 332                  touchmoveCount++;
 333              }, 500 );
 334          }, supportsPassive ? { passive: true } : false );
 335  
 336          document.addEventListener( 'touchstart', function ( e ) {
 337              lastTouchStart = ( new Date() ).getTime();
 338          }, supportsPassive ? { passive: true } : false );
 339  
 340          document.addEventListener( 'touchend', function ( e ) {
 341              if ( ! lastTouchStart ) {
 342                  // If the touchstart happened before this script was loaded, but the touchend happened after...
 343                  return;
 344              }
 345  
 346              var now = ( new Date() ).getTime();
 347  
 348              var touchEvent = [];
 349              touchEvent.push( now - lastTouchStart );
 350  
 351              if ( lastTouchEnd ) {
 352                  touchEvent.push( lastTouchStart - lastTouchEnd );
 353              }
 354  
 355              touchEvents.push( touchEvent );
 356  
 357              lastTouchEnd = now;
 358  
 359              // Don't record this time as an interval between keypresses.
 360              lastKeydown = null;
 361              lastKeyup = null;
 362              keydowns = {};
 363          }, supportsPassive ? { passive: true } : false );
 364  
 365          document.addEventListener( 'scroll', function ( e ) {
 366              if ( scrollCountTimer ) {
 367                  clearTimeout( scrollCountTimer );
 368              }
 369  
 370              scrollCountTimer = setTimeout( function () {
 371                  scrollCount++;
 372              }, 500 );
 373          }, supportsPassive ? { passive: true } : false );
 374      }
 375  
 376      /**
 377       * For the timing/coordinate data that is collected, don't send more than `limit` data points in the request.
 378       * Choose a random slice and send those, with each batch separated by semicolons and the items in each batch
 379       * separated by commas.
 380       */
 381  	function prepare_array_for_request( a, limit ) {
 382          if ( ! limit ) {
 383              limit = 100;
 384          }
 385  
 386          var rv = '';
 387  
 388          if ( a.length > 0 ) {
 389              var random_starting_point = Math.max( 0, Math.floor( Math.random() * a.length - limit ) );
 390  
 391              for ( var i = 0; i < limit && i < a.length; i++ ) {
 392                  var entry = a[ random_starting_point + i ];
 393                  rv += entry.join( ',' ) + ';';
 394              }
 395          }
 396  
 397          return rv;
 398      }
 399  
 400      if ( document.readyState !== 'loading' ) {
 401          init();
 402      } else {
 403          document.addEventListener( 'DOMContentLoaded', init );
 404      }
 405  })();


Generated : Wed Sep 9 08:20:27 2026 Cross-referenced by PHPXref