ServiceNow - Reference direct link to record

Add a direct link button to reference fields in forms

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ServiceNow - Reference direct link to record
// @version      0.0.5
// @description  Add a direct link button to reference fields in forms
// @author       Matteo Lecca
// @match        *.service-now.com/*.do*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=service-now.com
// @grant        none
// @license MIT
// @namespace    https://greasyfork.org/users/1246673
// ==/UserScript==

(function () {
    'use strict';

    let referenceList = document.querySelectorAll("button[data-type='reference_popup']:not([style*='display: none'])");

    referenceList.forEach(referenceField => {
        //let table = referenceField.dataset.table;
        let field = referenceField.dataset.ref;
        let form = referenceField.dataset.form;
        let recordId = g_form.getValue(field);

        let buttonLink = document.createElement('a');
        buttonLink.title = '[WK - SN] Direct link to record';
        buttonLink.href = 'https://' + window.location.hostname + '/' + form + '?sys_id=' + recordId;
        buttonLink.classList.add('btn', 'btn-info', 'btn-ref', 'icon', 'icon-arrow-right');

        referenceField.parentNode.append(buttonLink);
    });

    let documentIdList = document.querySelectorAll("a[data-type*='id_document_id_reference_popup']");

    documentIdList.forEach(documentField => {
        let field = documentField.dataset.ref;
        let form = documentField.dataset.form;
        let recordId = g_form.getValue(field);

        let buttonLink = document.createElement('a');
        buttonLink.title = '[WK - SN] Direct link to record';
        buttonLink.href = 'https://' + window.location.hostname + '/' + form + '?sys_id=' + recordId;
        buttonLink.classList.add('btn', 'btn-info', 'btn-ref', 'icon', 'icon-arrow-right');

        documentField.parentNode.append(buttonLink);
    });

    let variableIdList = document.querySelectorAll("a[id^='ni.VE'][id$='LINK.info']");

    variableIdList.forEach(variableField => {
        let elementId = variableField.id.replace('LINK.info', '');
        let form;
        let recordId;
        let tableElem = document.getElementById(elementId + "TABLE");
        if (tableElem) {
            form = tableElem.value;
        }
        let sysIdElem = document.getElementById(elementId);
        if (sysIdElem) {
            recordId = sysIdElem.value;
        }

        if(form && recordId) {
            let buttonLink = document.createElement('a');
            buttonLink.title = '[WK - SN] Direct link to record';
            buttonLink.href = 'https://' + window.location.hostname + '/' + form + '?sys_id=' + recordId;
            buttonLink.classList.add('btn', 'btn-info', 'btn-ref', 'icon', 'icon-arrow-right');

            variableField.parentNode.append(buttonLink);
        }
    });

})();