SHARE
TWEET

Untitled

a guest Aug 25th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function simulate(element, eventName, options)
  2. {
  3.     var options = extend(defaultOptions, arguments[2] || {});
  4.     var oEvent, eventType = null;
  5.  
  6.     for (var name in eventMatchers)
  7.     {
  8.         if (eventMatchers[name].test(eventName)) { eventType = name; break; }
  9.     }
  10.  
  11.     if (!eventType)
  12.         throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
  13.  
  14.     if (document.createEvent)
  15.     {
  16.         oEvent = document.createEvent(eventType);
  17.         if (eventType == 'HTMLEvents')
  18.         {
  19.             oEvent.initEvent(eventName, options.bubbles, options.cancelable);
  20.         }
  21.         else
  22.         {
  23.             oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
  24.             options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
  25.             options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
  26.         }
  27.         element.dispatchEvent(oEvent);
  28.     }
  29.     else
  30.     {
  31.         options.clientX = options.pointerX;
  32.         options.clientY = options.pointerY;
  33.         var evt = document.createEventObject();
  34.         oEvent = extend(evt, options);
  35.         element.fireEvent('on' + eventName, oEvent);
  36.     }
  37.     return element;
  38. }
  39.  
  40. function extend(destination, source) {
  41.     for (var property in source)
  42.       destination[property] = source[property];
  43.     return destination;
  44. }
  45.  
  46. var eventMatchers = {
  47.     'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
  48.     'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
  49. }
  50. var defaultOptions = {
  51.     pointerX: 0,
  52.     pointerY: 0,
  53.     button: 0,
  54.     ctrlKey: false,
  55.     altKey: false,
  56.     shiftKey: false,
  57.     metaKey: false,
  58.     bubbles: true,
  59.     cancelable: true
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top