Simulate_click

Simulate click

目前为 2016-03-06 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/1930/111530/Simulate_click.js

  1. // ==UserScript==
  2. // @name Simulate_click
  3. // @namespace Simulate_click
  4. // @version 1
  5. // @description Simulate click
  6. // ==/UserScript==
  7.  
  8. (function( $, undefined ) {
  9.  
  10. var rkeyEvent = /^key/,
  11. rmouseEvent = /^(?:mouse|contextmenu)|click/;
  12. $.fn.simulate = function( type, options ) {
  13. return this.each(function() {
  14. new $.simulate( this, type, options );
  15. });
  16. };
  17. $.simulate = function( elem, type, options ) {
  18. var method = $.camelCase( "simulate-" + type );
  19. this.target = elem;
  20. this.options = options;
  21. if ( this[ method ] ) {
  22. this[ method ]();
  23. } else {
  24. this.simulateEvent( elem, type, options );
  25. }
  26. };
  27. $.extend( $.simulate, {
  28. keyCode: {
  29. BACKSPACE: 8,
  30. COMMA: 188,
  31. DELETE: 46,
  32. DOWN: 40,
  33. END: 35,
  34. ENTER: 13,
  35. ESCAPE: 27,
  36. HOME: 36,
  37. LEFT: 37,
  38. NUMPAD_ADD: 107,
  39. NUMPAD_DECIMAL: 110,
  40. NUMPAD_DIVIDE: 111,
  41. NUMPAD_ENTER: 108,
  42. NUMPAD_MULTIPLY: 106,
  43. NUMPAD_SUBTRACT: 109,
  44. PAGE_DOWN: 34,
  45. PAGE_UP: 33,
  46. PERIOD: 190,
  47. RIGHT: 39,
  48. SPACE: 32,
  49. TAB: 9,
  50. UP: 38
  51. },
  52. buttonCode: {
  53. LEFT: 0,
  54. MIDDLE: 1,
  55. RIGHT: 2
  56. }
  57. });
  58. $.extend( $.simulate.prototype, {
  59. simulateEvent: function( elem, type, options ) {
  60. var event = this.createEvent( type, options );
  61. this.dispatchEvent( elem, type, event, options );
  62. },
  63. createEvent: function( type, options ) {
  64. if ( rkeyEvent.test( type ) ) {
  65. return this.keyEvent( type, options );
  66. }
  67. if ( rmouseEvent.test( type ) ) {
  68. return this.mouseEvent( type, options );
  69. }
  70. },
  71. mouseEvent: function( type, options ) {
  72. var event, eventDoc, doc, body;
  73. options = $.extend({
  74. bubbles: true,
  75. cancelable: (type !== "mousemove"),
  76. view: window,
  77. detail: 0,
  78. screenX: 0,
  79. screenY: 0,
  80. clientX: 1,
  81. clientY: 1,
  82. ctrlKey: false,
  83. altKey: false,
  84. shiftKey: false,
  85. metaKey: false,
  86. button: 0,
  87. relatedTarget: undefined
  88. }, options );
  89. if ( document.createEvent ) {
  90. event = document.createEvent( "MouseEvents" );
  91. event.initMouseEvent( type, options.bubbles, options.cancelable,
  92. options.view, options.detail,
  93. options.screenX, options.screenY, options.clientX, options.clientY,
  94. options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
  95. options.button, options.relatedTarget || document.body.parentNode );
  96. // IE 9+ creates events with pageX and pageY set to 0.
  97. // Trying to modify the properties throws an error,
  98. // so we define getters to return the correct values.
  99. if ( event.pageX === 0 && event.pageY === 0 && Object.defineProperty ) {
  100. eventDoc = event.relatedTarget.ownerDocument || document;
  101. doc = eventDoc.documentElement;
  102. body = eventDoc.body;
  103. Object.defineProperty( event, "pageX", {
  104. get: function() {
  105. return options.clientX +
  106. ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
  107. ( doc && doc.clientLeft || body && body.clientLeft || 0 );
  108. }
  109. });
  110. Object.defineProperty( event, "pageY", {
  111. get: function() {
  112. return options.clientY +
  113. ( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
  114. ( doc && doc.clientTop || body && body.clientTop || 0 );
  115. }
  116. });
  117. }
  118. } else if ( document.createEventObject ) {
  119. event = document.createEventObject();
  120. $.extend( event, options );
  121. // standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
  122. // old IE event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ms533544(v=vs.85).aspx
  123. // so we actually need to map the standard back to oldIE
  124. event.button = {
  125. 0: 1,
  126. 1: 4,
  127. 2: 2
  128. }[ event.button ] || event.button;
  129. }
  130. return event;
  131. },
  132. keyEvent: function( type, options ) {
  133. var event;
  134. options = $.extend({
  135. bubbles: true,
  136. cancelable: true,
  137. view: window,
  138. ctrlKey: false,
  139. altKey: false,
  140. shiftKey: false,
  141. metaKey: false,
  142. keyCode: 0,
  143. charCode: undefined
  144. }, options );
  145. if ( document.createEvent ) {
  146. try {
  147. event = document.createEvent( "KeyEvents" );
  148. event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
  149. options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
  150. options.keyCode, options.charCode );
  151. // initKeyEvent throws an exception in WebKit
  152. // see: http://stackoverflow.com/questions/6406784/initkeyevent-keypress-only-works-in-firefox-need-a-cross-browser-solution
  153. // and also https://bugs.webkit.org/show_bug.cgi?id=13368
  154. // fall back to a generic event until we decide to implement initKeyboardEvent
  155. } catch( err ) {
  156. event = document.createEvent( "Events" );
  157. event.initEvent( type, options.bubbles, options.cancelable );
  158. $.extend( event, {
  159. view: options.view,
  160. ctrlKey: options.ctrlKey,
  161. altKey: options.altKey,
  162. shiftKey: options.shiftKey,
  163. metaKey: options.metaKey,
  164. keyCode: options.keyCode,
  165. charCode: options.charCode
  166. });
  167. }
  168. } else if ( document.createEventObject ) {
  169. event = document.createEventObject();
  170. $.extend( event, options );
  171. }
  172. if ( !!/msie [\w.]+/.exec( navigator.userAgent.toLowerCase() ) || (({}).toString.call( window.opera ) === "[object Opera]") ) {
  173. event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
  174. event.charCode = undefined;
  175. }
  176. return event;
  177. },
  178. dispatchEvent: function( elem, type, event ) {
  179. if ( elem.dispatchEvent ) {
  180. elem.dispatchEvent( event );
  181. } else if ( elem.fireEvent ) {
  182. elem.fireEvent( "on" + type, event );
  183. }
  184. },
  185. simulateFocus: function() {
  186. var focusinEvent,
  187. triggered = false,
  188. element = $( this.target );
  189. function trigger() {
  190. triggered = true;
  191. }
  192. element.bind( "focus", trigger );
  193. element[ 0 ].focus();
  194. if ( !triggered ) {
  195. focusinEvent = $.Event( "focusin" );
  196. focusinEvent.preventDefault();
  197. element.trigger( focusinEvent );
  198. element.triggerHandler( "focus" );
  199. }
  200. element.unbind( "focus", trigger );
  201. },
  202. simulateBlur: function() {
  203. var focusoutEvent,
  204. triggered = false,
  205. element = $( this.target );
  206. function trigger() {
  207. triggered = true;
  208. }
  209. element.bind( "blur", trigger );
  210. element[ 0 ].blur();
  211. // blur events are async in IE
  212. setTimeout(function() {
  213. // IE won't let the blur occur if the window is inactive
  214. if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
  215. element[ 0 ].ownerDocument.body.focus();
  216. }
  217. // Firefox won't trigger events if the window is inactive
  218. // IE doesn't trigger events if we had to manually focus the body
  219. if ( !triggered ) {
  220. focusoutEvent = $.Event( "focusout" );
  221. focusoutEvent.preventDefault();
  222. element.trigger( focusoutEvent );
  223. element.triggerHandler( "blur" );
  224. }
  225. element.unbind( "blur", trigger );
  226. }, 1 );
  227. }
  228. });
  229. /** complex events **/
  230. function findCenter( elem ) {
  231. var offset,
  232. document = $( elem.ownerDocument );
  233. elem = $( elem );
  234. offset = elem.offset();
  235. return {
  236. x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
  237. y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
  238. };
  239. }
  240. function findCorner( elem ) {
  241. var offset,
  242. document = $( elem.ownerDocument );
  243. elem = $( elem );
  244. offset = elem.offset();
  245. return {
  246. x: offset.left - document.scrollLeft(),
  247. y: offset.top - document.scrollTop()
  248. };
  249. }
  250. $.extend( $.simulate.prototype, {
  251. simulateDrag: function() {
  252. var i = 0,
  253. target = this.target,
  254. options = this.options,
  255. center = options.handle === "corner" ? findCorner( target ) : findCenter( target ),
  256. x = Math.floor( center.x ),
  257. y = Math.floor( center.y ),
  258. coord = { clientX: x, clientY: y },
  259. dx = options.dx || ( options.x !== undefined ? options.x - x : 0 ),
  260. dy = options.dy || ( options.y !== undefined ? options.y - y : 0 ),
  261. moves = options.moves || 3;
  262. this.simulateEvent( target, "mousedown", coord );
  263. for ( ; i < moves ; i++ ) {
  264. x += dx / moves;
  265. y += dy / moves;
  266. coord = {
  267. clientX: Math.round( x ),
  268. clientY: Math.round( y )
  269. };
  270. this.simulateEvent( target.ownerDocument, "mousemove", coord );
  271. }
  272. if ( $.contains( document, target ) ) {
  273. this.simulateEvent( target, "mouseup", coord );
  274. this.simulateEvent( target, "click", coord );
  275. } else {
  276. this.simulateEvent( document, "mouseup", coord );
  277. }
  278. }
  279. });
  280.  
  281. })( jQuery );