ServiceNow - Open BG Iframe

Open a casual Background Script frame in current List/Form

当前为 2021-02-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ServiceNow - Open BG Iframe
// @namespace    http://tampermonkey.net/
// @version      0.2
// @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';

    const isForm = typeof g_form !== 'undefined';
    const 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();
while (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);
})();