Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

flat merge of both notification and filter support #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
89 changes: 80 additions & 9 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Prefs
var refreshTime;
var requestUrl;
var filterPattern;
var auth;
var desc;
var green;
Expand All @@ -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();
}

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 36 additions & 2 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<link rel="stylesheet" type="text/css" href="style.css"/>
<script type="text/javascript" src="constants.js"></script>
<script type="text/javascript" src="options.js"></script>

</head>
<body>
<h1>Hudson Monitor</h1>
Expand Down Expand Up @@ -52,6 +51,24 @@ <h1>Hudson Monitor</h1>
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend><input type="checkbox" id="showNotifications"/> Desktop Notifications</legend>
<table>
<tr>
<td><label for="notificationTimeout">Hide notification after:</label></td>
<td colspan="2">
<select id="notificationTimeout" name="notificationTimeout">
<option value="0">never hide</option>
<option value="5">5 seconds</option>
<option value="15">15 seconds</option>
<option value="30">30 seconds</option>
<option value="60">1 minute</option>
<option value="300">5 minutes</option>
</select>
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>
Expand Down Expand Up @@ -95,11 +112,28 @@ <h1>Hudson Monitor</h1>
</tr>
</table>
</fieldset>
<fieldset>
<legend><input id="enableFilter" type="checkbox" name="enabled"/> Filter by job name</legend>
<table>
<tr>
<td><input id="filterText" type="text" name="filterText" style="width:200px"/></td>
<td><input id="filterCaseSensitive" type="checkbox" name="filterCaseSensitive" value="yes" title="make filter case-sensitive" checked/></td>
</tr>
<tr>
<td colspan="2">
<p>
The filter text is converted into a regular expression. Only things that match
the expression are displayed in the popup and considered for the main status
image.
</p>
</td>
</tr>
</table>
</fieldset>
<div class="optionform">
<a href="license.html">License</a>
<input type="button" id="save" name="save" value="Save"/>
<input type="button" id="cancel" name="cancel" value="Cancel"/>
</div>
</body>
</html>

Loading