rrweb
Version:
record and replay the web
3 lines (2 loc) • 11.9 kB
JavaScript
var rrwebRecord=function(){"use strict";var e,t=function(){return(t=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"}(e||(e={}));var n=1;function r(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}}var o=/url\((?:'([^']*)'|"([^"]*)"|([^)]*))\)/gm,a=/^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/,i=/^(data:)([\w\/\+]+);(charset=[\w-]+|base64).*,(.*)/gi;function u(e,t){return e.replace(o,function(e,n,r,o){var u,c=n||r||o;if(!c)return e;if(!a.test(c))return"url('"+c+"')";if(i.test(c))return"url("+c+")";if("/"===c[0])return"url('"+(((u=t).indexOf("//")>-1?u.split("/").slice(0,3).join("/"):u.split("/")[0]).split("?")[0]+c)+"')";var s=t.split("/"),l=c.split("/");s.pop();for(var d=0,f=l;d<f.length;d++){var p=f[d];"."!==p&&(".."===p?s.pop():s.push(p))}return"url('"+s.join("/")+"')"})}function c(e,t){var n=e.createElement("a");return n.href=t,n.href}function s(t,o,a,i,l){void 0===l&&(l=!1);var d=function(t,n,o){switch(t.nodeType){case t.DOCUMENT_NODE:return{type:e.Document,childNodes:[]};case t.DOCUMENT_TYPE_NODE:return{type:e.DocumentType,name:t.name,publicId:t.publicId,systemId:t.systemId};case t.ELEMENT_NODE:var a=!1;"string"==typeof o?a=t.classList.contains(o):t.classList.forEach(function(e){o.test(e)&&(a=!0)});for(var i=t.tagName.toLowerCase(),s={},l=0,d=Array.from(t.attributes);l<d.length;l++){var f=d[l],p=f.name,m=f.value;s[p]="src"===p||"href"===p?c(n,m):"style"===p?u(m,location.href):m}if("link"===i){var h,v=Array.from(n.styleSheets).find(function(e){return e.href===t.href});(h=r(v))&&(delete s.rel,delete s.href,s._cssText=u(h,v.href))}if("style"===i&&t.sheet&&!t.innerText.trim().length&&(h=r(t.sheet))&&(s._cssText=u(h,location.href)),"input"!==i&&"textarea"!==i&&"select"!==i||(m=t.value,"radio"!==s.type&&"checkbox"!==s.type&&m?s.value=m:t.checked&&(s.checked=t.checked)),"option"===i){var y=t.parentElement;s.value===y.value&&(s.selected=t.selected)}if(a){var g=t.getBoundingClientRect(),E=g.width,b=g.height;s.rr_width=E+"px",s.rr_height=b+"px"}return{type:e.Element,tagName:i,attributes:s,childNodes:[],isSVG:(T=t,"svg"===T.tagName||T instanceof SVGElement||void 0),needBlock:a};case t.TEXT_NODE:var C=t.parentNode&&t.parentNode.tagName,N=t.textContent,w="STYLE"===C||void 0;return w&&N&&(N=u(N,location.href)),"SCRIPT"===C&&(N="SCRIPT_PLACEHOLDER"),{type:e.Text,textContent:N||"",isStyle:w};case t.CDATA_SECTION_NODE:return{type:e.CDATA,textContent:""};case t.COMMENT_NODE:return{type:e.Comment,textContent:t.textContent||""};default:return!1}var T}(t,o,i);if(!d)return console.warn(t,"not serialized"),null;var f=Object.assign(d,{id:n++});t.__sn=f,a[f.id]=t;var p=!l;if(f.type===e.Element&&(p=p&&!f.needBlock,delete f.needBlock),(f.type===e.Document||f.type===e.Element)&&p)for(var m=0,h=Array.from(t.childNodes);m<h.length;m++){var v=s(h[m],o,a,i);v&&f.childNodes.push(v)}return f}function l(e,t){void 0===t&&(t="rr-block"),n=1;var r={};return[s(e,e,r,t),r]}function d(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 f,p,m,h,v={map:{},getId:function(e){return e.__sn?e.__sn.id:-1},getNode:function(e){return v.map[e]||null},removeNodeFromMap:function(e){var t=e.__sn&&e.__sn.id;delete v.map[t],e.childNodes&&e.childNodes.forEach(function(e){return v.removeNodeFromMap(e)})},has:function(e){return v.map.hasOwnProperty(e)}};function y(e,t,n){void 0===n&&(n={});var r=null,o=0;return function(){var a=Date.now();o||!1!==n.leading||(o=a);var i=t-(a-o),u=this,c=arguments;i<=0||i>t?(r&&(window.clearTimeout(r),r=null),o=a,e.apply(u,c)):r||!1===n.trailing||(r=window.setTimeout(function(){o=!1===n.leading?0:Date.now(),r=null,e.apply(u,c)},i))}}function g(){return window.innerHeight||document.documentElement&&document.documentElement.clientHeight||document.body&&document.body.clientHeight}function E(){return window.innerWidth||document.documentElement&&document.documentElement.clientWidth||document.body&&document.body.clientWidth}function b(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||b(e.parentNode,t)}return b(e.parentNode,t)}function C(e,t){var n=new MutationObserver(function(n){var r=[],o=[],a=[],i=[],u=new Set,c=new Set,l=function(e){b(e,t)||(u.add(e),c.delete(e),e.childNodes.forEach(function(e){return l(e)}))};n.forEach(function(e){var n=e.type,i=e.target,s=e.oldValue,d=e.addedNodes,f=e.removedNodes,p=e.attributeName;switch(n){case"characterData":var m=i.textContent;b(i,t)||m===s||r.push({value:m,node:i});break;case"attributes":m=i.getAttribute(p);if(b(i,t)||m===s)return;var h=o.find(function(e){return e.node===i});h||(h={node:i,attributes:{}},o.push(h)),h.attributes[p]=m;break;case"childList":d.forEach(function(e){return l(e)}),f.forEach(function(e){var n=v.getId(e),r=v.getId(i);b(e,t)||(u.has(e)?(!function e(t,n){t.delete(n),n.childNodes.forEach(function(n){return e(t,n)})}(u,e),c.add(e)):u.has(i)&&-1===n||function e(t){var n=v.getId(t);return!v.has(n)||(!t.parentNode||t.parentNode.nodeType!==t.DOCUMENT_NODE)&&(!t.parentNode||e(t.parentNode))}(i)||a.push({parentId:r,id:n}),v.removeNodeFromMap(e))})}}),Array.from(u).forEach(function(e){(function e(t,n){var r=n.parentNode;return!!r&&(!!t.has(r)||e(t,r))})(c,e)||function e(t,n){var r=n.parentNode;if(!r)return!1;var o=v.getId(r);return!!t.some(function(e){return e.id===o})||e(t,r)}(a,e)?c.add(e):i.push({parentId:v.getId(e.parentNode),previousId:e.previousSibling?v.getId(e.previousSibling):e.previousSibling,nextId:e.nextSibling?v.getId(e.nextSibling):e.nextSibling,node:s(e,document,v.map,t,!0)})});var d={texts:r.map(function(e){return{id:v.getId(e.node),value:e.value}}).filter(function(e){return v.has(e.id)}),attributes:o.map(function(e){return{id:v.getId(e.node),attributes:e.attributes}}).filter(function(e){return v.has(e.id)}),removes:a,adds:i};(d.texts.length||d.attributes.length||d.removes.length||d.adds.length)&&e(d)});return n.observe(document,{attributes:!0,attributeOldValue:!0,characterData:!0,characterDataOldValue:!0,childList:!0,subtree:!0}),n}function N(e,t){var n=[];return Object.keys(m).filter(function(e){return Number.isNaN(Number(e))}).forEach(function(r){var o=r.toLowerCase(),a=function(n){return function(r){if(!b(r.target,t)){var o=v.getId(r.target),a=r.clientX,i=r.clientY;e({type:m[n],id:o,x:a,y:i})}}}(r);n.push(d(o,a))}),function(){n.forEach(function(e){return e()})}}!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"}(f||(f={})),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"}(p||(p={})),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"}(m||(m={})),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"}(h||(h={}));var w=["INPUT","TEXTAREA","SELECT"],T=new WeakMap;function I(e,n,r){function o(e){var t=e.target;if(t&&t.tagName&&!(w.indexOf(t.tagName)<0)&&!b(t,n)){var o=t.type;if("password"!==o&&!t.classList.contains(r)){var i=t.value,u=!1;"radio"!==o&&"checkbox"!==o||(u=t.checked),a(t,{text:i,isChecked:u});var c=t.name;"radio"===o&&c&&u&&document.querySelectorAll('input[type="radio"][name="'+c+'"]').forEach(function(e){e!==t&&a(e,{text:e.value,isChecked:!u})})}}}function a(n,r){var o=T.get(n);if(!o||o.text!==r.text||o.isChecked!==r.isChecked){T.set(n,r);var a=v.getId(n);e(t({},r,{id:a}))}}var i=["input","change"].map(function(e){return d(e,o)}),u=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,"value"),c=[[HTMLInputElement.prototype,"value"],[HTMLInputElement.prototype,"checked"],[HTMLSelectElement.prototype,"value"],[HTMLTextAreaElement.prototype,"value"]];return u&&u.set&&i.push.apply(i,c.map(function(e){return function e(t,n,r){var o=Object.getOwnPropertyDescriptor(t,n);return Object.defineProperty(t,n,{set:function(e){var t=this;setTimeout(function(){r.set.call(t,e)},0),o&&o.set&&o.set.call(this,e)}}),function(){return e(t,n,o||{})}}(e[0],e[1],{set:function(){o({target:this})}})})),function(){i.forEach(function(e){return e()})}}function S(e){var t,n,r,o,a=C(e.mutationCb,e.blockClass),i=(t=e.mousemoveCb,r=[],o=y(function(){var e=Date.now()-n;t(r.map(function(t){return t.timeOffset-=e,t})),r=[],n=null},500),d("mousemove",y(function(e){var t=e.clientX,a=e.clientY,i=e.target;n||(n=Date.now()),r.push({x:t,y:a,id:v.getId(i),timeOffset:Date.now()-n}),o()},50,{trailing:!1}))),u=N(e.mouseInteractionCb,e.blockClass),c=function(e,t){return d("scroll",y(function(n){if(n.target&&!b(n.target,t)){var r=v.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))}(e.scrollCb,e.blockClass),s=function(e){return d("resize",y(function(){var t=g(),n=E();e({width:Number(n),height:Number(t)})},200),window)}(e.viewportResizeCb),l=I(e.inputCb,e.blockClass,e.ignoreClass);return function(){a.disconnect(),i(),u(),c(),s(),l()}}function D(e){return t({},e,{timestamp:Date.now()})}return function(e){void 0===e&&(e={});var n,r=e.emit,o=e.checkoutEveryNms,a=e.checkoutEveryNth,i=e.blockClass,u=void 0===i?"rr-block":i,c=e.ignoreClass,s=void 0===c?"rr-ignore":c;if(!r)throw new Error("emit function is required");var m=0,h=function(e,t){if(r(e,t),e.type===f.FullSnapshot)n=e,m=0;else if(e.type===f.IncrementalSnapshot){m++;var i=a&&m>=a,u=o&&e.timestamp-n.timestamp>o;(i||u)&&y(!0)}};function y(e){void 0===e&&(e=!1),h(D({type:f.Meta,data:{href:window.location.href,width:E(),height:g()}}),e);var t=l(document,u),n=t[0],r=t[1];if(!n)return console.warn("Failed to snapshot the document");v.map=r,h(D({type:f.FullSnapshot,data:{node:n,initialOffset:{left:document.documentElement.scrollLeft,top:document.documentElement.scrollTop}}}))}try{var b=[];b.push(d("DOMContentLoaded",function(){h(D({type:f.DomContentLoaded,data:{}}))}));var C=function(){y(),b.push(S({mutationCb:function(e){return h(D({type:f.IncrementalSnapshot,data:t({source:p.Mutation},e)}))},mousemoveCb:function(e){return h(D({type:f.IncrementalSnapshot,data:{source:p.MouseMove,positions:e}}))},mouseInteractionCb:function(e){return h(D({type:f.IncrementalSnapshot,data:t({source:p.MouseInteraction},e)}))},scrollCb:function(e){return h(D({type:f.IncrementalSnapshot,data:t({source:p.Scroll},e)}))},viewportResizeCb:function(e){return h(D({type:f.IncrementalSnapshot,data:t({source:p.ViewportResize},e)}))},inputCb:function(e){return h(D({type:f.IncrementalSnapshot,data:t({source:p.Input},e)}))},blockClass:u,ignoreClass:s}))};return"interactive"===document.readyState||"complete"===document.readyState?C():b.push(d("load",function(){h(D({type:f.Load,data:{}})),C()},window)),function(){b.forEach(function(e){return e()})}}catch(e){console.warn(e)}}}();
//# sourceMappingURL=rrweb-record.min.js.map