ServiceNow - Open BG Iframe

Open a casual Background Script frame in current List/Form

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         ServiceNow - Open BG Iframe
// @namespace    http://tampermonkey.net/
// @version      0.3.1
// @description  Open a casual Background Script frame in current List/Form
// @author       Ricardo Constantino <[email protected]>
// @license      MIT
// @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 = '';
    calculateScriptPreset();

    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;
}

#bg_refresh {
position: fixed;
top: 3em;
right: 0;
z-index: 50;
background: #eee;
}
`;

    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 button2 = document.createElement('button');
    button2.id = 'bg_refresh';
    button2.title = 'Refresh Checked rows filter';
    button2.textContent = '↻';
    button2.addEventListener('click', () => {
        let iframeElement = document.querySelector('#bg_iframe');
        if (iframeElement) {
            calculateScriptPreset();
            let currentBG = iframeElement.contentDocument.querySelector('#runscript').value.split(/\r?\n/).slice(4).join('\n');
            iframeElement.contentDocument.querySelector('#runscript').value = scriptPreset.split(/\r?\n/).slice(0,5).join('\n') + currentBG;
        }
    });

    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');
            document.querySelector('#bg_refresh').toggle('display');
        } else {
            document.body.appendChild(iframe);
            if (isList) document.body.appendChild(button2);
        }
    });
    document.body.appendChild(button);

    function calculateScriptPreset() {
        if (isForm) {
            let _filter = 'sys_id=' + g_form.getUniqueValue();
            scriptPreset = `
var table = ${JSON.stringify(g_form.getTableName())}, recID,
    filter = ${JSON.stringify(_filter)};

var gr = new GlideRecord(table); gr.addEncodedQuery(filter); gr.query();

while (gr.next()) {
    recID = gr.getUniqueValue();
    // 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);
            }
            let checked = (g_list.getChecked() != '' ? 'sys_idIN'+g_list.getChecked()+'^' : '');

            scriptPreset = `
var table = ${JSON.stringify(g_list.getTableName())}, recID,
    filter = ${JSON.stringify(_filter.join('^'))};
    filter = ${JSON.stringify(checked)} + filter; // checked rows filter
var gr = new GlideRecord(table); gr.addEncodedQuery(filter); gr.query();

while (gr.next()) {
    recID = gr.getUniqueValue();
    // new GlideUpdateManager2().saveRecord(gr)

}
`.trim();
        }
    }
})();