whatnowmax / crm_replace_text_with_JIRA_links

// ==UserScript==
// @name crm_replace_text_with_JIRA_links
// @descriptioncase_title_in_tab_title
// @version 1.6
// @author whatnowmax
// @license MIT
// @match https://crm.stagrp.com/index.php?action=DetailView&module=Cases&record=*
// @match https://crm.stagrp.com/index.php?module=Cases*

// @run-at document-end
// @grant none
// ==/UserScript==

    //need two separate functions for IDs and ClassNames because IDs are not arrays

    var _projList = ["TIDALDEV-","TP-","TAA-","TE-","TR-","TRP-"];
    for (var i = 0; i < _projList.length; i++){
        //replace all case updates for each type
        replaceWithJIRALink_ClassName(_projList[i],"caseUpdate");
        //replace resolution summary for each type
        replaceWithJIRALink_ID(_projList[i],"resolution");
        //replace subject for each type
        replaceWithJIRALink_ID(_projList[i],"name");
        replaceWithJIRALink_ID(_projList[i],"description");
        replaceWithJIRALink_ID(_projList[i],"dtbc_business_impact_c");
        replaceWithJIRALink_ID(_projList[i],"dtbc_steps_performed_c");
        replaceWithJIRALink_ID(_projList[i],"dtbc_action_plan_c");
    }




function replaceWithJIRALink_ClassName(_proj,_field) {
    var _caseUPs = document.getElementsByClassName(_field);
    //console.log("replacing links to JIRA project " + _proj + " for " + _caseUPs.id);

    //for each case update
    for (var i = 0; i < _caseUPs.length; i++){
        var _newCaseUP;
        var _bugid;
        //split by TIDALDEV-
        var _spl = _caseUPs[i].innerHTML.split(_proj);
        //if the split array has more than one length
        if (_spl.length > 1) {
            //start at the second string since the first is either blank (TIDALDEV- is at the beginning) or irrelevant
            _newCaseUP = _spl[0];
            for (var j = 1; j < _spl.length; j++){
               _bugid = _spl[j].match(/[a-z0-9'\-]+/gi)[0];
               _newCaseUP = _newCaseUP + '<a target="_blank" href="https://tidalautomation.atlassian.net/browse/' + _proj + _bugid + '">' + _proj + _bugid + '</a>';
               //chop off bug id before adding
               _newCaseUP = _newCaseUP + _spl[j].substring(_bugid.length,_spl[j].length);
            }
        //set the new case update
        _caseUPs[i].innerHTML = _newCaseUP;
        //console.log(_newCaseUP);
        }
    }
}

function replaceWithJIRALink_ID(_proj,_field) {
    var _caseUP = document.getElementById(_field);
    //console.log("replacing links to JIRA project " + _proj + " for " + _caseUP.id);
    //no loop because IDs only have one record
        var _newCaseUP;
        var _bugid;
        //split by TIDALDEV-
        var _spl = _caseUP.innerHTML.split(_proj);
        //if the split array has more than one length
        if (_spl.length > 1) {
            //start at the second string since the first is either blank (TIDALDEV- is at the beginning) or irrelevant
            _newCaseUP = _spl[0];
            for (var j = 1; j < _spl.length; j++){
               _bugid = _spl[j].match(/[a-z0-9'\-]+/gi)[0];
               _newCaseUP = _newCaseUP + '<a target="_blank" href="https://tidalautomation.atlassian.net/browse/' + _proj + _bugid + '">' + _proj + _bugid + '</a>';
               //chop off bug id before adding
               _newCaseUP = _newCaseUP + _spl[j].substring(_bugid.length,_spl[j].length);
            }
        //set the new case update
        _caseUP.innerHTML = _newCaseUP;
        //console.log(_newCaseUP);
        }
}