Make a Jenkins log window with GreaseMonkey

Original link: https://www.kawabangga.com/posts/5166

Last time I introduced the basic usage of the oil monkey script . This article simply records a script that uses oil monkey to improve Quality of Life today.

First describe the problem I want to solve:

Many of our online operations are usually performed through Jenkins, and tasks need to be run on large-scale machines. Sometimes, these tasks take a long time to run. I generally keep an eye on these automations, and generally do other work. I want the log to always appear on the screen, but it doesn’t affect my other work.

The solution is, I added a button to the Jenkins page, through which a minimized window can be opened, the effect is as follows:

jenkins-setup.png

This is a newly added button, click here to pop up a log window

jenkins-log-scaled.jpeg

The log window that pops up is located in the upper left corner. There is no menu bar, no bookmark bar, and no Extentions. Basically all the space is used to display logs.

When doing this operation, there are real-time logs rolling all the time, so I feel more at ease.


The source code is as follows:

 // ==UserScript== 
  
// @name Jenkins Minimized Log 
  
// @namespace lxt 
  
// @version 0.1 
  
// @description Display jenkins log in a minimized window 
  
// @author [email protected] 
  
// @include https://jenkins.*console* 
  
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js 
  
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js 
  
 
  
// @run-at document-end 
  
// ==/UserScript== 
  
 
  
(function () { 
  
  "use strict"; 
  
 
  
  let url = new URL(window. location. href); 
  
 
  
  if (url. searchParams. get("view_window") === "minimized") { 
  
    minimized() 
  
  } 
  
 
  
  url.searchParams.append("view_window", "minimized"); 
  
 
  
  $(`<a href="${url}" class="open-in-minimal-window">Minimized log window</a>`).insertAfter( 
  
    "a[name='skip2content']" 
  
  ); 
  
 
  
  document. addEventListener("click", navigateTo, false); 
  
 
  
  function navigateTo(event) { 
  
    if (event. target. matches("a. open-in-minimal-window")) { 
  
      window. open( 
  
        event.target.href, 
  
        "_blank", 
  
        "menubar=no,toolbar=no,directories=no,resizable=yes,dependent,width=800,height=1000,left=0,top=0" 
  
      ); 
  
      event. preventDefault(); 
  
    } 
  
 
  
    return false; 
  
  } 
  
 
  
  var progress_bar = $(".build-caption-progress-container"); 
  
  progress_bar.appendTo("#spinner"); 
  
 
  
  function minimized() { 
  
    console.log("should use minimized window!"); 
  
    $("#side-panel"). remove(); 
  
    $(".page-footer").remove(); 
  
  } 
  
})();

Paste it directly to your oil monkey and use it.

The core logic is, if the current page is the original URL of Jenkins (Params does not have view_window=minimized), insert a link <a> on the page, and the target is the current URL + parameter view_window=minimized. Through the code, set the menubar and toolbar to be closed when the URL is opened, and set the window size, position, etc.

If the parameter view_window=minimized is detected in the URL, delete the sidebar, footbar, etc. in the page, so that all the space is used to display logs.

The most needed button during operation is the stop button, but the default Jenkins puts this button on the top of the page, so that you can see the scrolling log at the bottom. If you need to stop, you have to drag it to the top of the page to find the button, which is too slow . I used JQuery to put it under the log scroll.

I originally wanted to make a Jenkins plug-in to directly change the company’s Jenkins, but after reading the Jenkins release plug-in, it is still quite complicated, and I have to write some Java and XML, which may take a day or two. So it was realized directly with the oil monkey, and it took half an hour.

The post Making a Jenkins Logging Window with GreaseMonkey first appeared on Kawabanga! .

This article is transferred from: https://www.kawabangga.com/posts/5166
This site is only for collection, and the copyright belongs to the original author.