Last active
August 13, 2023 01:16
-
-
Save jmhdez/6405474 to your computer and use it in GitHub Desktop.
Javascript console replacement
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(window, document) { | |
// Create the DOM structure to hold the console messages | |
var div = document.createElement("div"); | |
div.style.cssText = "position: absolute; " + | |
"top: 5px; left: 5px; right: 5px; bottom: 5px; " + | |
"padding: 10px; " + | |
"overflow-y: auto; " + | |
"display: none; " + | |
"background: rgba(0, 32, 0, 0.9); " + | |
"border: 3px solid #888; " + | |
"font: 14px Consolas,Monaco,Monospace; " + | |
"color: #ddd; " + | |
"z-index: 500"; | |
var ul = document.createElement("ul"); | |
ul.style.cssText = "padding: 0; list-style-type: none; margin: 0"; | |
div.appendChild(ul) | |
document.body.appendChild(div); | |
var toggleButton = document.createElement("button"); | |
toggleButton.innerText = "Console"; | |
toggleButton.style.cssText = "position: absolute; right: 10px; top: 10px; z-index: 501"; | |
toggleButton.addEventListener("click", function () { | |
div.style.display = div.style.display === "none" ? "block" : "none"; | |
}); | |
document.body.appendChild(toggleButton); | |
var clearButton = document.createElement("button"); | |
clearButton.innerText = "Clear"; | |
clearButton.style.cssText = "position: absolute; right: 10px; top: 30px; z-index: 501"; | |
clearButton.addEventListener("click", function() { | |
ul.innerHTML = ""; | |
}); | |
div.appendChild(clearButton); | |
function addMsg(msg) { | |
var li = document.createElement("li"); | |
li.innerText = msg; | |
ul.appendChild(li); | |
} | |
// Monkey-patch console object | |
var methods = ["log", "debug", "error", "info", "warn"]; | |
for (var i = 0; i < methods.length; i++) { | |
var method = methods[i]; | |
var original = window.console[method]; | |
window.console[method] = function(msg) { | |
addMsg(msg); | |
original.apply(window.console, arguments); | |
}; | |
} | |
})(window, document); |
ZulyMaressa
commented
Jan 20, 2021
<script src="https://gist.github.com/jmhdez/6405474/raw/8f4bdbb07b58eef2834ef72a66bdd56902e58419/console.js"></script>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment