2019-01-03 18:34:01 -08:00
|
|
|
/**
|
|
|
|
|
* Serialize an object
|
|
|
|
|
* @param {Object} obj
|
|
|
|
|
* @return {string}
|
|
|
|
|
*/
|
|
|
|
|
function serialize(obj) {
|
|
|
|
|
if (obj instanceof Text) return '#text';
|
|
|
|
|
if (obj instanceof Element) return `<${obj.localName}>${obj.textContent}`;
|
|
|
|
|
if (obj === document) return 'document';
|
2020-03-14 16:25:25 +01:00
|
|
|
if (typeof obj == 'string') return obj;
|
2019-01-03 18:34:01 -08:00
|
|
|
return Object.prototype.toString.call(obj).replace(/(^\[object |\]$)/g, '');
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 18:55:58 -08:00
|
|
|
/** @type {string[]} */
|
|
|
|
|
let log = [];
|
2019-01-03 18:34:01 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Modify obj's original method to log calls and arguments on logger object
|
|
|
|
|
* @template T
|
|
|
|
|
* @param {T} obj
|
|
|
|
|
* @param {keyof T} method
|
|
|
|
|
*/
|
|
|
|
|
export function logCall(obj, method) {
|
|
|
|
|
let old = obj[method];
|
2023-03-21 14:16:56 -07:00
|
|
|
obj[method] = function (...args) {
|
2019-01-03 18:34:01 -08:00
|
|
|
let c = '';
|
2019-10-29 20:40:58 +01:00
|
|
|
for (let i = 0; i < args.length; i++) {
|
2019-01-03 18:34:01 -08:00
|
|
|
if (c) c += ', ';
|
2019-01-03 18:55:58 -08:00
|
|
|
c += serialize(args[i]);
|
2019-01-03 18:34:01 -08:00
|
|
|
}
|
2019-03-12 07:57:29 +01:00
|
|
|
|
2023-06-14 11:14:44 +02:00
|
|
|
let operation;
|
|
|
|
|
switch (method) {
|
|
|
|
|
case 'insertBefore': {
|
|
|
|
|
if (args[1] === null && args.length === 2) {
|
|
|
|
|
operation = `${serialize(this)}.appendChild(${serialize(args[0])})`;
|
|
|
|
|
} else {
|
2024-11-30 02:59:40 -06:00
|
|
|
operation = `${serialize(this)}.${String(method)}(${c})`;
|
2023-06-14 11:14:44 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
2024-11-30 02:59:40 -06:00
|
|
|
operation = `${serialize(this)}.${String(method)}(${c})`;
|
2023-06-14 11:14:44 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 18:55:58 -08:00
|
|
|
log.push(operation);
|
|
|
|
|
return old.apply(this, args);
|
2019-01-03 18:34:01 -08:00
|
|
|
};
|
2020-11-11 12:25:22 +01:00
|
|
|
|
|
|
|
|
return () => (obj[method] = old);
|
2019-01-03 18:34:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return log object
|
2019-01-03 18:55:58 -08:00
|
|
|
* @return {string[]} log
|
2019-01-03 18:34:01 -08:00
|
|
|
*/
|
|
|
|
|
export function getLog() {
|
|
|
|
|
return log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Clear log object */
|
|
|
|
|
export function clearLog() {
|
2019-01-03 18:55:58 -08:00
|
|
|
log = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getLogSummary() {
|
|
|
|
|
/** @type {{ [key: string]: number }} */
|
|
|
|
|
const summary = {};
|
|
|
|
|
|
|
|
|
|
for (let entry of log) {
|
|
|
|
|
summary[entry] = (summary[entry] || 0) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return summary;
|
2019-01-03 18:34:01 -08:00
|
|
|
}
|