Skip to content

Instantly share code, notes, and snippets.

@Lwdthe1
Last active February 8, 2020 01:23
Show Gist options
  • Save Lwdthe1/f0f637970fa333596f79adf2f900e8b4 to your computer and use it in GitHub Desktop.
Save Lwdthe1/f0f637970fa333596f79adf2f900e8b4 to your computer and use it in GitHub Desktop.
A helper function for teachers to console log to the HTML document.
(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>`
}
}())
@Lwdthe1
Copy link
Author

Lwdthe1 commented Feb 4, 2020

Here's an example:

console.document('<h1>Hi</h1>','<br>','<i>byex</i>')

@Lwdthe1
Copy link
Author

Lwdthe1 commented Feb 8, 2020

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