UNPKG

@xysfe/memento-core

Version:

record and replay the web

1,130 lines (1,114 loc) 137 kB
var mementoCoreRecord = (function () { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __values(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } var NodeType; (function (NodeType) { NodeType[NodeType["Document"] = 0] = "Document"; NodeType[NodeType["DocumentType"] = 1] = "DocumentType"; NodeType[NodeType["Element"] = 2] = "Element"; NodeType[NodeType["Text"] = 3] = "Text"; NodeType[NodeType["CDATA"] = 4] = "CDATA"; NodeType[NodeType["Comment"] = 5] = "Comment"; })(NodeType || (NodeType = {})); var _id = 1; var symbolAndNumberRegex = RegExp('[^a-z1-6-]'); function genId() { return _id++; } function getValidTagName(tagName) { var processedTagName = tagName.toLowerCase().trim(); if (symbolAndNumberRegex.test(processedTagName)) { return 'div'; } return processedTagName; } function getCssRulesString(s) { try { var rules = s.rules || s.cssRules; return rules ? Array.from(rules).reduce(function (prev, cur) { return prev + getCssRuleString(cur); }, '') : null; } catch (error) { return null; } } function getCssRuleString(rule) { return isCSSImportRule(rule) ? getCssRulesString(rule.styleSheet) || '' : rule.cssText; } function isCSSImportRule(rule) { return 'styleSheet' in rule; } function extractOrigin(url) { var origin; if (url.indexOf('//') > -1) { origin = url.split('/').slice(0, 3).join('/'); } else { origin = url.split('/')[0]; } origin = origin.split('?')[0]; return origin; } var URL_IN_CSS_REF = /url\((?:'([^']*)'|"([^"]*)"|([^)]*))\)/gm; var RELATIVE_PATH = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/; var DATA_URI = /^(data:)([\w\/\+\-]+);(charset=[\w-]+|base64).*,(.*)/i; function absoluteToStylesheet(cssText, href) { return (cssText || '').replace(URL_IN_CSS_REF, function (origin, path1, path2, path3) { var filePath = path1 || path2 || path3; if (!filePath) { return origin; } if (!RELATIVE_PATH.test(filePath)) { return "url('" + filePath + "')"; } if (DATA_URI.test(filePath)) { return "url(" + filePath + ")"; } if (filePath[0] === '/') { return "url('" + (extractOrigin(href) + filePath) + "')"; } var stack = href.split('/'); var parts = filePath.split('/'); stack.pop(); for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) { var part = parts_1[_i]; if (part === '.') { continue; } else if (part === '..') { stack.pop(); } else { stack.push(part); } } return "url('" + stack.join('/') + "')"; }); } function getAbsoluteSrcsetString(doc, attributeValue) { if (attributeValue.trim() === '') { return attributeValue; } var srcsetValues = attributeValue.split(','); var resultingSrcsetString = srcsetValues .map(function (srcItem) { var trimmedSrcItem = srcItem.trimLeft().trimRight(); var urlAndSize = trimmedSrcItem.split(' '); if (urlAndSize.length === 2) { var absUrl = absoluteToDoc(doc, urlAndSize[0]); return absUrl + " " + urlAndSize[1]; } else if (urlAndSize.length === 1) { var absUrl = absoluteToDoc(doc, urlAndSize[0]); return "" + absUrl; } return ''; }) .join(', '); return resultingSrcsetString; } function absoluteToDoc(doc, attributeValue) { if (!attributeValue || attributeValue.trim() === '') { return attributeValue; } var a = doc.createElement('a'); a.href = attributeValue; return a.href; } function isSVGElement(el) { return el.tagName === 'svg' || el instanceof SVGElement; } function transformAttribute(doc, name, value) { if (name === 'src' || (name === 'href' && value)) { return absoluteToDoc(doc, value); } else if (name === 'srcset' && value) { return getAbsoluteSrcsetString(doc, value); } else if (name === 'style' && value) { return absoluteToStylesheet(value, location.href); } else { return value; } } function serializeNode(n, doc, blockClass, inlineStylesheet, maskInputOptions, blockElements, recordCanvas) { if (maskInputOptions === void 0) { maskInputOptions = {}; } if (blockElements === void 0) { blockElements = []; } if (recordCanvas === void 0) { recordCanvas = false; } switch (n.nodeType) { case n.DOCUMENT_NODE: return { type: NodeType.Document, childNodes: [], }; case n.DOCUMENT_TYPE_NODE: return { type: NodeType.DocumentType, name: n.name, publicId: n.publicId, systemId: n.systemId, }; case n.ELEMENT_NODE: var needBlock_1 = false; var placeholder = ''; if (typeof blockClass === 'string') { needBlock_1 = n.classList.contains(blockClass); } else if (n.classList && !!(n.classList.forEach)) { n.classList.forEach(function (className) { if (blockClass.test(className)) { needBlock_1 = true; } }); } var tagName = getValidTagName(n.tagName); if (!needBlock_1) { for (var _i = 0, blockElements_1 = blockElements; _i < blockElements_1.length; _i++) { var blockElement = blockElements_1[_i]; if (tagName === blockElement.tagName) { needBlock_1 = true; if (blockElement.placeholder) { placeholder = blockElement.placeholder; } break; } } } var attributes_1 = {}; for (var _a = 0, _b = Array.from(n.attributes); _a < _b.length; _a++) { var _c = _b[_a], name = _c.name, value = _c.value; attributes_1[name] = transformAttribute(doc, name, value); var isStaticRes = (tagName === 'img' && name === 'src') || (tagName === 'link' && name === 'href'); var rel_1 = n.rel; var isPreload_1 = rel_1 === 'dns-prefetch' || rel_1 === 'preload'; if (isStaticRes && !isPreload_1 && !/^data:image/i.test(attributes_1[name])) { attributes_1["_memimage_" + name] = attributes_1[name]; } } var rel = n.rel; var isPreload = rel === 'dns-prefetch' || rel === 'preload' || rel === 'prefetch'; if (tagName === 'link' && !isPreload && inlineStylesheet) { var stylesheet = Array.from(doc.styleSheets).find(function (s) { return s.href === n.href; }); var cssText = getCssRulesString(stylesheet); if (cssText) { delete attributes_1.rel; delete attributes_1.href; if ('_memimage_href' in attributes_1) { delete attributes_1._memimage_href; } attributes_1._cssText = absoluteToStylesheet(cssText, stylesheet.href); } } if (tagName === 'style' && n.sheet && !(n.innerText || n.textContent || '').trim().length) { var cssText = getCssRulesString(n.sheet); if (cssText) { attributes_1._cssText = absoluteToStylesheet(cssText, location.href); } } if (tagName === 'input' || tagName === 'textarea' || tagName === 'select') { var value = n.value; if (attributes_1.type !== 'radio' && attributes_1.type !== 'checkbox' && attributes_1.type !== 'submit' && attributes_1.type !== 'button' && value) { attributes_1.value = maskInputOptions[attributes_1.type] || maskInputOptions[tagName] ? '*'.repeat(value.length) : value; } else if (n.checked) { attributes_1.checked = n.checked; } } if (tagName === 'option') { var selectValue = n.parentElement; if (attributes_1.value === selectValue.value) { attributes_1.selected = n.selected; } } if (tagName === 'canvas' && recordCanvas) { attributes_1.mem_dataURL = n.toDataURL(); } if (n.scrollLeft) { attributes_1.mem_scrollLeft = n.scrollLeft; } if (n.scrollTop) { attributes_1.mem_scrollTop = n.scrollTop; } if (needBlock_1) { var _d = n.getBoundingClientRect(), width = _d.width, height = _d.height; attributes_1.mem_width = width + "px"; attributes_1.mem_height = height + "px"; if (placeholder) { attributes_1.mem_placeholder = placeholder; } } return { type: NodeType.Element, tagName: tagName, attributes: attributes_1, childNodes: [], isSVG: isSVGElement(n) || undefined, needBlock: needBlock_1, }; case n.TEXT_NODE: var parentTagName = n.parentNode && n.parentNode.tagName; var textContent = n.textContent; var isStyle = parentTagName === 'STYLE' ? true : undefined; if (isStyle && textContent) { textContent = absoluteToStylesheet(textContent, location.href); } if (parentTagName === 'SCRIPT') { textContent = 'SCRIPT_PLACEHOLDER'; } return { type: NodeType.Text, textContent: textContent || '', isStyle: isStyle, }; case n.CDATA_SECTION_NODE: return { type: NodeType.CDATA, textContent: '', }; case n.COMMENT_NODE: return { type: NodeType.Comment, textContent: n.textContent || '', }; default: return false; } } function serializeNodeWithId(n, doc, map, blockClass, skipChild, inlineStylesheet, maskInputOptions, blockElements, recordCanvas) { if (skipChild === void 0) { skipChild = false; } if (inlineStylesheet === void 0) { inlineStylesheet = true; } if (blockElements === void 0) { blockElements = []; } if (recordCanvas === void 0) { recordCanvas = false; } var _serializedNode = serializeNode(n, doc, blockClass, inlineStylesheet, maskInputOptions, blockElements, recordCanvas); if (!_serializedNode) { console.warn(n, 'not serialized'); return null; } var id; if ('__sn' in n) { id = n.__sn.id; } else { id = genId(); } var serializedNode = Object.assign(_serializedNode, { id: id }); n.__sn = serializedNode; map[id] = n; var recordChild = !skipChild; if (serializedNode.type === NodeType.Element) { recordChild = recordChild && !serializedNode.needBlock; delete serializedNode.needBlock; } if ((serializedNode.type === NodeType.Document || serializedNode.type === NodeType.Element) && recordChild) { for (var _i = 0, _a = Array.from(n.childNodes); _i < _a.length; _i++) { var childN = _a[_i]; var serializedChildNode = serializeNodeWithId(childN, doc, map, blockClass, skipChild, inlineStylesheet, maskInputOptions, blockElements, recordCanvas); if (serializedChildNode) { serializedNode.childNodes.push(serializedChildNode); } } } return serializedNode; } function snapshot(n, blockClass, inlineStylesheet, maskAllInputsOrOptions, blockElements, recordCanvas) { if (blockClass === void 0) { blockClass = 'mem-block'; } if (inlineStylesheet === void 0) { inlineStylesheet = true; } if (blockElements === void 0) { blockElements = []; } if (recordCanvas === void 0) { recordCanvas = false; } var idNodeMap = {}; var maskInputOptions = maskAllInputsOrOptions === true ? { color: true, date: true, 'datetime-local': true, email: true, month: true, number: true, range: true, search: true, tel: true, text: true, time: true, url: true, week: true, textarea: true, select: true, } : maskAllInputsOrOptions === false ? {} : maskAllInputsOrOptions; return [ serializeNodeWithId(n, n, idNodeMap, blockClass, false, inlineStylesheet, maskInputOptions, blockElements, recordCanvas), idNodeMap, ]; } var EventType; (function (EventType) { EventType[EventType["DomContentLoaded"] = 0] = "DomContentLoaded"; EventType[EventType["Load"] = 1] = "Load"; EventType[EventType["FullSnapshot"] = 2] = "FullSnapshot"; EventType[EventType["IncrementalSnapshot"] = 3] = "IncrementalSnapshot"; EventType[EventType["Meta"] = 4] = "Meta"; EventType[EventType["PageChange"] = 5] = "PageChange"; EventType[EventType["Custom"] = 9] = "Custom"; EventType[EventType["Other"] = 99] = "Other"; })(EventType || (EventType = {})); var IncrementalSource; (function (IncrementalSource) { IncrementalSource[IncrementalSource["Mutation"] = 0] = "Mutation"; IncrementalSource[IncrementalSource["MouseMove"] = 1] = "MouseMove"; IncrementalSource[IncrementalSource["MouseInteraction"] = 2] = "MouseInteraction"; IncrementalSource[IncrementalSource["Scroll"] = 3] = "Scroll"; IncrementalSource[IncrementalSource["ViewportResize"] = 4] = "ViewportResize"; IncrementalSource[IncrementalSource["Input"] = 5] = "Input"; IncrementalSource[IncrementalSource["TouchMove"] = 6] = "TouchMove"; IncrementalSource[IncrementalSource["MediaInteraction"] = 7] = "MediaInteraction"; IncrementalSource[IncrementalSource["StyleSheetRule"] = 8] = "StyleSheetRule"; IncrementalSource[IncrementalSource["CanvasMutation"] = 9] = "CanvasMutation"; IncrementalSource[IncrementalSource["Drag"] = 10] = "Drag"; IncrementalSource[IncrementalSource["UserDefinedEvent"] = 11] = "UserDefinedEvent"; })(IncrementalSource || (IncrementalSource = {})); var UserDefinedEvent; (function (UserDefinedEvent) { UserDefinedEvent[UserDefinedEvent["LivePlay"] = 0] = "LivePlay"; })(UserDefinedEvent || (UserDefinedEvent = {})); var OtherSource; (function (OtherSource) { OtherSource[OtherSource["Console"] = 1] = "Console"; OtherSource[OtherSource["Network"] = 2] = "Network"; })(OtherSource || (OtherSource = {})); var MouseInteractions; (function (MouseInteractions) { MouseInteractions[MouseInteractions["MouseUp"] = 0] = "MouseUp"; MouseInteractions[MouseInteractions["MouseDown"] = 1] = "MouseDown"; MouseInteractions[MouseInteractions["Click"] = 2] = "Click"; MouseInteractions[MouseInteractions["ContextMenu"] = 3] = "ContextMenu"; MouseInteractions[MouseInteractions["DblClick"] = 4] = "DblClick"; MouseInteractions[MouseInteractions["Focus"] = 5] = "Focus"; MouseInteractions[MouseInteractions["Blur"] = 6] = "Blur"; MouseInteractions[MouseInteractions["TouchStart"] = 7] = "TouchStart"; MouseInteractions[MouseInteractions["TouchMove_Departed"] = 8] = "TouchMove_Departed"; MouseInteractions[MouseInteractions["TouchEnd"] = 9] = "TouchEnd"; })(MouseInteractions || (MouseInteractions = {})); var CanvasContext; (function (CanvasContext) { CanvasContext[CanvasContext["2D"] = 0] = "2D"; CanvasContext[CanvasContext["WebGL"] = 1] = "WebGL"; CanvasContext[CanvasContext["WebGL2"] = 2] = "WebGL2"; })(CanvasContext || (CanvasContext = {})); var MediaInteractions; (function (MediaInteractions) { MediaInteractions[MediaInteractions["Play"] = 0] = "Play"; MediaInteractions[MediaInteractions["Pause"] = 1] = "Pause"; })(MediaInteractions || (MediaInteractions = {})); var ReplayerEvents; (function (ReplayerEvents) { ReplayerEvents["Start"] = "start"; ReplayerEvents["Pause"] = "pause"; ReplayerEvents["Resume"] = "resume"; ReplayerEvents["Resize"] = "resize"; ReplayerEvents["Finish"] = "finish"; ReplayerEvents["FullsnapshotRebuilded"] = "fullsnapshot-rebuilded"; ReplayerEvents["LoadStylesheetStart"] = "load-stylesheet-start"; ReplayerEvents["LoadStylesheetEnd"] = "load-stylesheet-end"; ReplayerEvents["SkipStart"] = "skip-start"; ReplayerEvents["SkipEnd"] = "skip-end"; ReplayerEvents["MouseInteraction"] = "mouse-interaction"; ReplayerEvents["EventCast"] = "event-cast"; ReplayerEvents["CustomEvent"] = "custom-event"; ReplayerEvents["Flush"] = "flush"; ReplayerEvents["AddEvents"] = "add-events"; })(ReplayerEvents || (ReplayerEvents = {})); function on(type, fn, target) { if (target === void 0) { target = document; } var options = { capture: true, passive: true }; target.addEventListener(type, fn, options); return function () { return target.removeEventListener(type, fn, options); }; } var mirror = { map: {}, getId: function (n) { if (!n || !n.__sn) { return -1; } return n.__sn.id; }, getNode: function (id) { return mirror.map[id] || null; }, removeNodeFromMap: function (n) { var id = n.__sn && n.__sn.id; delete mirror.map[id]; if (n.childNodes) { n.childNodes.forEach(function (child) { return mirror.removeNodeFromMap(child); }); } }, has: function (id) { return mirror.map.hasOwnProperty(id); }, }; function throttle(func, wait, options) { if (options === void 0) { options = {}; } var timeout = null; var previous = 0; return function (arg) { var now = Date.now(); if (!previous && options.leading === false) { previous = now; } var remaining = wait - (now - previous); var context = this; var args = arguments; if (remaining <= 0 || remaining > wait) { if (timeout) { window.clearTimeout(timeout); timeout = null; } previous = now; func.apply(context, args); } else if (!timeout && options.trailing !== false) { timeout = window.setTimeout(function () { previous = options.leading === false ? 0 : Date.now(); timeout = null; func.apply(context, args); }, remaining); } }; } function hookSetter(target, key, d, isRevoked, win) { if (win === void 0) { win = window; } var original = win.Object.getOwnPropertyDescriptor(target, key); win.Object.defineProperty(target, key, isRevoked ? d : { set: function (value) { var _this = this; setTimeout(function () { d.set.call(_this, value); }, 0); if (original && original.set) { original.set.call(this, value); } }, }); return function () { return hookSetter(target, key, original || {}, true); }; } function patch(source, name, replacement) { if (!(name in source)) { return function () { }; } var original = source[name]; var wrapped = replacement(original); if (typeof wrapped === 'function') { try { wrapped.prototype = wrapped.prototype || {}; Object.defineProperties(wrapped, { __memento_original__: { enumerable: false, value: original, }, }); } catch (_a) { } } source[name] = wrapped; return function () { source[name] = original; }; } 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(node, blockClass, blockElements) { var e_1, _a; if (!node) { return false; } if (node.nodeType === node.ELEMENT_NODE) { var needBlock_1 = false; if (typeof blockClass === 'string') { needBlock_1 = node.classList.contains(blockClass); } else if (node.classList && !!(node.classList)) { node.classList.forEach(function (className) { if (blockClass.test(className)) { needBlock_1 = true; } }); } if (!needBlock_1 && node.tagName && blockElements) { try { for (var blockElements_1 = __values(blockElements), blockElements_1_1 = blockElements_1.next(); !blockElements_1_1.done; blockElements_1_1 = blockElements_1.next()) { var blockElement = blockElements_1_1.value; if (blockElement.tagName === node.tagName.toLowerCase()) { needBlock_1 = true; break; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (blockElements_1_1 && !blockElements_1_1.done && (_a = blockElements_1.return)) _a.call(blockElements_1); } finally { if (e_1) throw e_1.error; } } } return needBlock_1 || isBlocked(node.parentNode, blockClass, blockElements); } if (node.nodeType === node.TEXT_NODE) { return isBlocked(node.parentNode, blockClass, blockElements); } return isBlocked(node.parentNode, blockClass, blockElements); } function isAncestorRemoved(target) { var id = mirror.getId(target); if (!mirror.has(id)) { return true; } if (target.parentNode && target.parentNode.nodeType === target.DOCUMENT_NODE) { return false; } if (!target.parentNode) { return true; } return isAncestorRemoved(target.parentNode); } function isTouchEvent(event) { return Boolean(event.changedTouches); } function polyfill() { if ('NodeList' in window && !NodeList.prototype.forEach) { NodeList.prototype.forEach = Array.prototype .forEach; } } function isNumber(value) { return Object.prototype.toString.call(value) == '[object Number]'; } function isString(value) { return Object.prototype.toString.call(value) == '[object String]'; } function isArray(value) { return Object.prototype.toString.call(value) == '[object Array]'; } function isBoolean(value) { return Object.prototype.toString.call(value) == '[object Boolean]'; } function isUndefined(value) { return value === undefined; } function isNull(value) { return value === null; } function isSymbol(value) { return Object.prototype.toString.call(value) == '[object Symbol]'; } function isObject(value) { return (Object.prototype.toString.call(value) == '[object Object]' || (!isNumber(value) && !isString(value) && !isBoolean(value) && !isArray(value) && !isNull(value) && !isFunction(value) && !isUndefined(value) && !isSymbol(value))); } function isFunction(value) { return Object.prototype.toString.call(value) == '[object Function]'; } function isElement(value) { return (typeof HTMLElement === 'object' ? value instanceof HTMLElement : value && typeof value === "object" && value !== null && value.nodeType === 1 && typeof value.nodeName === "string"); } function isWindow(value) { var toString = Object.prototype.toString.call(value); return toString == '[object global]' || toString == '[object Window]' || toString == '[object DOMWindow]'; } function isPlainObject(obj) { var hasOwn = Object.prototype.hasOwnProperty; if (!obj || typeof obj !== 'object' || obj.nodeType || isWindow(obj)) { return false; } try { if (obj.constructor && !hasOwn.call(obj, 'constructor') && !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf')) { return false; } } catch (e) { return false; } var key; for (key in obj) { } return key === undefined || hasOwn.call(obj, key); } var moveKey = function (id, parentId) { return id + "@" + parentId; }; function isINode(n) { return '__sn' in n; } function isNodeInLinkedList(n) { return '__ln' in n; } var DoubleLinkedList = (function () { function DoubleLinkedList() { this.length = 0; this.head = null; } DoubleLinkedList.prototype.get = function (position) { if (position >= this.length) { throw new Error('Position outside of list range'); } var current = this.head; for (var index = 0; index < position; index++) { current = (current === null || current === void 0 ? void 0 : current.next) || null; } return current; }; DoubleLinkedList.prototype.addNode = function (n) { var node = { value: n, previous: null, next: null, }; n.__ln = node; if (n.previousSibling && isNodeInLinkedList(n.previousSibling)) { var current = n.previousSibling.__ln.next; node.next = current; node.previous = n.previousSibling.__ln; n.previousSibling.__ln.next = node; if (current) { current.previous = node; } } else if (n.nextSibling && isNodeInLinkedList(n.nextSibling) && n.nextSibling.__ln.previous) { var current = n.nextSibling.__ln.previous; node.previous = current; node.next = n.nextSibling.__ln; n.nextSibling.__ln.previous = node; if (current) { current.next = node; } } else { if (this.head) { this.head.previous = node; } node.next = this.head; this.head = node; } this.length++; }; DoubleLinkedList.prototype.removeNode = function (n) { var current = n.__ln; if (!this.head) { return; } if (!current.previous) { this.head = current.next; if (this.head) { this.head.previous = null; } } else { current.previous.next = current.next; if (current.next) { current.next.previous = current.previous; } } if (n.__ln) { delete n.__ln; } this.length--; }; return DoubleLinkedList; }()); var MutationBuffer = (function () { function MutationBuffer(cb, blockClass, inlineStylesheet, maskInputOptions, blockElements, asyncClass) { var _this = this; if (blockElements === void 0) { blockElements = []; } this.texts = []; this.attributes = []; this.removes = []; this.adds = []; this.movedMap = {}; this.addedSet = new Set(); this.movedSet = new Set(); this.droppedSet = new Set(); this.processMutations = function (mutations) { var e_1, _a, e_2, _b, e_3, _c, e_4, _d; _this.reset(); mutations.forEach(_this.processMutation); var texts = _this.texts; var attributes = _this.attributes; var removes = _this.removes; var adds = _this.adds; var addedSet = _this.addedSet; var movedSet = _this.movedSet; var droppedSet = _this.droppedSet; var changedSet = __spread(addedSet, movedSet); var asyncMutations = []; var addList = new DoubleLinkedList(); var getNextId = function (n) { var nextId = n.nextSibling && mirror.getId(n.nextSibling); if (nextId === -1 && isBlocked(n.nextSibling, _this.blockClass)) { nextId = null; } return nextId; }; var asyncPush = false; if (_this.asyncClass) { try { for (var changedSet_1 = __values(changedSet), changedSet_1_1 = changedSet_1.next(); !changedSet_1_1.done; changedSet_1_1 = changedSet_1.next()) { var item = changedSet_1_1.value; if (isBlocked(item.parentNode, _this.asyncClass)) { asyncPush = true; break; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (changedSet_1_1 && !changedSet_1_1.done && (_a = changedSet_1.return)) _a.call(changedSet_1); } finally { if (e_1) throw e_1.error; } } if (!asyncPush) { try { for (var attributes_1 = __values(attributes), attributes_1_1 = attributes_1.next(); !attributes_1_1.done; attributes_1_1 = attributes_1.next()) { var attribute = attributes_1_1.value; if (isBlocked(attribute.node.parentNode, _this.asyncClass)) { asyncPush = true; break; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (attributes_1_1 && !attributes_1_1.done && (_b = attributes_1.return)) _b.call(attributes_1); } finally { if (e_2) throw e_2.error; } } } } var _pushAdd = function (n) { if (!n.parentNode) { return; } var parentId = mirror.getId(n.parentNode); var nextId = getNextId(n); if (parentId === -1 || nextId === -1) { return addList.addNode(n); } adds.push({ parentId: parentId, nextId: nextId, node: serializeNodeWithId(n, document, mirror.map, _this.blockClass, true, _this.inlineStylesheet, _this.maskInputOptions, _this.blockElements), }); }; var pushAdd = function (n) { if (asyncPush) { asyncMutations.push(new Promise(function (resolve) { _this.requestIdleCallback(function () { _pushAdd(n); resolve(); }); })); } else { _pushAdd(n); } }; try { for (var movedSet_1 = __values(movedSet), movedSet_1_1 = movedSet_1.next(); !movedSet_1_1.done; movedSet_1_1 = movedSet_1.next()) { var n = movedSet_1_1.value; pushAdd(n); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (movedSet_1_1 && !movedSet_1_1.done && (_c = movedSet_1.return)) _c.call(movedSet_1); } finally { if (e_3) throw e_3.error; } } try { for (var addedSet_1 = __values(addedSet), addedSet_1_1 = addedSet_1.next(); !addedSet_1_1.done; addedSet_1_1 = addedSet_1.next()) { var n = addedSet_1_1.value; if (!isAncestorInSet(droppedSet, n) && !isParentRemoved(removes, n)) { pushAdd(n); } else if (isAncestorInSet(movedSet, n)) { pushAdd(n); } else { droppedSet.add(n); } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (addedSet_1_1 && !addedSet_1_1.done && (_d = addedSet_1.return)) _d.call(addedSet_1); } finally { if (e_4) throw e_4.error; } } var handleQueue = function () { var candidate = null; while (addList.length) { var node = null; if (candidate) { var parentId = mirror.getId(candidate.value.parentNode); var nextId = getNextId(candidate.value); if (parentId !== -1 && nextId !== -1) { node = candidate; } } if (!node) { for (var index = addList.length - 1; index >= 0; index--) { var _node = addList.get(index); var parentId = mirror.getId(_node.value.parentNode); var nextId = getNextId(_node.value); if (parentId !== -1 && nextId !== -1) { node = _node; break; } } } if (!node) { while (addList.head) { addList.removeNode(addList.head.value); } break; } candidate = node.previous; addList.removeNode(node.value); _pushAdd(node.value); } }; if (asyncPush) { var timestamp_1 = Date.now(); Promise.all(asyncMutations).then(function () { handleQueue(); _this.emit(texts, attributes, removes, adds, timestamp_1); }); } else { handleQueue(); _this.emit(texts, attributes, removes, adds); } }; this.emit = function (texts, attributes, removes, adds, t) { var payload = { texts: texts .map(function (text) { return ({ id: mirror.getId(text.node), value: text.value, }); }) .filter(function (text) { return mirror.has(text.id); }), attributes: attributes .map(function (attribute) { return ({ id: mirror.getId(attribute.node), attributes: attribute.attributes, }); }) .filter(function (attribute) { return mirror.has(attribute.id); }), removes: removes, adds: adds, timestamp: t, }; if (!payload.texts.length && !payload.attributes.length && !payload.removes.length && !payload.adds.length) { return; } _this.emissionCallback(payload); }; this.processMutation = function (m) { switch (m.type) { case 'characterData': { var value = m.target.textContent; if (!isBlocked(m.target, _this.blockClass, _this.blockElements) && value !== m.oldValue) { _this.texts.push({ value: value, node: m.target, }); } break; } case 'attributes': { var value = m.target.getAttribute(m.attributeName); if (isBlocked(m.target, _this.blockClass, _this.blockElements) || value === m.oldValue) { return; } var item = _this.attributes.find(function (a) { return a.node === m.target; }); if (!item) { item = { node: m.target, attributes: {}, }; _this.attributes.push(item); } item.attributes[m.attributeName] = transformAttribute(document, m.attributeName, value); var tagName = m.target.tagName.toLowerCase().trim(); var isStaticRes = (tagName === 'img' && m.attributeName === 'src') || (tagName === 'link' && m.attributeName === 'href'); var rel = m.target.rel; var isPreload = rel === 'dns-prefetch' || rel === 'preload'; if (isStaticRes && !isPreload && !/^data:image/i.test(item.attributes[m.attributeName])) { item.attributes["_memimage_" + m.attributeName] = item.attributes[m.attributeName]; } break; } case 'childList': { m.addedNodes.forEach(function (n) { return _this.genAdds(n, m.target); }); m.removedNodes.forEach(function (n) { var nodeId = mirror.getId(n); var parentId = mirror.getId(m.target); if (isBlocked(n, _this.blockClass, _this.blockElements) || isBlocked(m.target, _this.blockClass, _this.blockElements)) { return; } if (_this.addedSet.has(n)) { deepDelete(_this.addedSet, n); _this.droppedSet.add(n); } else if (_this.addedSet.has(m.target) && nodeId === -1) ;