Last active
February 8, 2020 01:23
-
-
Save Lwdthe1/f0f637970fa333596f79adf2f900e8b4 to your computer and use it in GitHub Desktop.
A helper function for teachers to console log to the HTML document.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
if (window.console.document) { | |
return | |
} | |
window.console.document = function() { | |
const args = Array.prototype.slice.call(arguments) | |
window.document.body.innerHTML = `<div id="__window_console_document__" style="padding:20px; font-size:20px; color: black">${args.join(' ')}</div>` | |
} | |
}()) |
Another version if you want to use it with console.log
instead.
(function () {
if (!window.console.document) {
window.console.document = function () {
const args = Array.prototype.slice.call(arguments)
window.document.body.innerHTML = `<div id="__window_console_document__" style="padding:20px; font-size:20px; color: black">${args.reduce((s, arg) => {
let argV
if (typeof arg === 'object') {
argV = JSON.stringify(arg)
} else {
argV = arg
}
return `${s} ${argV}`
}, '')}</div>`
}
}
const originalConsoleLog = console.log
console.log = function() {
originalConsoleLog.call(this, ...arguments)
console.document.call(this, ...arguments)
}
}())
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example: