ServiceNow - Open BG Iframe

Open a casual Background Script frame in current List/Form

Verze ze dne 24. 10. 2020. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         ServiceNow - Open BG Iframe
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Open a casual Background Script frame in current List/Form
// @author       Ricardo Constantino <[email protected]>
// @match        https://*.service-now.com/*.do?*
// @exclude      https://*.service-now.com/sys.scripts.do
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let isForm = typeof g_form !== 'undefined';
    let isList = typeof GlideList2 !== 'undefined' && document.querySelector('#sys_target') != null && GlideList2.get(document.querySelector('#sys_target').value) != null;
    if (!isForm && !isList) return;

    let scriptPreset = '';
    if (isForm)
        scriptPreset = `
var table = ${JSON.stringify(g_form.getTableName())},
    recID = ${JSON.stringify(g_form.getUniqueValue())};

var gr = new GlideRecord(table); gr.get(recID);
// new GlideUpdateManager2().saveRecord(gr)
`.trim();
    else if (isList) {
        let g_list = GlideList2.get(document.querySelector('#sys_target').value);
        let filter = g_list.getQuery({all:true}).split('^');
        if (!g_list.orderBySet) {
            filter.push('ORDERBY' + (g_list.sortDir === 'ASC' ? '' : 'DESC') + g_list.sortBy);
        }

        scriptPreset = `
var table = ${JSON.stringify(g_list.getTableName())},
    filter = ${JSON.stringify(filter.join('^'))};

var gr = new GlideRecord(table); gr.addEncodedQuery(filter); gr.query(); gr.next();
// new GlideUpdateManager2().saveRecord(gr)
`.trim();
    }

    const style = document.createElement('style');
    style.innerText = `
#bg_iframe {
height: 96vh;
top: 3em;
position: fixed;
right: 0;
width: 30%;
z-index: 45;
min-width: 400px;
}

#bg_toggle {
position: fixed;
top: 0;
right: 50%;
z-index: 50;
}
`;

    document.head.appendChild(style);

    const iframe = document.createElement('iframe');
    iframe.id = 'bg_iframe';
    iframe.setAttribute('src', '/sys.scripts.do');
    iframe.addEventListener('load', () => {
        iframe.contentDocument.querySelector('#runscript').value = scriptPreset + '\n\n';
    });

    const button = document.createElement('button');
    button.id = 'bg_toggle';
    button.textContent = 'Open BG frame';
    button.addEventListener('click', () => {
        let iframeElement = document.querySelector('#bg_iframe');
        if (iframeElement) {
            iframeElement.toggle('display');
        } else {
            document.body.appendChild(iframe);
        }
    });
    document.body.appendChild(button);
})();