Skip to content

Instantly share code, notes, and snippets.

@haxiomic
Created July 27, 2021 09:50
Show Gist options
  • Save haxiomic/3055ecd614baee291a6bf1a0afb70fd9 to your computer and use it in GitHub Desktop.
Save haxiomic/3055ecd614baee291a6bf1a0afb70fd9 to your computer and use it in GitHub Desktop.
When you can't access the console, this script will display errors and warnings over the html (ensure this runs before your own code)
(() => {
let errorDisplayEl = document.createElement('div');
errorDisplayEl.classList.add('error-display');
errorDisplayEl.style.display = 'none';
errorDisplayEl.style.position = 'absolute';
errorDisplayEl.style.zIndex = '2000';
errorDisplayEl.style.backgroundColor = 'rgba(146, 0, 0, 0.6)';
errorDisplayEl.style.padding = '20px';
errorDisplayEl.style.fontFamily = 'monospace, sans-serif';
errorDisplayEl.style.color = '#fff5f5';
errorDisplayEl.style.backdropFilter = 'blur(10px)';
errorDisplayEl.style.width = '100%';
errorDisplayEl.style.overflow = 'scroll';
document.body.prepend(errorDisplayEl);
let closeButtonEl = document.createElement('button');
closeButtonEl.classList.add('close-button');
closeButtonEl.innerHTML = '×';
closeButtonEl.style.color = 'white';
closeButtonEl.style.position = 'absolute';
closeButtonEl.style.right = '5px';
closeButtonEl.style.top = '5px';
closeButtonEl.style.border = 'none';
closeButtonEl.style.width = '30px';
closeButtonEl.style.height = '30px';
closeButtonEl.style.fontSize = '30px';
closeButtonEl.style.lineHeight = '30px';
closeButtonEl.style.padding = '0';
closeButtonEl.style.fontWeight = '100';
closeButtonEl.style.background = 'none';
closeButtonEl.style.cursor = 'pointer';
closeButtonEl.style.webkitTouchCallout = 'none';
closeButtonEl.style.webkitUserSelect = 'none';
closeButtonEl.style.khtmlUserSelect = 'none';
closeButtonEl.style.mozUserSelect = 'none';
closeButtonEl.style.msUserSelect = 'none';
closeButtonEl.style.userSelect = 'none';
errorDisplayEl.appendChild(closeButtonEl);
let errorContentEl = document.createElement('div');
errorContentEl.classList.add('error-content');
errorDisplayEl.appendChild(errorContentEl);
closeButtonEl.addEventListener('click', () => {
errorDisplayEl.style.display = 'none';
// clear content
errorContentEl.innerHTML = '';
});
function showError(message) {
errorDisplayEl.style.display = 'block';
let errorEl = document.createElement('p');
errorEl.innerHTML = message;
errorContentEl.appendChild(errorEl);
}
// override console.error
let _console_error = window.console.error;
let _console_warn = window.console.warn;
window.console.error = function() {
let args = Array.prototype.slice.call(arguments);
showError('<b>Console.error:</b> ' + args.join(', '));
_console_error.apply(this, args);
}
window.console.warn = function() {
let args = Array.prototype.slice.call(arguments);
showError('<b>Console.warn:</b> ' + args.join(', '));
_console_warn.apply(this, args);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment