diff --git a/README.md b/README.md index 686d25c..cc04571 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ You can [install](https://chrome.google.com/extensions/detail/lnalnbkkohdcnaapee ### Version 0.3.2 * Merged Pull Request #7 from Nick Doyle (Manifest v2 & updated styling) +* add desktop notifications ### Version 0.3.1 diff --git a/background.js b/background.js index 25504aa..ba4f9c6 100644 --- a/background.js +++ b/background.js @@ -1,6 +1,7 @@ // Prefs var refreshTime; var requestUrl; +var filterPattern; var auth; var desc; var green; @@ -9,27 +10,35 @@ var green; var abortTimer; var refreshTimer; var jobs; +var previousJobsByName; var xhr; +var showNotification; function init() { desc = localStorage.desc == 'true'; green = localStorage.green == 'true'; + showNotification = localStorage.showNotification == 'true'; + if (!localStorage.url) { updateStatus(-1); jobs = null; return; } else requestUrl = localStorage.url; - + refreshTime = localStorage.refreshTime || REFRESH_DEFAULT; refreshTime *= 60 * 1000; // minutes in ms - + if (typeof localStorage.username == 'string') { auth = window.btoa((localStorage.username || '') + ':' + (localStorage.password || '')); } else { auth = null; } - + + if (typeof localStorage.filterText === 'string') { + filterPattern = new RegExp(localStorage.filterText, localStorage.filterCaseSensitive ? '' : 'i'); + } + doRequest(); } @@ -40,7 +49,7 @@ function doRequest() { xhr = new XMLHttpRequest(); window.clearTimeout(abortTimer); abortTimer = window.setTimeout(xhr.abort, REQUEST_TIMEOUT); - + try { xhr.onreadystatechange = checkResponse; xhr.onerror = handleError; @@ -54,21 +63,83 @@ function doRequest() { } } + +// Displays a notification popup +function notify(message) { + try { + var notification = webkitNotifications.createNotification("./images/icon48.png", "Hudson", message); + var timeout = 15; + + notification.show(); + if( localStorage.showNotificationTime ) { + setTimeout(function () { notification.cancel(); }, localStorage.showNotificationTime * 1000); + } + + } catch (e) { + alert("error displaying notification"); + } +} + + function checkResponse() { if (xhr.readyState != 4) return; - + if (xhr.status == 200 && xhr.responseText) { var response = JSON.parse(xhr.responseText); var topStatus = -1; if (response.jobs) { - jobs = response.jobs; + jobs = response.jobs.filter(function (job) { + if (filterPattern) { + return !!filterPattern.test(job.name); + } else { + return true; + } + }); + + if (previousJobsByName) { + var jobsByName = {}; + var msgs = jobs + // find the jobs with status changes and reindex the jobs + .map(function (job) { + var previousJob = previousJobsByName[job.name]; + jobsByName[job.name] = job; + if (!previousJob) { + return job.name + ' was added'; + } else if (job.color !== previousJob.color) { + switch(job.color) { + case 'green_anime': + case 'blue_anime': + case 'green': + case 'blue': + return job.name + ' is now passing!'; + case 'red_anime': + case 'red': + return job.name + ' is now failing!'; + } + } + }) + // filter out empty messages + .filter(Boolean); + + if(msgs.length) { + notify(msgs.join('\n')); + } + previousJobsByName = jobsByName; + } else { + previousJobsByName = {}; + jobs.forEach(function (job) { + previousJobsByName[job.name] = job; + }); + } + if (localStorage.sorting == 'status') { jobs.sort(sortByStatus); } else { jobs.sort(sortByName); } - for (var i in response.jobs) - topStatus = Math.max(topStatus, STATUSES[response.jobs[i].color]); + jobs.forEach(function (job) { + topStatus = Math.max(topStatus, STATUSES[job.color]); + }); } handleSuccess(topStatus); return; @@ -97,7 +168,7 @@ function handleError(error) { function sortByName(a, b) { var o1 = a.name.toLowerCase(); var o2 = b.name.toLowerCase(); - + if (o1 < o2) return desc ? 1 : -1; else if (o2 < o1) return desc ? -1 : 1; else return 0; diff --git a/options.html b/options.html index 9de753b..9403a57 100644 --- a/options.html +++ b/options.html @@ -19,7 +19,6 @@ -