@area2-ai/a2-node-keystroke-package
Version:
## Description
3 lines (2 loc) • 12.6 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("emoji-regex"))&&"object"==typeof t&&"default"in t?t.default:t,i=require("uuid");function n(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,r(n.key),n)}}function s(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function a(){return(a=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var n in i)({}).hasOwnProperty.call(i,n)&&(t[n]=i[n])}return t}).apply(null,arguments)}function r(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var n=i.call(t,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}var o={en:{country:"US",keyboardLayout:"QWERTY"},zh:{country:"CN",keyboardLayout:"QWERTY"},es:{country:"ES",keyboardLayout:"QWERTY (Spanish)"},fr:{country:"FR",keyboardLayout:"AZERTY"},ar:{country:"SA",keyboardLayout:"Arabic (Standard)"},pt:{country:"PT",keyboardLayout:"QWERTY (Portuguese)"},ru:{country:"RU",keyboardLayout:"ЙЦУКЕН"},ja:{country:"JP",keyboardLayout:"QWERTY (JIS)"},de:{country:"DE",keyboardLayout:"QWERTZ"},it:{country:"IT",keyboardLayout:"QWERTY (Italian)"}},c={1:/[12qwaszx]/i,2:/[34erdfcv]/i,3:/[56tyghbn]/i,4:/[78uijkm,]/i,5:/[90opl;./]/i,6:/[-=']/i},p={Backspace:/Backspace/i,Meta:/(Shift|Alt|Control|CapsLock)/i,Enter:/Enter/i,Space:/ /i},y={alphanumeric:/^[a-z0-9]$/i,punctuation:/[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]/,modifierKeys:/^(Shift|Alt|Control|CapsLock|Meta|ContextMenu|NumLock|ScrollLock)$/i,space:/ /,backspace:/Backspace/i,emoji:e(),enterKey:/Enter/i},h={1:"Winter",2:"Winter",3:"Spring",4:"Spring",5:"Spring",6:"Summer",7:"Summer",8:"Summer",9:"Fall",10:"Fall",11:"Fall",12:"Winter"},u={Backspace:8,Tab:9,Enter:13,Shift:16,Ctrl:17,Alt:18,Pause:19,CapsLock:20,Esc:27,Space:32,PageUp:33,PageDown:34,End:35,Home:36,LeftArrow:37,UpArrow:38,RightArrow:39,DownArrow:40,Insert:45,Delete:46,Meta:91,ContextMenu:93,NumLock:144,ScrollLock:145,SemiColon:186,EqualSign:187,Comma:188,Dash:189,Period:190,ForwardSlash:191,GraveAccent:192,OpenBracket:219,BackSlash:220,CloseBracket:221,SingleQuote:222},g=function(t){return e().test(t)},m=function(){return i.v4()},f=function(t){return["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][t.getDay()]},T=function(t,e){var i=t.toLowerCase().trim().split(/\s+/),n=e.toLowerCase().trim().split(/\s+/);return i.filter((function(t){return!n.includes(t)})).join(" ")},d=function(t,e){return void 0===e&&(e=!1),e?t.normalize("NFC").codePointAt(0).toString(16).toUpperCase():t.codePointAt(0).toString(16).toUpperCase()},l=function(t){var e=!!t.getAttribute("autocorrect");return"INPUT"!==t.tagName&&"TEXTAREA"!==t.tagName?"Text_ACOFF":e||t.spellcheck?"Text":"Text_ACOFF"},v=function(){var t=window.innerHeight,e=window.screen.height-t;if(!(e<=100))return{x:0,y:t,width:window.innerWidth,height:e}},D=function(){var t,e=navigator.language.split("-")[0];return(null==(t=o[e])?void 0:t.keyboardLayout)||"QWERTY"},k=function(t){var e=screen.width,i=screen.height;if("px"===t)return{width:e,height:i};var n=96*window.devicePixelRatio,s=screen.width/n*25.4,a=screen.height/n*25.4;return{width:Number(s.toFixed(2)),height:Number(a.toFixed(2))}},w=function(t){if(t<1||t>12)throw new Error("The month must be a number between 1 and 12");return h[t]},S=function(t){if(t<0||t>=24)throw new Error("The time must be a number between 0 and 23");return t>=6&&t<12?"Morning":t>=12&&t<18?"Afternoon":t>=18&&t<=23?"Evening":"Night"},A=function(t){return g(t)?900:1===t.length?t.charCodeAt(0):u[t]||0},x=function(t){for(var e=0,i=Object.entries(c);e<i.length;e++){var n=i[e],s=n[0];if(n[1].test(t))return parseInt(s)}return p.Backspace.test(t)||p.Meta.test(t)||p.Enter.test(t)||p.Space.test(t),0},b=function(t){for(var e=0,i=Object.entries(y);e<i.length;e++){var n=i[e],s=n[0];if(n[1].test(t))switch(s){case"alphanumeric":return"an";case"punctuation":return"pt";case"modifierKeys":return"md";case"space":return"sp";case"backspace":return"bk";case"emoji":return"em";case"enterKey":return"en"}}return"ukn"},E=function(){return{autocorrectLengths:[],autocorrectTimes:[],emojis:[],keyArea:[],keyTypes:[],language:(e=navigator.language.split("-"),i=e[0],n=e[1],s=Number(n),n&&isNaN(s)?e.join("-"):"not-found"===(n=(null==(t=o[i])?void 0:t.country)||"not-found")?"en-US":i+"-"+n),layout:D(),pasteLengths:[],pasteTimes:[],performance:[],predictionLengths:[],predictionTimes:[],pressTimes:[],qualityCheck:[],releaseTimes:[],screenSizeMm:k("mm"),screenSizePx:k("px"),season:w((new Date).getMonth()+1),sessionID:m(),startUnixTime:null,textField:"Text_ACOFF",time:S((new Date).getHours()),timeZone:(new Date).getTimezoneOffset()/-60,weekday:f(new Date)};var t,e,i,n,s},P=function(){return{keyArea:[],keyTypes:[],length:0,pressTimes:[],qualityCheck:[],releaseTimes:[],season:w((new Date).getMonth()+1),sessionID:m(),setting:"ah",source:"mech",startUnixTime:null,study:"demo",task:"free",textStructure:[],time:S((new Date).getHours()),timeZone:(new Date).getTimezoneOffset()/-60,weekday:f(new Date)}},L=function(){function t(){this.isTypingSessionActive=!1,this.typingData=E(),this.openKeyEntry=void 0,this.textWasPasted=!1,this.previousText=void 0,this.backspaceWasPressed=!1,this.performanceStart=0,this.lastKeyPressed=void 0,this.prevContentLength=0}var e=t.prototype;return e.processBeforeInput=function(t,e){this.prevContentLength=t.length,this.previousText=e},e.processPaste=function(t){if(this.textWasPasted=!0,this.isTypingSessionActive){var e=Date.now()/1e3-this.typingData.startUnixTime;this.typingData=a({},this.typingData,{pasteTimes:[].concat(this.typingData.pasteTimes,[Math.trunc(1e3*e)]),pasteLengths:[].concat(this.typingData.pasteLengths,[t.length])})}},e.processAutocorrection=function(t,e){if(this.textWasPasted)this.textWasPasted=!1;else if(this.backspaceWasPressed)this.backspaceWasPressed=!1;else if(!(g(t)||t.length<=1)){var i,n=Date.now()/1e3-this.typingData.startUnixTime;t===e&&(i=t.trim().toLowerCase()),0!==(i=T(e,this.previousText)).length&&(this.previousText=void 0,this.typingData=a({},this.typingData,{autocorrectLengths:[].concat(this.typingData.autocorrectLengths,[i.length]),autocorrectTimes:[].concat(this.typingData.autocorrectTimes,[Math.trunc(1e3*n)])}))}},e.processKeyInput=function(t){var e,i=t.length-this.prevContentLength,n=t.slice(-i);i<1&&(this.backspaceWasPressed=!0,n="Backspace"),this.lastKeyPressed=n,this.processAutocorrection(n,t),(null==(e=this.openKeyEntry)?void 0:e.keyValue)!==n&&(this.openKeyEntry={keyValue:n})},e.processKeydown=function(t){this.performanceStart=performance.now();var e,i=Date.now()/1e3;if(!this.isTypingSessionActive)return this.isTypingSessionActive=!0,e=Date.now()/1e3-i,void(this.typingData=a({},this.typingData,{textField:l(t),keyboardArea:v(),startUnixTime:i,pressTimes:[].concat(this.typingData.pressTimes,[Math.trunc(1e6*e)])}));e=Date.now()/1e3-this.typingData.startUnixTime,this.typingData=a({},this.typingData,{pressTimes:[].concat(this.typingData.pressTimes,[Math.trunc(1e6*e)])})},e.processKeyup=function(){var t,e=A(this.lastKeyPressed),i=x(this.lastKeyPressed),n=b(this.lastKeyPressed);900===e&&(t=d(this.lastKeyPressed,!0));var s=Date.now()/1e3-this.typingData.startUnixTime,r=performance.now();this.typingData=a({},this.typingData,{releaseTimes:[].concat(this.typingData.releaseTimes,[Math.trunc(1e6*s)]),performance:[].concat(this.typingData.performance,[r-this.performanceStart]),keyArea:[].concat(this.typingData.keyArea,[i]),keyTypes:[].concat(this.typingData.keyTypes,[n]),emojis:[].concat(this.typingData.emojis,t?[t]:[])}),this.openKeyEntry=void 0},e.endTypingSession=function(){return this.isTypingSessionActive=!1,this.typingData},e.resetTypingData=function(){this.typingData=E(),this.performanceStart=0},s(t,[{key:"getIsTypingSessionActive",get:function(){return this.isTypingSessionActive}}])}(),K=function(){function t(){this.isTypingSessionActive=!1,this.typingData=E(),this.openKeyEntry={},this.textWasPasted=!1,this.previousText=void 0,this.backspaceWasPressed=!1,this.performanceStart=0,this.prevContentLength=0}var e=t.prototype;return e.processPaste=function(t){if(this.textWasPasted=!0,this.isTypingSessionActive){var e=Date.now()/1e3-this.typingData.startUnixTime;this.typingData=a({},this.typingData,{pasteTimes:[].concat(this.typingData.pasteTimes,[Math.trunc(1e3*e)]),pasteLengths:[].concat(this.typingData.pasteLengths,[t.length])})}},e.processAutocorrection=function(t){if(this.previousText&&!t.startsWith(this.previousText)){var e=Date.now()/1e3-this.typingData.startUnixTime,i=T(t,this.previousText);this.typingData=a({},this.typingData,{autocorrectTimes:[].concat(this.typingData.autocorrectTimes,[Math.trunc(1e3*e)]),autocorrectLengths:[].concat(this.typingData.autocorrectLengths,[i.length])}),this.previousText=void 0}},e.processPrediction=function(t,e){if(this.textWasPasted)this.textWasPasted=!1;else if(this.backspaceWasPressed)this.backspaceWasPressed=!1;else{var i=t.slice(-(t.length-this.prevContentLength));if(!g(i)&&t.length-e.length!=1&&t!==e){var n=Date.now()/1e3-this.typingData.startUnixTime,s=T(t,e);this.typingData=a({},this.typingData,{predictionTimes:[].concat(this.typingData.predictionTimes,[Math.trunc(1e3*n)]),predictionLengths:[].concat(this.typingData.predictionLengths,[s.length])})}}},e.handleAutocorrectionTrigger=function(t,e){" "!==t&&"Enter"!==t||(this.previousText=e)},e.processKeydown=function(t,e){var i;this.performanceStart=performance.now();var n=Date.now()/1e3;if("Backspace"===t&&(this.backspaceWasPressed=!0),!this.isTypingSessionActive){var s=(new Date).getTimezoneOffset()/-60;this.isTypingSessionActive=!0,this.typingData=a({},this.typingData,{timeZone:s,textField:l(e),keyboardArea:v(),startUnixTime:n})}if(!(t in this.openKeyEntry)){var r=Date.now()/1e3-this.typingData.startUnixTime;this.handleAutocorrectionTrigger(t,e.value),this.openKeyEntry=a({},this.openKeyEntry,((i={})[t]={pressTime:Math.trunc(1e6*r),keyCode:A(t),keyArea:x(t),keyType:b(t)},i))}},e.processKeyup=function(t){var e;if(t in this.openKeyEntry){var i=this.openKeyEntry[t];900===i.keyCode&&(e=d(t));var n=Date.now()/1e3-this.typingData.startUnixTime,s=performance.now();this.typingData=a({},this.typingData,{pressTimes:[].concat(this.typingData.pressTimes,[i.pressTime]),releaseTimes:[].concat(this.typingData.releaseTimes,[Math.trunc(1e6*n)]),keyArea:[].concat(this.typingData.keyArea,[i.keyArea]),keyTypes:[].concat(this.typingData.keyTypes,[i.keyType]),emojis:[].concat(this.typingData.emojis,e?[e]:[]),performance:[].concat(this.typingData.performance,[s-this.performanceStart])}),delete this.openKeyEntry[t]}},e.endTypingSession=function(){return this.isTypingSessionActive=!1,this.typingData},e.resetTypingData=function(){this.openKeyEntry={},this.typingData=E(),this.performanceStart=0},s(t,[{key:"getIsTypingSessionActive",get:function(){return this.isTypingSessionActive}},{key:"setPrevContentLength",set:function(t){this.prevContentLength=t}}])}(),C=function(){function t(){this.isTypingSessionActive=!1,this.typingData=P(),this.openKeyEntries={}}var e=t.prototype;return e.processInputChange=function(t){this.typingData=a({},this.typingData,{length:t.length})},e.processKeydown=function(t){var e=Date.now()/1e3;if(this.isTypingSessionActive||(this.isTypingSessionActive=!0,this.typingData=a({},this.typingData,{startUnixTime:e})),!(t in this.openKeyEntries)){var i=Date.now()/1e3-this.typingData.startUnixTime;this.openKeyEntries[t]={pressTime:Math.trunc(1e6*i),keyCode:A(t),keyArea:x(t),keyType:b(t)}}},e.processKeyup=function(t){if(t in this.openKeyEntries){var e=this.openKeyEntries[t],i=Date.now()/1e3-this.typingData.startUnixTime;this.typingData=a({},this.typingData,{pressTimes:[].concat(this.typingData.pressTimes,[e.pressTime]),releaseTimes:[].concat(this.typingData.releaseTimes,[Math.trunc(1e6*i)]),keyArea:[].concat(this.typingData.keyArea,[e.keyArea]),keyTypes:[].concat(this.typingData.keyTypes,[e.keyType])}),delete this.openKeyEntries[t]}},e.endTypingSession=function(){return this.isTypingSessionActive=!1,this.typingData},e.resetTypingData=function(){this.typingData=P(),this.openKeyEntries={}},s(t,[{key:"getIsTypingSessionActive",get:function(){return this.isTypingSessionActive}}])}();exports.AndroidKeystrokeManager=L,exports.IosKeystrokeManager=K,exports.KeystrokeManager=C;
//# sourceMappingURL=a2-node-keystroke-package.cjs.production.min.js.map