Skip to content

Instantly share code, notes, and snippets.

@pmbauer
Last active March 16, 2018 20:54
Show Gist options
  • Save pmbauer/339a836c517ad80dbe6961caf0c8e985 to your computer and use it in GitHub Desktop.
Save pmbauer/339a836c517ad80dbe6961caf0c8e985 to your computer and use it in GitHub Desktop.
grease monkey script to highlight personal jobs in the gitlab pipeline view, because linear search is a drag
// ==UserScript==
// @name gitlab pipeline job highlighter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description highlight personal jobs, customize for your url (match)
// @author pmbauer
// @match https://gitlab.ddbuild.io/*/pipelines*
// @grant none
// ==/UserScript==
var __GLUSER_CLS = '.branch-name, .commit-row-message, .ref-name'; // Elements that might contain your username
var __JOB_CLS = '__myjob'; // Class to outline a job row
// element to watch for before running the highlighter
var __LOADED_SELECTOR = 'div.commit:nth-child(21)';
// Adapted from: https://gist.github.com/redking/0de4094a41a6734bdfa665c3f13a86d3
function doTheGitLabHighlight() {
var __GLUSER = $('.profile-link')[0].getAttribute('data-user');
document.querySelectorAll(__GLUSER_CLS).forEach(e => e.setAttribute('title', e.innerText));
document.styleSheets[0].insertRule(`.${__JOB_CLS}{outline: 2px solid red}`, 1);
__clearPipes = () => document.querySelectorAll(`.${__JOB_CLS}`).forEach(e => e.classList.remove(__JOB_CLS));
__clearPipes();
Array.prototype.slice.call(document.querySelectorAll(__GLUSER_CLS))
.filter(e => e.innerText.indexOf(__GLUSER) !== -1)
.forEach(e => {
p = ((el, cls) => {
while ((el = el.parentElement) && !el.classList.contains(cls));
return el;
})(e, 'commit');
if (p) {
p.classList.add(__JOB_CLS);
p.addEventListener('DOMSubtreeModified', __clearPipes, {once: true});
}
});
}
// adapted from https://gist.github.com/raw/2625891/waitForKeyElements.js
function waitForKeyElements (
selectorTxt, /* Required: The jQuery selector string that
specifies the desired element(s).
*/
actionFunction, /* Required: The code to run when elements are
found. It is passed a jNode to the matched
element.
*/
bWaitOnce /* Optional: If false, will continue to scan for
new elements even after the first match is
found.
*/
) {
var targetNodes, btargetsFound;
targetNodes = $(selectorTxt);
if (targetNodes && targetNodes.length > 0) {
btargetsFound = true;
/*--- Found target node(s). Go through each and act if they
are new.
*/
targetNodes.each ( function () {
var jThis = $(this);
var alreadyFound = jThis.data ('alreadyFound') || false;
if (!alreadyFound) {
//--- Call the payload function.
var cancelFound = actionFunction (jThis);
if (cancelFound)
btargetsFound = false;
else
jThis.data ('alreadyFound', true);
}
} );
}
else {
btargetsFound = false;
}
//--- Get the timer-control variable for this selector.
var controlObj = waitForKeyElements.controlObj || {};
var controlKey = selectorTxt.replace (/[^\w]/g, "_");
var timeControl = controlObj [controlKey];
//--- Now set or clear the timer as appropriate.
if (btargetsFound && bWaitOnce && timeControl) {
//--- The only condition where we need to clear the timer.
clearInterval (timeControl);
delete controlObj [controlKey];
}
else {
//--- Set a timer, if needed.
if ( ! timeControl) {
timeControl = setInterval ( function () {
waitForKeyElements ( selectorTxt,
actionFunction,
bWaitOnce
);
},
300
);
controlObj [controlKey] = timeControl;
}
}
waitForKeyElements.controlObj = controlObj;
}
(function() {
'use strict';
waitForKeyElements(__LOADED_SELECTOR, doTheGitLabHighlight);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment