Skip to content

Instantly share code, notes, and snippets.

View alwinchan's full-sized avatar

Alwin Chan alwinchan

View GitHub Profile
@alwinchan
alwinchan / error-detection.js
Last active March 23, 2024 10:26
Use html2canvas to capture a screenshot of the page upon any errors in the console via window.onerror event and send it to Slack as alert/notification. Image are stored with imgbb.com
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//html2canvas.hertzen.com/dist/html2canvas.min.js';
document.head.appendChild(script);
window.onerror = async function () {
const { 0: errorMessage, 1: fileName, 2: lineNumber, 3: columnNumber, 4: errorObj } = arguments;
if (typeof html2canvas === 'function') {
html2canvas(document.body, { scale: 1.0 }).then(async (canvas) => {
const base64image = canvas.toDataURL('image/png');
let imageData = base64image.split('base64,');