@analytics/activity-utils
Version:
User activity listener utilities
3 lines (2 loc) • 4.85 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n||self).utilActivity={})}(this,function(n){function t(){return t=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var r in e)({}).hasOwnProperty.call(e,r)&&(n[r]=e[r])}return n},t.apply(null,arguments)}var e=function(){};"undefined"==typeof process||process;var r="undefined"!=typeof document;function o(n,t){return t.charAt(0)[n]()+t.slice(1)}"undefined"!=typeof Deno&&Deno,r&&"nodejs"===window.name||"undefined"!=typeof navigator&&void 0!==navigator.userAgent&&(navigator.userAgent.includes("Node.js")||navigator.userAgent.includes("jsdom"));var i=o.bind(null,"toUpperCase"),u=o.bind(null,"toLowerCase");function c(n,t){void 0===t&&(t=!0);var e=function(n){return d(n)?i("null"):"object"==typeof n?function(n){return f(n.constructor)?n.constructor.name:null}(n):Object.prototype.toString.call(n).slice(8,-1)}(n);return t?u(e):e}function a(n,t){return typeof t===n}var f=a.bind(null,"function"),l=a.bind(null,"string");function d(n){return null===n}function s(n,t){if("object"!=typeof t||d(t))return!1;if(t instanceof n)return!0;var e=c(new n(""));if(function(n){return n instanceof Error||l(n.message)&&n.constructor&&function(n){return"number"===c(n)&&!isNaN(n)}(n.constructor.stackTraceLimit)}(t))for(;t;){if(c(t)===e)return!0;t=Object.getPrototypeOf(t)}return!1}function v(n,t){var e=n instanceof Element||n instanceof HTMLDocument;return e&&t?function(n,t){return void 0===t&&(t=""),n&&n.nodeName===t.toUpperCase()}(n,t):e}function p(n){var t=[].slice.call(arguments,1);return function(){return n.apply(void 0,[].slice.call(arguments).concat(t))}}a.bind(null,"undefined"),a.bind(null,"boolean"),a.bind(null,"symbol"),s.bind(null,TypeError),s.bind(null,SyntaxError),p(v,"form"),p(v,"button"),p(v,"input"),p(v,"select");var m="EventListener";function y(n,t){if(l(n))return t?y(document.querySelectorAll(n)):(e=n).split(e.indexOf(",")>-1?",":" ").map(function(n){return n.trim()});var e;if(NodeList.prototype.isPrototypeOf(n)){for(var r=[],o=n.length>>>0;o--;)r[o]=n[o];return r}var i=function(n){return n?function(n){return"array"===c(n)}(n)?n:[n]:[]}(n);return t?i.map(function(n){return l(n)?y(n,!0):n}).flat():i}function b(n,t){var e;return function(){return n&&(e=n.apply(t||this,arguments),n=null),e}}var w=function(n,t,o,i){var u=o||e,c=i||!1;if(!r)return u;var a=y(t),f=y(n,!0);if(!f.length)throw new Error("noElements");if(!a.length)throw new Error("noEvent");var l=[];return function n(t){t&&(l=[]);for(var e=t?"add"+m:"remove"+m,r=0;r<f.length;r++){var o=f[r];l[r]=t?c&&c.once?b(u):u:l[r]||u;for(var i=0;i<a.length;i++)o[e]?o["on"+a[i]]=t?l[r]:null:o[e](a[i],l[r],c)}return n.bind(null,!t)}("Event")};function g(n,t){return r&&f(window[n])?(e=window[n],o=t,void 0===(i=window)&&(i=null),f(e)?function(){e.apply(i,arguments),o.apply(i,arguments)}:o):window[n]=t;var e,o,i}g.bind(null,"onerror"),g.bind(null,"onload");var h="undefined"==typeof window,E="hidden",j=["mousemove","mousedown","touchmove","touchstart","touchend","keydown"];function D(n,t){void 0===t&&(t={});var e=function(n,t){var e=this,r=!1;return function(o){r||(n.call(e,o),r=!0,setTimeout(function(){return r=!1},t))}}(n,t.throttle||1e4),r=[];function o(){var n=function(n){if(h)return!1;var t=h||E in document?E:["webkit","moz","ms","o"].reduce(function(n,t){var e=t+"Hidden";return!n&&e in document?e:n},null),e="".concat(t.replace(/[H|h]idden/,""),"visibilitychange"),r=function(){return n(Boolean(document[t]))},o=function(){return document.addEventListener(e,r)};return o(),function(){return document.removeEventListener(e,r),o}}(function(n){n||e({type:"tabVisible"})});return r=[n].concat(j.map(function(n){return w(document,n,e)})).concat(w(window,"load",e)).concat(w(window,"scroll",e,{capture:!0,passive:!0})),i}function i(){r.map(function(n){return n()})}return o(),function(){return i(),o}}function T(n){var t,e,r=n.onIdle,o=n.onWakeUp,i=n.onHeartbeat,u=n.timeout,c=void 0===u?1e4:u,a=n.throttle,f=void 0===a?2e3:a,l=!1,d=!1,s=new Date,v=function(){return clearTimeout(t)};function p(n){v(),i&&!l&&i(A(s,d),n),o&&l&&(l=!1,o(A(e,d),n),s=new Date),t=setTimeout(function(){l=!0,r&&(e=new Date,r(A(s,d),n))},c)}var m=D(p,{throttle:f});return p({type:"init"}),{disable:function(){d=!0,l=!1,v();var n=m();return function(){return d=!1,s=new Date,p({type:"load"}),n()}},getStatus:function(){return{isIdle:l,isDisabled:d,active:l?0:A(s,d),idle:l?A(e,d):0}}}}function A(n,t){return t?0:Math.round(((new Date).getTime()-n.getTime())/1e3)}n.onDomActivity=D,n.onIdle=function(n,e){return void 0===e&&(e={}),T(t({onIdle:n},e))},n.onUserActivity=T,n.onWakeUp=function(n,e){return void 0===e&&(e={}),T(t({onWakeUp:n},e))}});
//# sourceMappingURL=activity-utils.umd.js.map