Skip to content

Instantly share code, notes, and snippets.

@latentflip
Created March 14, 2011 11:42
Show Gist options
  • Save latentflip/869030 to your computer and use it in GitHub Desktop.
Save latentflip/869030 to your computer and use it in GitHub Desktop.
Gives a growl like popup if a page has an error. Useful under development.
-if RAILS_ENV == 'development'
:javascript
$(function() {
var notify = function(icon, title, body) {
if (window.webkitNotifications.checkPermission() == 0) {
var popup = window.webkitNotifications.createNotification(
icon, title, body);
popup.show();
return true;
}
}
//Bind the popup to window.onerror
window.onerror = function(str, url, line) {
notify("", "JS Error", str+'\n'+url+':'+line);
};
});
@latentflip
Copy link
Author

There may be a better way to do this, but:

When writing JS I often make a silly little typo or something that I don't notice unless I:

  • notice that something seems broken
  • open Chrome's developer toolbar, which I inevitably closed last time to get it out of the way
  • hit the console button to see the list of errors

This little snippet uses Chrome's desktop notifications (unobtrusive pop-ups a bit like growl's) to give you an alert if there's an error on the page. Here I am only enabling it in dev. mode for obvious reasons.

I couldn't be bothered having it asking for & enabling permissions, so I have just enabled mine globally in chrome prefs.


Is there a better way of solving this problem?

@froots
Copy link

froots commented Mar 14, 2011

It would be pretty easy to package this up in an extension, and then you can set the target URLs for the extension to include localhost, 0.0.0.0, etc or any other places that you want to show it. That would avoid the need for changing your Chrome security prefs globally and having debug code in your Rails views.

@latentflip
Copy link
Author

Good idea, I might just do that. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment