rrweb
Version:
record and replay the web
3 lines (2 loc) • 13.8 kB
JavaScript
var NodeType,__assign=function(){return(__assign=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};!function(e){e[e.Document=0]="Document",e[e.DocumentType=1]="DocumentType",e[e.Element=2]="Element",e[e.Text=3]="Text",e[e.CDATA=4]="CDATA",e[e.Comment=5]="Comment"}(NodeType||(NodeType={}));var _id=1;function genId(){return _id++}function resetId(){_id=1}function getCssRulesString(e){try{var t=e.rules||e.cssRules;return t?Array.from(t).reduce(function(e,t){return e+t.cssText},""):null}catch(e){return null}}function extractOrigin(e){return(e.indexOf("//")>-1?e.split("/").slice(0,3).join("/"):e.split("/")[0]).split("?")[0]}var URL_IN_CSS_REF=/url\((?:'([^']*)'|"([^"]*)"|([^)]*))\)/gm,RELATIVE_PATH=/^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/,DATA_URI=/^(data:)([\w\/\+]+);(charset=[\w-]+|base64).*,(.*)/gi;function absoluteToStylesheet(e,t){return e.replace(URL_IN_CSS_REF,function(e,n,r,o){var i=n||r||o;if(!i)return e;if(!RELATIVE_PATH.test(i))return"url('"+i+"')";if(DATA_URI.test(i))return"url("+i+")";if("/"===i[0])return"url('"+(extractOrigin(t)+i)+"')";var a=t.split("/"),s=i.split("/");a.pop();for(var u=0,c=s;u<c.length;u++){var l=c[u];"."!==l&&(".."===l?a.pop():a.push(l))}return"url('"+a.join("/")+"')"})}function absoluteToDoc(e,t){var n=e.createElement("a");return n.href=t,n.href}function isSVGElement(e){return"svg"===e.tagName||e instanceof SVGElement}function serializeNode(e,t,n){switch(e.nodeType){case e.DOCUMENT_NODE:return{type:NodeType.Document,childNodes:[]};case e.DOCUMENT_TYPE_NODE:return{type:NodeType.DocumentType,name:e.name,publicId:e.publicId,systemId:e.systemId};case e.ELEMENT_NODE:var r=!1;"string"==typeof n?r=e.classList.contains(n):e.classList.forEach(function(e){n.test(e)&&(r=!0)});for(var o=e.tagName.toLowerCase(),i={},a=0,s=Array.from(e.attributes);a<s.length;a++){var u=s[a],c=u.name,l=u.value;i[c]="src"===c||"href"===c?absoluteToDoc(t,l):"style"===c?absoluteToStylesheet(l,location.href):l}if("link"===o){var d,p=Array.from(t.styleSheets).find(function(t){return t.href===e.href});(d=getCssRulesString(p))&&(delete i.rel,delete i.href,i._cssText=absoluteToStylesheet(d,p.href))}if("style"===o&&e.sheet&&!e.innerText.trim().length)(d=getCssRulesString(e.sheet))&&(i._cssText=absoluteToStylesheet(d,location.href));if("input"===o||"textarea"===o||"select"===o){l=e.value;"radio"!==i.type&&"checkbox"!==i.type&&l?i.value=l:e.checked&&(i.checked=e.checked)}if("option"===o){var m=e.parentElement;i.value===m.value&&(i.selected=e.selected)}if(r){var f=e.getBoundingClientRect(),v=f.width,h=f.height;i.rr_width=v+"px",i.rr_height=h+"px"}return{type:NodeType.Element,tagName:o,attributes:i,childNodes:[],isSVG:isSVGElement(e)||void 0,needBlock:r};case e.TEXT_NODE:var y=e.parentNode&&e.parentNode.tagName,E=e.textContent,g="STYLE"===y||void 0;return g&&E&&(E=absoluteToStylesheet(E,location.href)),"SCRIPT"===y&&(E="SCRIPT_PLACEHOLDER"),{type:NodeType.Text,textContent:E||"",isStyle:g};case e.CDATA_SECTION_NODE:return{type:NodeType.CDATA,textContent:""};case e.COMMENT_NODE:return{type:NodeType.Comment,textContent:e.textContent||""};default:return!1}}function serializeNodeWithId(e,t,n,r,o){void 0===o&&(o=!1);var i=serializeNode(e,t,r);if(!i)return console.warn(e,"not serialized"),null;var a=Object.assign(i,{id:genId()});e.__sn=a,n[a.id]=e;var s=!o;if(a.type===NodeType.Element&&(s=s&&!a.needBlock,delete a.needBlock),(a.type===NodeType.Document||a.type===NodeType.Element)&&s)for(var u=0,c=Array.from(e.childNodes);u<c.length;u++){var l=serializeNodeWithId(c[u],t,n,r);l&&a.childNodes.push(l)}return a}function snapshot(e,t){void 0===t&&(t="rr-block"),resetId();var n={};return[serializeNodeWithId(e,e,n,t),n]}function on(e,t,n){void 0===n&&(n=document);var r={capture:!0,passive:!0};return n.addEventListener(e,t,r),function(){return n.removeEventListener(e,t,r)}}var EventType,IncrementalSource,MouseInteractions,ReplayerEvents,mirror={map:{},getId:function(e){return e.__sn?e.__sn.id:-1},getNode:function(e){return mirror.map[e]||null},removeNodeFromMap:function(e){var t=e.__sn&&e.__sn.id;delete mirror.map[t],e.childNodes&&e.childNodes.forEach(function(e){return mirror.removeNodeFromMap(e)})},has:function(e){return mirror.map.hasOwnProperty(e)}};function throttle(e,t,n){void 0===n&&(n={});var r=null,o=0;return function(){var i=Date.now();o||!1!==n.leading||(o=i);var a=t-(i-o),s=this,u=arguments;a<=0||a>t?(r&&(window.clearTimeout(r),r=null),o=i,e.apply(s,u)):r||!1===n.trailing||(r=window.setTimeout(function(){o=!1===n.leading?0:Date.now(),r=null,e.apply(s,u)},a))}}function hookSetter(e,t,n){var r=Object.getOwnPropertyDescriptor(e,t);return Object.defineProperty(e,t,{set:function(e){var t=this;setTimeout(function(){n.set.call(t,e)},0),r&&r.set&&r.set.call(this,e)}}),function(){return hookSetter(e,t,r||{})}}function getWindowHeight(){return window.innerHeight||document.documentElement&&document.documentElement.clientHeight||document.body&&document.body.clientHeight}function getWindowWidth(){return window.innerWidth||document.documentElement&&document.documentElement.clientWidth||document.body&&document.body.clientWidth}function isBlocked(e,t){if(!e)return!1;if(e.nodeType===e.ELEMENT_NODE){var n=!1;return"string"==typeof t?n=e.classList.contains(t):e.classList.forEach(function(e){t.test(e)&&(n=!0)}),n||isBlocked(e.parentNode,t)}return isBlocked(e.parentNode,t)}function isAncestorRemoved(e){var t=mirror.getId(e);return!mirror.has(t)||(!e.parentNode||e.parentNode.nodeType!==e.DOCUMENT_NODE)&&(!e.parentNode||isAncestorRemoved(e.parentNode))}function deepDelete(e,t){e.delete(t),t.childNodes.forEach(function(t){return deepDelete(e,t)})}function isParentRemoved(e,t){var n=t.parentNode;if(!n)return!1;var r=mirror.getId(n);return!!e.some(function(e){return e.id===r})||isParentRemoved(e,n)}function isParentDropped(e,t){var n=t.parentNode;return!!n&&(!!e.has(n)||isParentDropped(e,n))}function initMutationObserver(e,t){var n=new MutationObserver(function(n){var r=[],o=[],i=[],a=[],s=new Set,u=new Set,c=function(e){isBlocked(e,t)||(s.add(e),u.delete(e),e.childNodes.forEach(function(e){return c(e)}))};n.forEach(function(e){var n=e.type,a=e.target,l=e.oldValue,d=e.addedNodes,p=e.removedNodes,m=e.attributeName;switch(n){case"characterData":var f=a.textContent;isBlocked(a,t)||f===l||r.push({value:f,node:a});break;case"attributes":f=a.getAttribute(m);if(isBlocked(a,t)||f===l)return;var v=o.find(function(e){return e.node===a});v||(v={node:a,attributes:{}},o.push(v)),v.attributes[m]=f;break;case"childList":d.forEach(function(e){return c(e)}),p.forEach(function(e){var n=mirror.getId(e),r=mirror.getId(a);isBlocked(e,t)||(s.has(e)?(deepDelete(s,e),u.add(e)):s.has(a)&&-1===n||isAncestorRemoved(a)||i.push({parentId:r,id:n}),mirror.removeNodeFromMap(e))})}}),Array.from(s).forEach(function(e){isParentDropped(u,e)||isParentRemoved(i,e)?u.add(e):a.push({parentId:mirror.getId(e.parentNode),previousId:e.previousSibling?mirror.getId(e.previousSibling):e.previousSibling,nextId:e.nextSibling?mirror.getId(e.nextSibling):e.nextSibling,node:serializeNodeWithId(e,document,mirror.map,t,!0)})});var l={texts:r.map(function(e){return{id:mirror.getId(e.node),value:e.value}}).filter(function(e){return mirror.has(e.id)}),attributes:o.map(function(e){return{id:mirror.getId(e.node),attributes:e.attributes}}).filter(function(e){return mirror.has(e.id)}),removes:i,adds:a};(l.texts.length||l.attributes.length||l.removes.length||l.adds.length)&&e(l)});return n.observe(document,{attributes:!0,attributeOldValue:!0,characterData:!0,characterDataOldValue:!0,childList:!0,subtree:!0}),n}function initMousemoveObserver(e){var t,n=[],r=throttle(function(){var r=Date.now()-t;e(n.map(function(e){return e.timeOffset-=r,e})),n=[],t=null},500);return on("mousemove",throttle(function(e){var o=e.clientX,i=e.clientY,a=e.target;t||(t=Date.now()),n.push({x:o,y:i,id:mirror.getId(a),timeOffset:Date.now()-t}),r()},50,{trailing:!1}))}function initMouseInteractionObserver(e,t){var n=[];return Object.keys(MouseInteractions).filter(function(e){return Number.isNaN(Number(e))}).forEach(function(r){var o=r.toLowerCase(),i=function(n){return function(r){if(!isBlocked(r.target,t)){var o=mirror.getId(r.target),i=r.clientX,a=r.clientY;e({type:MouseInteractions[n],id:o,x:i,y:a})}}}(r);n.push(on(o,i))}),function(){n.forEach(function(e){return e()})}}function initScrollObserver(e,t){return on("scroll",throttle(function(n){if(n.target&&!isBlocked(n.target,t)){var r=mirror.getId(n.target);if(n.target===document){var o=document.scrollingElement||document.documentElement;e({id:r,x:o.scrollLeft,y:o.scrollTop})}else e({id:r,x:n.target.scrollLeft,y:n.target.scrollTop})}},100))}function initViewportResizeObserver(e){return on("resize",throttle(function(){var t=getWindowHeight(),n=getWindowWidth();e({width:Number(n),height:Number(t)})},200),window)}!function(e){e[e.DomContentLoaded=0]="DomContentLoaded",e[e.Load=1]="Load",e[e.FullSnapshot=2]="FullSnapshot",e[e.IncrementalSnapshot=3]="IncrementalSnapshot",e[e.Meta=4]="Meta"}(EventType||(EventType={})),function(e){e[e.Mutation=0]="Mutation",e[e.MouseMove=1]="MouseMove",e[e.MouseInteraction=2]="MouseInteraction",e[e.Scroll=3]="Scroll",e[e.ViewportResize=4]="ViewportResize",e[e.Input=5]="Input"}(IncrementalSource||(IncrementalSource={})),function(e){e[e.MouseUp=0]="MouseUp",e[e.MouseDown=1]="MouseDown",e[e.Click=2]="Click",e[e.ContextMenu=3]="ContextMenu",e[e.DblClick=4]="DblClick",e[e.Focus=5]="Focus",e[e.Blur=6]="Blur",e[e.TouchStart=7]="TouchStart",e[e.TouchMove=8]="TouchMove",e[e.TouchEnd=9]="TouchEnd"}(MouseInteractions||(MouseInteractions={})),function(e){e.Start="start",e.Pause="pause",e.Resume="resume",e.Resize="resize",e.Finish="finish",e.FullsnapshotRebuilded="fullsnapshot-rebuilded",e.LoadStylesheetStart="load-stylesheet-start",e.LoadStylesheetEnd="load-stylesheet-end",e.SkipStart="skip-start",e.SkipEnd="skip-end",e.MouseInteraction="mouse-interaction"}(ReplayerEvents||(ReplayerEvents={}));var INPUT_TAGS=["INPUT","TEXTAREA","SELECT"],lastInputValueMap=new WeakMap;function initInputObserver(e,t,n){function r(e){var r=e.target;if(r&&r.tagName&&!(INPUT_TAGS.indexOf(r.tagName)<0)&&!isBlocked(r,t)){var i=r.type;if("password"!==i&&!r.classList.contains(n)){var a=r.value,s=!1;"radio"!==i&&"checkbox"!==i||(s=r.checked),o(r,{text:a,isChecked:s});var u=r.name;"radio"===i&&u&&s&&document.querySelectorAll('input[type="radio"][name="'+u+'"]').forEach(function(e){e!==r&&o(e,{text:e.value,isChecked:!s})})}}}function o(t,n){var r=lastInputValueMap.get(t);if(!r||r.text!==n.text||r.isChecked!==n.isChecked){lastInputValueMap.set(t,n);var o=mirror.getId(t);e(__assign({},n,{id:o}))}}var i=["input","change"].map(function(e){return on(e,r)}),a=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,"value"),s=[[HTMLInputElement.prototype,"value"],[HTMLInputElement.prototype,"checked"],[HTMLSelectElement.prototype,"value"],[HTMLTextAreaElement.prototype,"value"]];return a&&a.set&&i.push.apply(i,s.map(function(e){return hookSetter(e[0],e[1],{set:function(){r({target:this})}})})),function(){i.forEach(function(e){return e()})}}function initObservers(e){var t=initMutationObserver(e.mutationCb,e.blockClass),n=initMousemoveObserver(e.mousemoveCb),r=initMouseInteractionObserver(e.mouseInteractionCb,e.blockClass),o=initScrollObserver(e.scrollCb,e.blockClass),i=initViewportResizeObserver(e.viewportResizeCb),a=initInputObserver(e.inputCb,e.blockClass,e.ignoreClass);return function(){t.disconnect(),n(),r(),o(),i(),a()}}function wrapEvent(e){return __assign({},e,{timestamp:Date.now()})}function record(e){void 0===e&&(e={});var t,n=e.emit,r=e.checkoutEveryNms,o=e.checkoutEveryNth,i=e.blockClass,a=void 0===i?"rr-block":i,s=e.ignoreClass,u=void 0===s?"rr-ignore":s;if(!n)throw new Error("emit function is required");var c=0,l=function(e,i){if(n(e,i),e.type===EventType.FullSnapshot)t=e,c=0;else if(e.type===EventType.IncrementalSnapshot){c++;var a=o&&c>=o,s=r&&e.timestamp-t.timestamp>r;(a||s)&&d(!0)}};function d(e){void 0===e&&(e=!1),l(wrapEvent({type:EventType.Meta,data:{href:window.location.href,width:getWindowWidth(),height:getWindowHeight()}}),e);var t=snapshot(document,a),n=t[0],r=t[1];if(!n)return console.warn("Failed to snapshot the document");mirror.map=r,l(wrapEvent({type:EventType.FullSnapshot,data:{node:n,initialOffset:{left:document.documentElement.scrollLeft,top:document.documentElement.scrollTop}}}))}try{var p=[];p.push(on("DOMContentLoaded",function(){l(wrapEvent({type:EventType.DomContentLoaded,data:{}}))}));var m=function(){d(),p.push(initObservers({mutationCb:function(e){return l(wrapEvent({type:EventType.IncrementalSnapshot,data:__assign({source:IncrementalSource.Mutation},e)}))},mousemoveCb:function(e){return l(wrapEvent({type:EventType.IncrementalSnapshot,data:{source:IncrementalSource.MouseMove,positions:e}}))},mouseInteractionCb:function(e){return l(wrapEvent({type:EventType.IncrementalSnapshot,data:__assign({source:IncrementalSource.MouseInteraction},e)}))},scrollCb:function(e){return l(wrapEvent({type:EventType.IncrementalSnapshot,data:__assign({source:IncrementalSource.Scroll},e)}))},viewportResizeCb:function(e){return l(wrapEvent({type:EventType.IncrementalSnapshot,data:__assign({source:IncrementalSource.ViewportResize},e)}))},inputCb:function(e){return l(wrapEvent({type:EventType.IncrementalSnapshot,data:__assign({source:IncrementalSource.Input},e)}))},blockClass:a,ignoreClass:u}))};return"interactive"===document.readyState||"complete"===document.readyState?m():p.push(on("load",function(){l(wrapEvent({type:EventType.Load,data:{}})),m()},window)),function(){p.forEach(function(e){return e()})}}catch(e){console.warn(e)}}export default record;
//# sourceMappingURL=rrweb-record.min.js.map