ServiceNow - Open BG Iframe

Open a casual Background Script frame in current List/Form

Version vom 24.10.2020. Aktuellste Version

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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.

(I already have a user style manager, let me install it!)

// ==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);
})();