xSh0uTx / ServiceNow NewCall Utilities

// ==UserScript==
// @name         ServiceNow NewCall Utilities
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  ServiceNow NewCall Utilities
// @author       76561198102993358
// @match        https://qvcprod.service-now.com/new_call.do?*
// @grant        none
// @copyright 2021, xSh0uTx (https://openuserjs.org/users/xSh0uTx)
// @license MIT
// @downloadURL https://openuserjs.org/install/xSh0uTx/ServiceNow_NewCall_Utilities.user.js
// @updateURL https://openuserjs.org/meta/xSh0uTx/ServiceNow_NewCall_Utilities.meta.js
// ==/UserScript==




function ID_Button(event) {
    let tempButton = $('myButton2');
    navigator.clipboard.writeText(g_form.getReference('caller').user_name);
    tempButton.innerHTML = 'C';
    setTimeout(function(){ tempButton.innerHTML= "ID"; }, 1000);
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function record_Behalf_Button(event) {
    let pop = null;
    let loop = 0;
    while(loop < 6){
        pop = document.querySelector("div.popover-header.popover-header-bar.popover-header-buttons");
        await sleep(200);
        loop += 1;
        if(pop != null){
            let user = document.querySelector("#sys_readonly\\.sys_user\\.user_name").value;
            let domain = "QVCDEV"
            pop.innerHTML += `<a target="_blank" rel="noopener noreferrer" href="customURI:?u?${user}" class="btn btn-sm btn-default pop-over-button pull-right">Unlock</a>
                              <a target="_blank" rel="noopener noreferrer" href="customURI:?ls?${user}?${domain}" class="btn btn-sm btn-default pop-over-button pull-right">LockStatus</a>`;
            console.log(loop);
            return;
        }
    }
}

async function record_Caller_Button(event) {
    let pop = null;
    let loop = 0;
    while(loop < 6){
        pop = document.querySelector("div.popover-header.popover-header-bar.popover-header-buttons");
        await sleep(200);
        loop += 1;
        if(pop != null){
            let user = document.querySelector("#sys_readonly\\.sys_user\\.user_name").value;
            let domain = "QVCDEV"
            pop.innerHTML += `<a target="_blank" rel="noopener noreferrer" href="customURI:?u?${user}" class="btn btn-sm btn-default pop-over-button pull-right">Unlock</a>
                              <a target="_blank" rel="noopener noreferrer" href="customURI:?ls?${user}?${domain}" class="btn btn-sm btn-default pop-over-button pull-right">LockStatus</a>`;
            console.log(loop);
            return;
        }
    }
}
var caller_addons = document.querySelector("#element\\.new_call\\.caller > div.col-xs-2.col-sm-3.col-lg-2.form-field-addons");
caller_addons.innerHTML += '<button id="myButton2" style="white-space: nowrap" type="button" title="" data-original-title="Copy User ID" aria-expanded="false">ID</button>';



document.querySelector("#myButton2").addEventListener("click", ID_Button , false);


document.querySelector("#viewr\\.new_call\\.u_service_recipient").addEventListener ("click", record_Behalf_Button , false);
document.querySelector("#viewr\\.new_call\\.caller").addEventListener ("click", record_Caller_Button , false);