Skip to content

Instantly share code, notes, and snippets.

@M4C4R
Last active July 22, 2021 19:44
Show Gist options
  • Save M4C4R/34de4736c342fdc98beb7801892b1cc9 to your computer and use it in GitHub Desktop.
Save M4C4R/34de4736c342fdc98beb7801892b1cc9 to your computer and use it in GitHub Desktop.
Fixes Jenkins console logs from getting stuck as it force refreshes every 10 seconds, also fixes the inline approve/abort buttons. Click Raw to install on script managers like GreaseMonkey and Tampermonkey.
// ==UserScript==
// @author Mert Acar
// @name Refresh console text AND fix approve/abort buttons
// @version 0.1
// @include *//jenkinsserver001*/job/*/consoleText
// @include *//jenkinsserver001*/job/*/console
// @icon https://www.jenkins.io/favicon.ico
// @updateURL https://gist.github.com/M4C4R/34de4736c342fdc98beb7801892b1cc9/raw/bce4d4896eb6fec2453144df46f0f53220954a8b/jenkins_auto_refresh_console_logs.user.js
// ==/UserScript==
// *** START Configuration ***
const refreshSeconds = 10
// *** END Configuration ***
// -------------- Script Itself --------------
setInterval(function () {
// Check if we are looking at plain logs or formatted
var isPlainText = location.href.split('#')[0].includes('consoleText');
var address;
if (isPlainText) {
address = location.href.split('#')[0];
} else {
address = location.href.split('#')[0].replace(/console$/g, 'logText/progressiveHtml');
}
// Make a [synchronous] request to get the latest data
var xhr = new XMLHttpRequest();
xhr.open('GET', address, false);
xhr.send(null);
// Create a new temporary element for the new data
var newElement = document.createElement('div');
newElement.innerHTML = xhr.response.replace(/\(\'http[\.a-zA-Z0-9:\/]*:8080/g, '(\''); // This fixes the approve/abort buttons within the logs
var to_check;
// Get the existing logs as well as prepare newElement depending on isPlainText
if (isPlainText) {
to_check = document.querySelector('body > pre');
} else {
to_check = document.querySelector('.console-output');
}
if (newElement.innerHTML === to_check.innerHTML) {
console.log('Refresh console text: No changes');
} else {
console.log('Refresh console text: Applying changes');
var shouldWeScroll = window.innerHeight + (document.documentElement.scrollTop || document.body.scrollTop) == document.body.scrollHeight;
to_check.innerHTML = newElement.innerHTML;
if (shouldWeScroll) {
window.scrollTo(0,document.body.scrollHeight);
}
};
},refreshSeconds * 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment