Skip to content

Instantly share code, notes, and snippets.

@andrelaszlo
Created April 26, 2012 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrelaszlo/2503946 to your computer and use it in GitHub Desktop.
Save andrelaszlo/2503946 to your computer and use it in GitHub Desktop.
Show desktop notification (chrome) when the duego.com pre-register page updates
// ==UserScript==
// @match http://duego.com/preregister*
// @require http://code.jquery.com/jquery-1.4.2.min.js
// ==/UserScript==
var load,execute,loadAndExecute;load=function(a,b,c){var d;d=document.createElement("script"),d.setAttribute("src",a),b!=null&&d.addEventListener("load",b),c!=null&&d.addEventListener("error",c),document.body.appendChild(d);return d},execute=function(a){var b,c;typeof a=="function"?b="("+a+")();":b=a,c=document.createElement("script"),c.textContent=b,document.body.appendChild(c);return c},loadAndExecute=function(a,b){return load(a,function(){return execute(b)})};
loadAndExecute("//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function() {
function Notifier() {};
// Returns "true" if this browser supports notifications.
Notifier.prototype.HasSupport = function() {
if (window.webkitNotifications) {
return true;
} else {
return false;
}
}
// Request permission for this page to send notifications. If allowed,
// calls function "cb" with true.
Notifier.prototype.RequestPermission = function(cb) {
if (window.webkitNotifications.checkPermission() != 0) {
window.webkitNotifications.requestPermission(function() {
if (cb) { cb(window.webkitNotifications.checkPermission() == 0); }
});
}
}
// Popup a notification with icon, title, and body. Returns false if
// permission was not granted.
Notifier.prototype.Notify = function(icon, title, body) {
if (window.webkitNotifications.checkPermission() == 0) {
var popup = window.webkitNotifications.createNotification(
icon, title, body);
popup.show();
return true;
}
return false;
}
var notifier = new Notifier();
notifier.RequestPermission();
var current = false;
if (!notifier.HasSupport()) {
console.log("No support for desktop notifications");
return;
}
$("<input type=\"button\" value=\"Show desktop notifications!\" id=\"req_perm\" />")
.hide()
.appendTo('body')
.css('position', 'absolute')
.css('left', '0px')
.css('top', '0px')
.css('height', '100%')
.css('width', '100%')
.css('z-index', '100')
.css('font-size', '5em')
.click(function() {
notifier.RequestPermission(function(allowed) {
if (allowed) {
notifier.Notify("http://static.duego.com/images/badges/duego-107x106.png?version=DEV", "Duego", "Notifications active");
}
});
$('#req_perm').hide();
});
if (window.webkitNotifications.checkPermission() != 0) {
$('#req_perm').show();
}
function updateCount() {
notifier.RequestPermission();
$.get("http://duego.com/preregister", function(data){
var matches = data.match("<strong>([0-9]*)</strong>");
if (matches.length > 1) {
var n = matches[1];
if (false === current) {
current = n;
return;
}
if (n < current) {
console.log("New number of signups left: " + n);
// show popup
if (!notifier.Notify("http://static.duego.com/images/badges/duego-107x106.png?version=DEV", "Duego", "Only " + n + " signups to go!")) {
console.log('No permission to show desktop notifications.');
$('#req_perm').show();
//notifier.RequestPermission();
}
current = n;
}
}
});
}
updateCount();
setInterval(updateCount, 60000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment