bbogdan / MatchPoint Browse Configs

// ==UserScript==
// @name         MatchPoint Browse Configs
// @namespace    http://bogdan.com
// @version      0.3
// @description  try to take over the world!!
// @author       bbogdan
// @match        https://*/*ConfigEditorForm.aspx*
// @grant        none
// ==/UserScript==

(function() {
    var editorPath = window.location.href.split('?')[0];
    $('select').each(function(i, e) {
        var $addLink = $('<a href="#">Open</a>').click(function() {
            var url = getUrl(e.value);
            if (url) {
                window.location.href =  url;
            } else {
                console.log('Invalid url');
            }
        });
        if (getUrl(e.value) || !e.value) {
            $(e).parents('tr:first').children().first().append($addLink);
        }
    });

    function getUrl(val) {
        if (val) {
            var parts = val.split('#');
            if (parts.length === 2) {
                if (parts[1].endsWith('.xml')) {
                    return String.format('{0}?Type={1}&File={2}&Source={3}', editorPath, parts[0], parts[1], encodeURIComponent(window.location.href));
                }
            }
        }

        return null;
    }
})();