gdy-rrweb-plugin
Version:
gdy web record & replay plugin
1 lines • 2.81 kB
JavaScript
"use strict";function _classCallCheck(e,i){if(!(e instanceof i))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,i){for(var t=0;t<i.length;t++){var s=i[t];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(e,s.key,s)}}function _createClass(e,i,t){return i&&_defineProperties(e.prototype,i),t&&_defineProperties(e,t),e}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var rrweb=require("rrweb"),dayjs=require("dayjs"),Hash=require("object-hash"),RRgdy=function(){function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"example",s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"",n=arguments.length>3?arguments[3]:void 0;_classCallCheck(this,e);var r={speed:1,root:document.body,loadTimeout:0,skipInactive:!1,showWarning:!0,showDebug:!1};dayjs().format("{YYYY} MM-DDTHH:mm:ss");this.events=[],this.uin=i,this.name=t,this.url=s,this.option=Object.assign({},r,n),this.startTime=0,this.endTime=0,this.session="",this.init=this.init.bind(this),this.record=this.record.bind(this),this.stop=this.stop.bind(this),this.setSession=this.setSession.bind(this),this.minimize=this.minimize.bind(this),this.restore=this.restore.bind(this),this.export=this.export.bind(this),this.replay=this.replay.bind(this),this.init()}return _createClass(e,[{key:"init",value:function(){this.setSession()}},{key:"record",value:function(){var e=this;rrweb.record({emit:function(i,t){t&&e.export(e.url);var s=Object.assign({},i,{uin:e.uin,name:e.name,session:e.session});e.events.push(s)},checkoutEveryNms:6e4})}},{key:"stop",value:function(){rrweb.record({})}},{key:"setSession",value:function(){var e=dayjs().format("{YYYY} MM-DDTHH:mm:ss");this.session=Hash({timestamp:e}),this.startTime=dayjs().unix(),this.events=[]}},{key:"minimize",value:function(e){return require("pako").deflate(JSON.stringify(e),{to:"string"})}},{key:"restore",value:function(e){var i=require("pako");return JSON.parse(i.inflate(e,{to:"string"}))}},{key:"export",value:function(e){this.startTime&&(this.endTime=dayjs().unix());var i=this.minimize(this.events),t={name:this.name,uin:this.uin,session:this.session,data:i,startTime:this.startTime,endTime:this.endTime};if(this.setSession(),navigator.sendBeacon&&i.length<65e3){var s=new Blob([JSON.stringify(t)],{type:"application/json"}),n=navigator.sendBeacon(e,s);n&&console.error(n)}else fetch(e,{method:"POST",body:JSON.stringify(t),cache:"no-cache",headers:new Headers({"Content-Type":"application/json"}),mode:"cors"}).then((function(e){return e.json()})).catch((function(e){console.error(e)}))}},{key:"replay",value:function(){new rrweb.Replayer(this.events,this.option).play()}}]),e}();exports.default=RRgdy;