Skip to content

Instantly share code, notes, and snippets.

@herejia
Created March 14, 2017 12:39
Show Gist options
  • Save herejia/3733afcfdcedc81aacd39ba2dd4ef012 to your computer and use it in GitHub Desktop.
Save herejia/3733afcfdcedc81aacd39ba2dd4ef012 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name jenkins-html5-notifier
// @namespace net.herezia
// @description Uses HTML5 notifications to show jenkins build result
// @include *jenkins*/console
// @version 1
// @grant none
// ==/UserScript==
const buildImageElement = document.querySelector('.build-caption > img');
if (!buildImageElement) {
return;
}
const spinner = document.querySelector('#spinner');
const hasEnded = () => !spinner || spinner.style.display === 'none';
const currentStatus = () => hasEnded() ? 'Ended' : buildImageElement.getAttribute('alt')
Notification.requestPermission();
let lastStatus = currentStatus();
let intervalId = setInterval(() => {
if (lastStatus !== currentStatus()) {
lastStatus = currentStatus();
notifyBuildStatus();
}
if (hasEnded()) {
clearInterval(intervalId);
}
}, 800);
function notifyBuildStatus() {
let jobDescription = Array.from(document.querySelectorAll('#breadcrumbs > .item a'))
.map(anchorElement => anchorElement.innerHTML)
.slice(-3)
.join(' ');
new Notification('Jenkins job status update', {
icon: buildImageElement.getAttribute('src'),
body: `${currentStatus()} : ${jobDescription}`,
renotify: true,
requireInteraction: true
});
}
@herejia
Copy link
Author

herejia commented Mar 14, 2017

tested on Jenkins ver. 2.19.3

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