console: hijack iframe console messages

This commit is contained in:
therewasaguy 2016-07-08 14:33:06 -04:00
parent e06c821923
commit 14ede1388b

View file

@ -4,6 +4,8 @@ import ReactDOM from 'react-dom';
class PreviewFrame extends React.Component { class PreviewFrame extends React.Component {
componentDidMount() { componentDidMount() {
this.hijackConsole();
if (this.props.isPlaying) { if (this.props.isPlaying) {
this.renderFrameContents(); this.renderFrameContents();
} }
@ -33,6 +35,25 @@ class PreviewFrame extends React.Component {
doc.close(); 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() { renderSketch() {
const doc = ReactDOM.findDOMNode(this).contentDocument; const doc = ReactDOM.findDOMNode(this).contentDocument;
this.clearPreview(); this.clearPreview();