Skip to content

Instantly share code, notes, and snippets.

@sloppylopez
Last active February 26, 2022 16:40
Show Gist options
  • Save sloppylopez/549eb4503fd596c92b617e6c3dda3eee to your computer and use it in GitHub Desktop.
Save sloppylopez/549eb4503fd596c92b617e6c3dda3eee to your computer and use it in GitHub Desktop.
SloppyLogger
// ==UserScript==
// @name Sloppy Logger
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @require https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js
// @require https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js
// @namespace http://tampermonkey.net/
// @version 0.1
// @description log uncaght window exceptions and write them in the document as bootstrap alert html elements
// @author Sloppy Lopez
// @match http*://*/
// @icon https://store-images.s-microsoft.com/image/apps.32031.13510798887630003.b4c5c861-c9de-4301-99ce-5af68bf21fd1.ba559483-bc2c-4eb9-a17e-c302009b2690?w=180&h=180&q=60
// @resource REMOTE_CSS https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css
// @grant GM_xmlhttpRequest
// @grant GM_getResourceText
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
const $ = window.jQuery;
console.defaultError = console.error.bind(console);
console.errors = [];
const errorsContainer = $("<div class=\"errorsContainer\">");
errorsContainer.appendTo($('body'))
console.error = function(){
// set the message to display: none to fade it in later.
const message = $("<div class=\"alert alert-danger\" style=\"display: none;\">");
// a close button
const close = $("<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times</button>");
message.append(close); // adding the close button to the message
message.append(arguments[0]); // adding the error response to the message
// add the message element to the body, fadein, wait 3secs, fadeout
message.prependTo(errorsContainer).fadeIn(300) //.delay(5000).fadeOut(500);
// default & console.error()
console.defaultError.apply(console, arguments);
// new & array data
console.errors.push(Array.from(arguments));
}
console.defaultWarn = console.warn.bind(console);
console.warns = [];
console.warn = function(){
// set the message to display: none to fade it in later.
const message = $("<div class=\"alert alert-warning\" style=\"display: none;\">");
// a close button
const close = $("<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times</button>");
message.append(close); // adding the close button to the message
message.append(arguments[0]); // adding the error response to the message
// add the message element to the body, fadein, wait 3secs, fadeout
message.prependTo(errorsContainer).fadeIn(300) //.delay(5000).fadeOut(500);
// default & console.error()
console.defaultWarn.apply(console, arguments);
// new & array data
console.warns.push(Array.from(arguments));
}
// Load remote CSS
// @see https://github.com/Tampermonkey/tampermonkey/issues/835
const myCss = GM_getResourceText("REMOTE_CSS");
GM_addStyle(myCss);
const errorMessageCss = ".alert {margin: 0px;}";
GM_addStyle(errorMessageCss);
const errorsContainerCss = ".errorsContainer {position: fixed; top: 0%; background-color: blue; width: 100%; z-index: 9999;}";
GM_addStyle(errorsContainerCss);
setTimeout(function() {
window.onerror = function (e, url, lineNumber) {
// alert(["window.onerror", errorMsg.error || errorMsg, url, lineNumber].join());
console.error('window.onerror ' + e.target.id);
return true;
};
window.addEventListener("unhandledrejection", function (e) {
//alert(["window: Unhandled Rejection", e.reason.message || e.error].join());
console.error('window: rejectionhandled ' + e.error || e);
});
window.addEventListener("rejectionhandled", function (e) {
//alert(["window: rejectionhandled", e.reason.message || e.error].join());
console.error('window: rejectionhandled ' + e.error || e);
});
window.addEventListener("error", function (e) {
//alert(["window: Error", e.reason.message || e.error].join());
console.error('window.onerror ' + e.error || e);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request)
);
});
$.error = function (message) {
//alert(["jQuery error", message || e.error].join());
console.error('jQuery error ' + message);
};
$.ajax({
url: window.location.href,
success: function( ) {
console.log( "Ajax Success for: " + window.location.href);
},
error: function( error ) {
console.error( "Ajax Error: (" + error.status + ") " + error.statusText);
}
});
}, 1000);
setTimeout(function() {
console.warn('Hello from Sloppy Logger');
}, 1000);
console.log(window.location.href);
(function(send) {
XMLHttpRequest.prototype.send = function(data) {
var _valuToAdd = $("input[name='valuToAdd']").val();
this.setRequestHeader('valueName', _valuToAdd);
send.call(this, data);
};
})(XMLHttpRequest.prototype.send);
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
//navigator.serviceWorker.register('sw.js').then(function(registration) {
// console.log('Service worker registered with scope: ', registration.scope);
//}, function(err) {
// console.log('ServiceWorker registration failed: ', err);
// });
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment