mirror of
https://github.com/chinchang/web-maker.git
synced 2025-06-12 04:31:35 +02:00
screenlog: support passing event objs in postmessage. fixes #399
This commit is contained in:
@ -7,20 +7,66 @@ function sanitizeDomNode(node) {
|
|||||||
tagName: node.tagName,
|
tagName: node.tagName,
|
||||||
childNodes: [...node.childNodes].map(child => sanitizeDomNode(child)),
|
childNodes: [...node.childNodes].map(child => sanitizeDomNode(child)),
|
||||||
textContent: node.textContent
|
textContent: node.textContent
|
||||||
}
|
};
|
||||||
if(node.attributes) {
|
if (node.attributes) {
|
||||||
fakeNode.attributes = [...node.attributes].map(attribute => ({name:attribute.name, value:attribute.value}))
|
fakeNode.attributes = [...node.attributes].map(attribute => ({
|
||||||
|
name: attribute.name,
|
||||||
|
value: attribute.value
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
return fakeNode;
|
return fakeNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sanitizeEvent(event) {
|
||||||
|
const fakeEvent = {};
|
||||||
|
|
||||||
|
// Doing this still does't show class name in inspector
|
||||||
|
// fakeEvent.__proto__.constructor = event.constructor
|
||||||
|
|
||||||
|
function isSerializable(thing) {
|
||||||
|
try {
|
||||||
|
JSON.stringify(thing);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const prohibitedKeys = ['srcElement', 'sourceCapabilities'];
|
||||||
|
for (var key in event) {
|
||||||
|
if (typeof event[key] === 'function' || prohibitedKeys.includes(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
event[key] instanceof HTMLElement ||
|
||||||
|
event[key] instanceof HTMLHtmlElement
|
||||||
|
) {
|
||||||
|
fakeEvent[key] = sanitizeDomNode(event[key]);
|
||||||
|
continue;
|
||||||
|
} else if (event[key] instanceof Window) {
|
||||||
|
fakeEvent[key] = 'Window';
|
||||||
|
continue;
|
||||||
|
} else if (event[key] instanceof Document) {
|
||||||
|
fakeEvent[key] = 'Document';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isSerializable(event[key])) {
|
||||||
|
fakeEvent[key] = event[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fakeEvent;
|
||||||
|
}
|
||||||
function sendLog(...args) {
|
function sendLog(...args) {
|
||||||
const sanitizedArgs = [...args].map(arg => {
|
const sanitizedArgs = [...args].map(arg => {
|
||||||
if(arg && arg instanceof HTMLElement) {
|
if (arg && arg instanceof HTMLElement) {
|
||||||
return sanitizeDomNode(arg)
|
return sanitizeDomNode(arg);
|
||||||
|
} else if (arg && arg instanceof Event) {
|
||||||
|
return sanitizeEvent(arg);
|
||||||
}
|
}
|
||||||
return arg;
|
return arg;
|
||||||
})
|
});
|
||||||
mainWindow.postMessage({ logs: sanitizedArgs },"*");
|
mainWindow.postMessage({ logs: sanitizedArgs }, '*');
|
||||||
}
|
}
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@ -224,10 +270,10 @@ window._wmEvaluate = function _wmEvaluate(expr) {
|
|||||||
sendLog(e.stack || e.message);
|
sendLog(e.stack || e.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sendLog(result)
|
sendLog(result);
|
||||||
};
|
};
|
||||||
window.addEventListener('message', e => {
|
window.addEventListener('message', e => {
|
||||||
if(e.data && e.data.exprToEval) {
|
if (e.data && e.data.exprToEval) {
|
||||||
_wmEvaluate(e.data.exprToEval);
|
_wmEvaluate(e.data.exprToEval);
|
||||||
}
|
}
|
||||||
})
|
});
|
Reference in New Issue
Block a user