@livelybone/mouse-mock
Version:
A lib for mock mouse events like click, move
10 lines (8 loc) • 5.92 kB
JavaScript
/**
* Bundle of @livelybone/mouse-mock
* Generated: 2021-07-01
* Version: 1.3.5
* License: MIT
* Author: 2631541504@qq.com
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).MouseMock={})}(this,function(e){"use strict";function m(e){var t=e.ownerDocument,n=t.getElementById("script-for-inject-owner-window");return!e.ownerWindow&&n&&(n.id="",n=null),n||((n=t.createElement("script")).type="text/javascript",n.id="script-for-inject-owner-window",n.innerHTML="HTMLElement.prototype.ownerWindow = window;",t.head.appendChild(n)),e.ownerWindow}function t(t,e){var n,r=Object.keys(t);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(t),e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)),r}function y(r){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{};e%2?t(Object(o),!0).forEach(function(e){var t,n;t=r,e=o[n=e],n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach(function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(o,e))})}return r}function h(e){return function(e){if(Array.isArray(e))return r(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return r(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Map"===(n="Object"===n&&e.constructor?e.constructor.name:n)||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(e,t):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function o(e){var t=window.getComputedStyle(e),n={top:0,left:0};return"HTML"!==e.nodeName&&!["scroll","auto","overlay"].includes(t.overflowX)||(n.left=Math.max(0,e.scrollWidth-e.clientWidth)),"HTML"!==e.nodeName&&!["scroll","auto","overlay"].includes(t.overflowY)||(n.top=Math.max(0,e.scrollHeight-e.clientHeight)),n}function i(e){return!e.top&&!e.left}function v(e){if(null!=e&&e.style){var t=window.getComputedStyle(e);if("fixed"!==t.position){var n=e.ownerDocument;if("absolute"===t.position)return e.offsetParent?v(e.offsetParent):i(o(n.body))?v(n.body):n.body;e=e.parentElement;return e?i(o(e))&&e!==n.documentElement?v(e):e:void 0}}}function c(n){var t,r,o,i,e="nodeType"in n?m(n):window,c=[{x:0,y:0,width:window.innerWidth,height:window.innerHeight}],l="style"in n?n.getBoundingClientRect():n,u=function(){if("style"in n){for(var e=[],t=v(n);t;)e.push(t.getBoundingClientRect()),t=v(t);return e.filter(function(e){return e.width&&e.height})}return[]}(),u=(r=t=-1/0,i=o=1/0,[l].concat(h(u),c).forEach(function(e){t=Math.max(t,e.x),r=Math.max(r,e.y),o=Math.min(o,e.x+e.width),i=Math.min(i,e.y+e.height)}),{x:t,y:r,width:o-t,height:i-r});if(u.width<=0||u.height<=0)return{clientX:-1,clientY:-1,screenY:-1,screenX:-1};var a,f,s={clientX:0,clientY:0},c={screenX:0,screenY:0};if(l){var d=Math.min(u.width/2,u.height/2),p={x:u.x+u.width/2,y:u.y+u.height/2};if(1<=d)for(;s.clientX=Math.floor(p.x+d*Math.random()*(.5<Math.random()?1:-1)),s.clientY=Math.floor(p.y+d*Math.random()*(.5<Math.random()?1:-1)),f=a=void 0,a=s.clientX-p.x,f=s.clientY-p.y,!(a*a+f*f<=d*d););else s.clientX=Math.floor(p.x),s.clientY=Math.floor(p.y)}u=e.innerWidth,e=e.innerHeight;return c.screenX=s.clientX-u,c.screenY=s.clientY-e,y(y({},s),c)}function l(n){var e,t;if(n.isTrusted)return n;for(var r={},o=n;o.__proto__;)Object.keys(o.__proto__).forEach(function(t){Object.defineProperty(r,t,{get:function(){var e=n[t];return"function"==typeof e?e.bind(n):e},configurable:!0})}),o=o.__proto__;return r.isTrusted=!0,r.__proto__=(null===(e=n.__proto__)||void 0===e||null===(t=e.constructor)||void 0===t?void 0:t.prototype)||n.__proto__,r}var u=new Map;function n(e,t){if(t){var n=c(t),r=t&&"nodeName"in t?t:document.elementFromPoint(n.clientX,n.clientY),o=null==r?void 0:r.ownerDocument.createEvent("MouseEvent");return r?(o.initMouseEvent(e,!0,!0,m(r),0,n.screenX,n.screenY,n.clientX,n.clientY,!1,!1,!1,!1,0,r),i=o,(o=r).dispatchEvent?o.dispatchEvent(i):null!==(r=o.fireEvent)&&void 0!==r&&r.call(o,i)):console.error("Cannot find element for ".concat(e,", it seems that the element or the rect you provide is out of the viewport.\n"),{el:t,point:n}),{point:n}}var i}e.mockClick=function(e){n("mouseenter",e),n("mouseover",e),n("mousemove",e),n("mousedown",e),n("mouseup",e);var t=n("click",e);return n("mousemove",e),n("mouseout",e),n("mouseleave",e),t},e.mockMouseEvent=n,e.rewriteListenerBinder=function(){window.$$RewriteListenerBinderSuccess$$||(Element.prototype._addEventListener=Element.prototype.addEventListener,Element.prototype._removeEventListener=Element.prototype.removeEventListener,Element.prototype.addEventListener=function(e,t){var n=u.get(t)||function(e){return t(l(e))};this._addEventListener(e,n)},Element.prototype.removeEventListener=function(e,t){var n=u.get(t);n&&(this._removeEventListener(e,n),u.delete(t))},Object.getOwnPropertyNames(HTMLElement.prototype).forEach(function(e){var t;e.startsWith("on")&&(t=Object.getOwnPropertyDescriptor(HTMLElement.prototype,e),Object.defineProperty(HTMLElement.prototype,"_".concat(e),t),Object.defineProperty(HTMLElement.prototype,e,y(y({},t),{},{set:function(t){this["_".concat(e)]="function"==typeof t?function(e){return t.call(this,l(e))}:t}})))}),Object.defineProperty(window,"$$RewriteListenerBinderSuccess$$",{value:!0,writable:!1,configurable:!0}))},Object.defineProperty(e,"__esModule",{value:!0})});