exit-intent-mobile
Version:
Exit intent detection library. Including support for desktop and mobile.
2 lines (1 loc) • 7.02 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.ExitIntent=t()}(this,function(){"use strict";function t(e,t,o){n(e,u,t,o),"DOMMouseScroll"==u&&n(e,"MozMousePixelScroll",t,o)}function n(e,t,n,o){e[i](q+t,"wheel"==u?n:function(e){!e&&(e=window.event);var t={originalEvent:e,target:e.target||e.srcElement,type:"wheel",deltaMode:"MozMousePixelScroll"==e.type?0:1,deltaX:0,deltaY:0,deltaZ:0,clientX:e.clientX,clientY:e.clientY,preventDefault:function(){e.preventDefault?e.preventDefault():e.returnValue=!1},stopPropagation:function(){e.stopPropagation&&e.stopPropagation()},stopImmediatePropagation:function(){e.stopImmediatePropagation&&e.stopImmediatePropagation()}};return"mousewheel"==u?(t.deltaY=-.025*e.wheelDelta,e.wheelDeltaX&&(t.deltaX=-.025*e.wheelDeltaX)):t.deltaY=e.detail,n(t)},o||!1)}function o(e,t,n,o){e[r](q+t,n,o||!1)}var i,r,u,a=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)},c="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},l="object"==typeof c&&c&&c.Object===Object&&c,f="object"==typeof self&&self&&self.Object===Object&&self,d=l||f||Function("return this")(),s=function(){return d.Date.now()},v=d.Symbol,p=Object.prototype,m=p.hasOwnProperty,h=p.toString,y=v?v.toStringTag:void 0,w=function(e){var t=m.call(e,y),n=e[y];try{e[y]=void 0;var o=!0}catch(e){}var i=h.call(e);return o&&(t?e[y]=n:delete e[y]),i},g=Object.prototype.toString,b=function(e){return g.call(e)},x="[object Null]",E="[object Undefined]",M=v?v.toStringTag:void 0,S=function(e){return null==e?void 0===e?E:x:M&&M in Object(e)?w(e):b(e)},T=function(e){return null!=e&&"object"==typeof e},j="[object Symbol]",D=function(e){return"symbol"==typeof e||T(e)&&S(e)==j},O=NaN,P=/^\s+|\s+$/g,I=/^[-+]0x[0-9a-f]+$/i,k=/^0b[01]+$/i,L=/^0o[0-7]+$/i,A=parseInt,_=function(e){if("number"==typeof e)return e;if(D(e))return O;if(a(e)){var t="function"==typeof e.valueOf?e.valueOf():e;e=a(t)?t+"":t}if("string"!=typeof e)return 0===e?e:+e;e=e.replace(P,"");var n=k.test(e);return n||L.test(e)?A(e.slice(2),n?2:8):I.test(e)?O:+e},W="Expected a function",X=Math.max,Y=Math.min,$=function(e,t,n){function o(t){var n=d,o=v;return d=v=void 0,w=t,m=e.apply(o,n)}function i(e){return w=e,h=setTimeout(c,t),g?o(e):m}function r(e){var n=e-w,o=t-(e-y);return b?Y(o,p-n):o}function u(e){var n=e-y,o=e-w;return void 0===y||n>=t||n<0||b&&o>=p}function c(){var e=s();if(u(e))return l(e);h=setTimeout(c,r(e))}function l(e){return h=void 0,x&&d?o(e):(d=v=void 0,m)}function f(){var e=s(),n=u(e);if(d=arguments,v=this,y=e,n){if(void 0===h)return i(y);if(b)return h=setTimeout(c,t),o(y)}return void 0===h&&(h=setTimeout(c,t)),m}var d,v,p,m,h,y,w=0,g=!1,b=!1,x=!0;if("function"!=typeof e)throw new TypeError(W);return t=_(t)||0,a(n)&&(g=!!n.leading,p=(b="maxWait"in n)?X(_(n.maxWait)||0,t):p,x="trailing"in n?!!n.trailing:x),f.cancel=function(){void 0!==h&&clearTimeout(h),w=0,d=y=v=h=void 0},f.flush=function(){return void 0===h?m:l(s())},f},z="Expected a function",N=function(e,t,n){var o=!0,i=!0;if("function"!=typeof e)throw new TypeError(z);return a(n)&&(o="leading"in n?!!n.leading:o,i="trailing"in n?!!n.trailing:i),$(e,t,{leading:o,maxWait:t,trailing:i})},F=function(e){return e&&e.__esModule?e.default:e}(function(e,t){return t={exports:{}},e(t,t.exports),t.exports}(function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(){return!("undefined"==typeof window||!("ontouchstart"in window||window.DocumentTouch&&"undefined"!=typeof document&&document instanceof window.DocumentTouch))||!("undefined"==typeof navigator||!navigator.maxTouchPoints&&!navigator.msMaxTouchPoints)},e.exports=t.default})),U=t,V=t,Z=function(e,t,n){o(e,u,t,n),"DOMMouseScroll"==u&&o(e,"MozMousePixelScroll",t,n)},q="";!function(e,t){e&&e.addEventListener?(i="addEventListener",r="removeEventListener"):(i="attachEvent",r="detachEvent",q="on"),u=t?"onwheel"in t.createElement("div")?"wheel":void 0!==t.onmousewheel?"mousewheel":"DOMMouseScroll":"wheel"}("undefined"!=typeof window&&window,"undefined"!=typeof document&&document),U.addWheelListener=V,U.removeWheelListener=Z;!function(){function e(e){this.value=e}function t(t){function n(i,r){try{var u=t[i](r),a=u.value;a instanceof e?Promise.resolve(a.value).then(function(e){n("next",e)},function(e){n("throw",e)}):o(u.done?"return":"normal",u.value)}catch(e){o("throw",e)}}function o(e,t){switch(e){case"return":i.resolve({value:t,done:!0});break;case"throw":i.reject(t);break;default:i.resolve({value:t,done:!1})}(i=i.next)?n(i.key,i.arg):r=null}var i,r;this._invoke=function(e,t){return new Promise(function(o,u){var a={key:e,arg:t,resolve:o,reject:u,next:null};r?r=r.next=a:(i=r=a,n(e,t))})},"function"!=typeof t.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)}}();var B=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},C=!F();return function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=B({},{debug:!1,maxDisplays:99999,eventThrottle:200,showAfterInactiveSecondsDesktop:60,showAfterInactiveSecondsMobile:40,showAgainAfterSeconds:10,onExitIntent:function(){}},t),o=function(){for(var e=arguments.length,t=Array(e),o=0;o<e;o++)t[o]=arguments[o];if(n.debug){var i;(i=console).log.apply(i,["[exit-intent-mobile]"].concat(t))}},i=0,r=N(function(){i<n.maxDisplays&&(o("display onExitIntent",++i),n.onExitIntent(),i>=n.maxDisplays&&s())},1e3*n.showAgainAfterSeconds,{trailing:!1}),u=void 0;C&&(o("register mouseleave for desktop"),u=document.body.addEventListener("mouseleave",N(function(){e instanceof MouseEvent&&(o("mouseleave"),r())},n.eventThrottle),!1));var a=C?n.showAfterInactiveSecondsDesktop:n.showAfterInactiveSecondsMobile;o("timeoutOnDevice",a);var c=void 0,l=function(){void 0!==a?(void 0!==c&&(o("clearTimeout()"),window.clearTimeout(c)),c=window.setTimeout(function(){o("display after timeout"),r()},1e3*a)):o("display disabled")};l();var f=[],d=function(e,t){o("registering event for restartTimer",e,t);var i=t.addEventListener(e,N(function(e){o("throttled listener",e),l()},n.eventThrottle),!1);return f.push({event:e,listener:i,target:t}),i};C&&(d("scroll",window),d("mousemove",window),V(window,function(e){N(function(e){o("throttled wheel listener",e),l()},n.eventThrottle)})),F&&(d("touchstart",document.body),d("touchend",document.body),d("touchmove",document.body));var s=function(){o("removeEvents",i),u&&(document.body.removeEventListener("mouseleave",u),Z(window,function(){o("removeWheelListener")})),f.forEach(function(e){var t=e.event,n=e.listener;e.target.removeEventListener(t,n)})};return s}});