ldeflandre / Jenkins: Rename replay link

// ==UserScript==
// @namespace https://openuserjs.org/users/ldeflandre
// @name     Jenkins: Rename replay link
// @description Rename /replay links that are named Rebuild like the legit /rebuild link
// @version  0.1
// @grant    none
// @include https://jenkins*
// @license MIT
// ==/UserScript==

// ==OpenUserJS==
// @author ldeflandre
// ==/OpenUserJS==

function xpath(context, expression) {
  const result = [];
  var i = document.evaluate(expression, context, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
  if (!i)
    throw new Error("Invalid query: " + expression);
  var data;
  while (data = i.iterateNext()) {
    result.push(data);
  }
  return result;
}

console.log("Renaming replay link");

// Filter static side menu
var nodes = xpath(document.getElementById("tasks"), './div/span/a[contains(@href,"/replay")]/span');
nodes.forEach(function (node) {
  if (node.innerText == "Rebuild") {
    node.innerText = "Replay";
  }
  //   node.classList.add("hide");
  return true;
});

// Filter dynamically added drop-down menus
var callback = function (mutations) {
  mutations.forEach(function (mutation) {
    mutation.addedNodes.forEach(function (node) {
      nodes = xpath(node, './div/ul/li/a[contains(@href,"/replay")]/span');
      nodes.forEach(function (node) {
        if (node.innerText == "Rebuild") {
          node.innerText = "Replay";
        }
        return true;
      });
      return true;
    })
  })
};

var observer = new MutationObserver(callback);
observer.observe(document.getElementById("breadcrumb-menu-target"), {
  childList: true
});