ServiceNow - Update Versions - Highlight different Sources

Highlight Update Versions with the same Update Set (Source)

当前为 2020-09-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         ServiceNow - Update Versions - Highlight different Sources
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Highlight Update Versions with the same Update Set (Source)
// @author       Ricardo Constantino <[email protected]>
// @match        https://*.service-now.com/sys_update_version_list.do*
// @match        https://*.service-now.com/sys_metadata.do?*
// @match        https://*.service-now.com/*.do?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    let palette = [
        '#ED90A466',
        '#ABB15066',
        '#00C1B266',
        '#ACA2EC66'
    ];

    //if (!window.location.pathname.startsWith('/sys_update_version_list.do') && typeof g_form === 'undefined') return;

    let hooks = {
        versions_list: 'div#sys_update_version',
        versions_rellist: '#related_lists_wrapper'
    };

    if (typeof g_form !== 'undefined' && g_form.getTableName() !== 'sys_update_version' &&
        (g_form.getRelatedListNames().includes('REL:67bdac52374010008687ddb1967334ee') || g_form.getRelatedListNames().includes('REL:67bdac52374010008687ddb1967334ee_list'))) {
        console.log('Form with Update Versions related list, creating observer');
        new MutationObserver(observerCallback).observe(document.querySelector(hooks.versions_rellist), { childList: true });
        applySourceColors();
    } else if (document.querySelector(hooks.versions_list)) {
        console.log('Update Versions list, creating observer');
        new MutationObserver(observerCallback).observe(document.querySelector(hooks.versions_list), { childList: true });
        applySourceColors();
    } else {
        console.log('Last chance');
        applySourceColors();
    }

    function observerCallback(mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type !== 'childList') continue;
            if (!mutation.target.querySelector('table[glide_table="sys_update_version"]')) return;
            console.log('Mutation added table we want to color:', mutation);
            applySourceColors();
        }
    }

    function applySourceColors() {
        let versionsTable = document.querySelector('table[glide_table="sys_update_version"]');
        if (!versionsTable) return;

        let sourceColumn = versionsTable.querySelector('th[name="source"]');
        if (!sourceColumn) return;

        let sourceColumnIdx = [...sourceColumn.parentElement.children].indexOf(sourceColumn);

        let hrefPaletteMap = {};
        let curPalette = -1;
        [...versionsTable.querySelectorAll('td:nth-child('+(sourceColumnIdx+1)+') a')]
            .forEach(e => {
            let id = e.href.match(/[a-f0-9]{32}/);
            if (id in hrefPaletteMap) {
                e.parentElement.setStyle('background-color: ' + hrefPaletteMap[id]);
                // if you also want the column before/after Source to be colored, uncomment these:
                //e.parentElement.previousElementSibling.setStyle('background-color: ' + hrefPaletteMap[id]);
                //e.parentElement.nextElementSibling.setStyle('background-color: ' + hrefPaletteMap[id]);
                return;
            }

            curPalette = (curPalette + 1) % palette.length;
            hrefPaletteMap[id] = palette[curPalette];
            e.parentElement.parentElement.childNodes.forEach(sibling => sibling.setStyle('font-weight: bold; background-color: ' + hrefPaletteMap[id]));
        })
    }
})();