console: hijack iframe console messages

This commit is contained in:
therewasaguy 2016-07-08 14:33:06 -04:00
parent e06c821923
commit 14ede1388b
1 changed files with 21 additions and 0 deletions

View File

@ -4,6 +4,8 @@ import ReactDOM from 'react-dom';
class PreviewFrame extends React.Component {
componentDidMount() {
this.hijackConsole();
if (this.props.isPlaying) {
this.renderFrameContents();
}
@ -33,6 +35,25 @@ class PreviewFrame extends React.Component {
doc.close();
}
hijackConsole() {
const iframeWindow = ReactDOM.findDOMNode(this).contentWindow;
const originalConsole = iframeWindow.console;
iframeWindow.console = {};
const methods = [
'debug', 'clear', 'error', 'info', 'log', 'warn'
];
methods.forEach((method) => {
iframeWindow.console[method] = (...theArgs) => {
originalConsole[method].apply(originalConsole, theArgs);
// TO DO: do something with the arguments
// window.alert(JSON.stringify(theArgs));
};
});
}
renderSketch() {
const doc = ReactDOM.findDOMNode(this).contentDocument;
this.clearPreview();