magnushakansson / JenkinsBuildPageFix

// ==UserScript==
// @name         JenkinsBuildPageFix
// @version      0.9
// @description  Load the the build page quicker
// @author       Magnus HÃ¥kansson
// @match        https://*/*/*/job/*/job/*/build?delay=0sec
// @match        https://*/job/*/job/*/build?delay=0sec
// @grant        GM_xmlhttpRequest
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const Event = class {
        constructor(script, target) {
            this.script = script;
            this.target = target;
            this._cancel = false;
            this._replace = null;
            this._stop = false;
        }
        preventDefault() {
            this._cancel = true;
        }
        stopPropagation() {
            this._stop = true;
        }
        replacePayload(payload) {
            this._replace = payload;
        }
    };
    let callbacks = [];
    window.addBeforeScriptExecuteListener = (f) => {
        if (typeof f !== "function") {
            throw new Error("Event handler must be a function.");
        }
        callbacks.push(f);
    };
    window.removeBeforeScriptExecuteListener = (f) => {
        let i = callbacks.length;
        while (i--) {
            if (callbacks[i] === f) {
                callbacks.splice(i, 1);
            }
        }
    };
    const dispatch = (script, target) => {

        if (script.tagName !== "SCRIPT") {
            return;
        }
        const e = new Event(script, target);
        if (typeof window.onbeforescriptexecute === "function") {
            try {
                window.onbeforescriptexecute(e);
            } catch (err) {
                console.error(err);
            }
        }
        for (const func of callbacks) {
            if (e._stop) {
                break;
            }
            try {
                func(e);
            } catch (err) {
                console.error(err);
            }
        }
        if (e._cancel) {
            script.textContent = "";
            script.remove();
            console.log("Cancelled!");
        } else if (typeof e._replace === "string") {
            script.textContent = e._replace;
        }
    };
    const observer = new MutationObserver((mutations) => {
        for (const m of mutations) {
            for (const n of m.addedNodes) {
                dispatch(n, m.target);
            }
        }
    });

    observer.observe(document, {
        childList: true,
        subtree: true,
    });

    function addScript(text) {
        text = text.replace(/UnoChoice\.renderDynamicRenderParameter.+;/g, "");
        text = text.replace(/this\.proxy\.getChoicesAsStringForUI\(function \(t\) {\s+var options = t\.responseText;\s+parameterElement\.innerHTML = JSON\.parse\(options\);\s+}\);/g, "");
        var newScript = document.createElement('script');
        newScript.type = "text/javascript";
        newScript.textContent = text;
        newScript.async = false;
        var head = document.getElementsByTagName('head')[0];
        console.log(newScript);
        head.appendChild(newScript);
    }

    window.onbeforescriptexecute = (e) => {
        if (e.script.src.search(/unochoice\.js/) != -1 || e.script.src.search(/UnoChoice\.js/) != -1) {
            e.preventDefault();
            console.log('onbeforescriptexecute');
            e.stopPropagation();
        }
    };

    function waitForFnc() {
        var scripts = document.getElementsByTagName("script");
        var found = false;
        var i;
        // Find another script with the correct first path in the src
        for (i = 0; i < scripts.length && !found; i++) {
            if (/.*adjuncts.*/.test(scripts[i].src)) {
                found = true;
            }
        }

        // Try again if not found
        if(!found){
            window.setTimeout(waitForFnc, 100);
            console.log("waitForFnc: waited");
        } else {
            console.log("waitForFnc");
            var src = scripts[2].src;
            // Remove everything after "org" so we can replace it with what we want
            src = src.replace(/\/org\/.*/g, "/org/");
//            console.log(src);
            GM_xmlhttpRequest({
                method: "GET",
                url: src + "biouno/unochoice/stapler/unochoice/UnoChoice.js",
//                url: src + "biouno/unochoice/stapler/unochoice/unochoice.js",
                onload: function(response) {
                   addScript(response.responseText);
                }
            });
        }
    }
    waitForFnc();

})();

jQuery(document).ready(function(){
    jQuery.noConflict();
    console.log("Ready!");
    jQuery("#BTN_SENSE_DEFAULT").each(function(index, element) {
        element.click();
        console.log("Sense Default");
    });

});