UNPKG

exit-intent-mobile-bugfix

Version:

Exit intent detection library. Including support for desktop and mobile.

2 lines (1 loc) 7.44 kB
!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 e(e,n,o){t(e,r,n,o),"DOMMouseScroll"==r&&t(e,"MozMousePixelScroll",n,o)}function t(e,t,n,i){e[o](V+t,"wheel"==r?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"==r?(t.deltaY=-.025*e.wheelDelta,e.wheelDeltaX&&(t.deltaX=-.025*e.wheelDeltaX)):t.deltaY=e.detail,n(t)},i||!1)}function n(e,t,n,o){e[i](V+t,n,o||!1)}var o,i,r,u=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)},a="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},l="object"==typeof a&&a&&a.Object===Object&&a,c="object"==typeof self&&self&&self.Object===Object&&self,f=l||c||Function("return this")(),s=function(){return f.Date.now()},d=f.Symbol,v=Object.prototype,p=v.hasOwnProperty,m=v.toString,h=d?d.toStringTag:void 0,w=function(e){var t=p.call(e,h),n=e[h];try{e[h]=void 0;var o=!0}catch(e){}var i=m.call(e);return o&&(t?e[h]=n:delete e[h]),i},y=Object.prototype.toString,g=function(e){return y.call(e)},b="[object Null]",x="[object Undefined]",T=d?d.toStringTag:void 0,E=function(e){return null==e?void 0===e?x:b:T&&T in Object(e)?w(e):g(e)},M=function(e){return null!=e&&"object"==typeof e},S="[object Symbol]",j=function(e){return"symbol"==typeof e||M(e)&&E(e)==S},D=NaN,O=/^\s+|\s+$/g,P=/^[-+]0x[0-9a-f]+$/i,I=/^0b[01]+$/i,k=/^0o[0-7]+$/i,L=parseInt,A=function(e){if("number"==typeof e)return e;if(j(e))return D;if(u(e)){var t="function"==typeof e.valueOf?e.valueOf():e;e=u(t)?t+"":t}if("string"!=typeof e)return 0===e?e:+e;e=e.replace(O,"");var n=I.test(e);return n||k.test(e)?L(e.slice(2),n?2:8):P.test(e)?D:+e},Y="Expected a function",_=Math.max,W=Math.min,X=function(e,t,n){function o(t){var n=d,o=v;return d=v=void 0,y=t,m=e.apply(o,n)}function i(e){return y=e,h=setTimeout(l,t),g?o(e):m}function r(e){var n=e-y,o=t-(e-w);return b?W(o,p-n):o}function a(e){var n=e-w,o=e-y;return void 0===w||n>=t||n<0||b&&o>=p}function l(){var e=s();if(a(e))return c(e);h=setTimeout(l,r(e))}function c(e){return h=void 0,x&&d?o(e):(d=v=void 0,m)}function f(){var e=s(),n=a(e);if(d=arguments,v=this,w=e,n){if(void 0===h)return i(w);if(b)return h=setTimeout(l,t),o(w)}return void 0===h&&(h=setTimeout(l,t)),m}var d,v,p,m,h,w,y=0,g=!1,b=!1,x=!0;if("function"!=typeof e)throw new TypeError(Y);return t=A(t)||0,u(n)&&(g=!!n.leading,p=(b="maxWait"in n)?_(A(n.maxWait)||0,t):p,x="trailing"in n?!!n.trailing:x),f.cancel=function(){void 0!==h&&clearTimeout(h),y=0,d=w=v=h=void 0},f.flush=function(){return void 0===h?m:c(s())},f},N="Expected a function",$=function(e,t,n){var o=!0,i=!0;if("function"!=typeof e)throw new TypeError(N);return u(n)&&(o="leading"in n?!!n.leading:o,i="trailing"in n?!!n.trailing:i),X(e,t,{leading:o,maxWait:t,trailing:i})},z=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})),C=e,F=e,U=function(e,t,o){n(e,r,t,o),"DOMMouseScroll"==r&&n(e,"MozMousePixelScroll",t,o)},V="";!function(e,t){e&&e.addEventListener?(o="addEventListener",i="removeEventListener"):(o="attachEvent",i="detachEvent",V="on"),r=t?"onwheel"in t.createElement("div")?"wheel":void 0!==t.onmousewheel?"mousewheel":"DOMMouseScroll":"wheel"}("undefined"!=typeof window&&window,"undefined"!=typeof document&&document),C.addWheelListener=F,C.removeWheelListener=U;!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 Z=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},q=!z(),B=function(){function e(){t=null,i=0}var t,n,o,i;return e(),function(){return n=window.scrollY,null!=t&&(i=n-t),t=n,clearTimeout(o),o=setTimeout(e,50),i}}();return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=Z({},{debug:!1,maxDisplays:99999,eventThrottle:200,showAfterInactiveSecondsDesktop:60,showAfterInactiveSecondsMobile:40,showAgainAfterSeconds:10,onExitIntent:function(){}},e),n=!1,o=function(){for(var e=arguments.length,n=Array(e),o=0;o<e;o++)n[o]=arguments[o];if(t.debug){var i;(i=console).log.apply(i,["[exit-intent-mobile]"].concat(n))}},i=0,r=$(function(){!n&&i<t.maxDisplays&&(o("display onExitIntent",++i),t.onExitIntent(),i>=t.maxDisplays&&d())},1e3*t.showAgainAfterSeconds,{trailing:!1}),u=void 0;q&&(o("register mouseleave for desktop"),u=document.body.addEventListener("mouseout",$(function(e){e instanceof MouseEvent&&e.clientY<50&&null==e.relatedTarget&&"select"!==e.target.nodeName.toLowerCase()&&(o("mouseout"),r())},t.eventThrottle),!1));var a=q?t.showAfterInactiveSecondsDesktop:t.showAfterInactiveSecondsMobile;o("timeoutOnDevice",a);var l=void 0,c=function(){void 0!==a?(void 0!==l&&(o("clearTimeout()"),window.clearTimeout(l)),l=window.setTimeout(function(){o("display after timeout"),r()},1e3*a)):o("display disabled")};c();var f=[],s=function(e,n){o("registering event for restartTimer",e,n);var i=$(function(e){o("throttled listener",e),c()},t.eventThrottle),u=null;u="scroll"===e?function(e){B()<-200&&(o("scroll"),r()),i(e)}:"mousemove"===e?function(e){e.clientY<50&&(o("intent-to-leave"),r()),i(e)}:i;var a=n.addEventListener(e,u,!1);return f.push({event:e,listener:a,target:n}),a};q&&(s("scroll",window),s("mousemove",window),F(window,function(e){$(function(e){o("throttled wheel listener",e),c()},t.eventThrottle)})),z&&(s("touchstart",document.body),s("touchend",document.body),s("touchmove",document.body),s("scroll",window));var d=function(){o("removeEvents",i),n=!0,u&&(document.body.removeEventListener("mouseleave",u),U(window,function(){o("removeWheelListener")})),f.forEach(function(e){var t=e.event,n=e.listener;e.target.removeEventListener(t,n)})};return d}});