diff --git a/README.md b/README.md index 073b5c2..388fb56 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ You can [install](https://chrome.google.com/extensions/detail/lnalnbkkohdcnaapee ## Changelog +### Version 0.3.2 +* add 2 minutes intervall (I am so impatient) +* add desktop notifications + ### Version 0.3.1 * _Feature:_ When sorting jobs by status, jobs with the same status are now ordered alphabetically diff --git a/background.html b/background.html index 4dc7157..67d49f7 100644 --- a/background.html +++ b/background.html @@ -29,10 +29,13 @@ var refreshTimer; var jobs; var xhr; + var previousResponse; + var showNotification; function init() { desc = localStorage.desc == 'true'; green = localStorage.green == 'true'; + showNotification = localStorage.showNotification == 'true'; if (!localStorage.url) { updateStatus(-1); jobs = null; @@ -72,12 +75,66 @@ handleError(e); } } + + function color2state(color) { + switch(color) { + case 'green_anime': + case 'blue_anime': + case 'green': + case 'blue': + return "OK"; + case 'red_anime': + case 'red': + return "FAILED"; + } + return color; + } + + // 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); + + if( previousResponse ) { + var previousJobs = previousResponse.jobs; + var previousStates = {}; + + for(i in previousJobs) { + job = previousJobs[i]; + previousStates[job.name] = job.color; + } + var message = ""; + for(i in response.jobs) { + job = response.jobs[i]; + if(color2state(job.color) != color2state(previousStates[job.name])) { + //message += "
" + job.name + ": " + color2state(previousStates[job.name]) + " -> " + color2state(job.color); + message += job.name + ": " + color2state(previousStates[job.name]) + " -> " + color2state(job.color) + "\n"; + } + } + if(message != "") { + notify(message) + } + } + + previousResponse = response; var topStatus = -1; if (response.jobs) { jobs = response.jobs; @@ -150,16 +207,29 @@ } } - function openTab() { - chrome.tabs.getAllInWindow(undefined, function(tabs) { - for (var i = 0, tab; tab = tabs[i]; i++) { - if (tab.url && tab.url.indexOf(localStorage.url) == 0) { - chrome.tabs.update(tab.id, {selected: true}); - return; - } - } - chrome.tabs.create({url: localStorage.url}); - }); + // This code was written by Tyler Akins and has been placed in the + // public domain. It would be nice if you left this header intact. + // Base64 code from Tyler Akins -- http://rumkin.com + function base64(input) { + var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + var output = ""; + var chr1, chr2, chr3, enc1, enc2, enc3, enc4; + var i = 0; + while (i < input.length) { + chr1 = input.charCodeAt(i++); + chr2 = input.charCodeAt(i++); + chr3 = input.charCodeAt(i++); + enc1 = chr1 >> 2; + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); + enc4 = chr3 & 63; + if (isNaN(chr2)) + enc3 = enc4 = 64; + else if (isNaN(chr3)) + enc4 = 64; + output += (keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4)); + } + return output; } window.onload = function() { @@ -168,4 +238,3 @@ - diff --git a/manifest.json b/manifest.json index 828b4a1..7cf7843 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "Hudson Monitor", - "version": "0.3.1", + "version": "0.3.2", "description": "This extension can monitor your Hudson CI server", "icons": { "16": "images/icon16.png", "32": "images/icon32.png", diff --git a/options.html b/options.html index ed97950..97310e5 100644 --- a/options.html +++ b/options.html @@ -23,6 +23,7 @@ var urlInput; var errorImage; var refreshDropdown; + var showNotificationTimeDropdown; var authCheckbox; var usernameInput; var passwordInput; @@ -42,6 +43,15 @@ errorImage.style.visibility = 'hidden'; } + showNotificationTimeDropdown = document.getElementById('showNotificationTimeDropdown'); + var showNotificationTime = localStorage.showNotificationTime || 0; + for (var i = 0; i < showNotificationTimeDropdown.options.length; i++) { + if (showNotificationTimeDropdown.options[i].value == showNotificationTime) { + showNotificationTimeDropdown.selectedIndex = i; + break; + } + } + refreshDropdown = document.getElementById('refresh'); var refreshTime = localStorage.refreshTime || REFRESH_DEFAULT; for (var i = 0; i < refreshDropdown.options.length; i++) { @@ -54,6 +64,7 @@ authCheckbox = document.getElementById('auth'); usernameInput = document.getElementById('username'); passwordInput = document.getElementById('password'); + showNotification = document.getElementById('showNotification'); if (typeof localStorage.username == 'string') { authCheckbox.checked = true; usernameInput.value = localStorage.username || ''; @@ -80,9 +91,38 @@ greenBalls = document.getElementById('greenBalls'); if (typeof localStorage.green == 'string') greenBalls.checked = true; + + showNotification = document.getElementById('showNotification'); + if( localStorage.showNotification == 'true' ) + showNotification.checked = true; + markClean(); } + + function requestUserPermission() { + try { + var checkboxUserPermission = document.getElementById('showNotification'); + if (checkboxUserPermission.checked) { + if (checkUserPermission()) + return; + + if (typeof webkitNotifications != "undefined") { + webkitNotifications.requestPermission(function () { + var permissionGranted = checkUserPermission(); + checkboxUserPermission.checked = permissionGranted; + }); + } + } + } catch (e) { checkboxUserPermission.checked = false; alert("error getting permissions");} + } + + function checkUserPermission() { + try { + return (webkitNotifications.checkPermission() == 0); + } catch (e) { return false; } + } + function save() { if (urlInput.value != '' && urlInput.value != 'http://') { @@ -91,6 +131,12 @@ delete localStorage.url; } + if (showNotificationTimeDropdown.value != "0") { + localStorage.showNotificationTime = showNotificationTimeDropdown.value; + } else { + delete localStorage.showNotificationTime; + } + if (refreshDropdown.value != REFRESH_DEFAULT) { localStorage.refreshTime = refreshDropdown.value; } else { @@ -122,6 +168,12 @@ delete localStorage.green; } + if (showNotification.checked == true) { + localStorage.showNotification = 'true'; + } else { + localStorage.showNotification = 'false'; + } + init(); chrome.extension.getBackgroundPage().init(); } @@ -140,7 +192,7 @@ usernameInput.disabled = true; passwordInput.disabled = true; } - + saveButton.disabled = false; } @@ -164,6 +216,7 @@

Options

+ + + + + + + + +
@@ -230,4 +306,3 @@

Options

-