// ==UserScript== // @name Login + OB / IB sur FCLM // @namespace add stations to function rollup // @version 3.1 // @description adds stations to function rollup // @author @aakalish // @downloadURL https://axzile.corp.amazon.com/-/carthamus/download_script/function-rollup-logins-aakalish.user.js // @updateURL https://axzile.corp.amazon.com/-/carthamus/download_script/function-rollup-logins-aakalish.user.js // @match https://fclm-portal.amazon.com/reports/functionRollup?* // @match https://fclm-portal.amazon.com/reports/timeOnTask?* // @match https://fclm-portal.amazon.com/reports/ppaTimeOnTask?* // @grant GM_xmlhttpRequest // @grant GM_addStyle // @require https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js // ==/UserScript== // v1.0 initial // v1.1 changed class name on header to work with the AA station script. // v2.0 added time on task and copying badge barcode // v3.0 added shift code (function() { 'use strict'; var logins = {}; var stTimer; var building = document.URL.split('warehouseId=')[1].split('&')[0]; GM_addStyle(`#snackbar { visibility: hidden; color: #fff; background-color: #333; min-width: 250px; margin-left: -125px; border-radius: 2px; padding: 16px; text-align: center; left: 50%; bottom: 30px; z-index: 1; position: fixed; } /* This will be activated when the snackbar's class is 'show' which will be added through JS */ #snackbar.show { visibility: visible; -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s; animation: fadein 0.5s, fadeout 0.5s 2.5s; } /* Animations for fading in and out */ @-webkit-keyframes fadein { from {bottom: 0; opacity: 0;} to {bottom: 30px; opacity: 1;} } @keyframes fadein { from {bottom: 0; opacity: 0;} to {bottom: 30px; opacity: 1;} } @-webkit-keyframes fadeout { from {bottom: 30px; opacity: 1;} to {bottom: 0; opacity: 0;} } @keyframes fadeout { from {bottom: 30px; opacity: 1;} to {bottom: 0; opacity: 0;} }`); var functionRollup = { show : function() { var tbls = document.getElementsByClassName('sortable result-table align-left').length > 0 ? document.getElementsByClassName('sortable result-table align-left') : document.getElementsByClassName('sortable result-table'); var is = document.URL.split('imeOnTask').length > 1 ? { id: 0, y: 2 } : { id: 1, y: 4 }; for ( var i = 0; i < tbls.length; i++ ) { if (tbls[i].querySelectorAll('.headerCompleter').length == 0) { let thead = tbls[i].querySelector('thead'); let hd = thead.children[0].children[0].cloneNode(true); hd.innerText = 'Login'; hd.classList.add('headerCompleter'); thead.children[0].insertBefore(hd,thead.children[0].children[is.y]); $(hd).click(function() { let down = true; if(this.classList.contains('headerSortDown')) { down = false; } this.className = this.className.replace('headerSortUp','').replace('headerSortDown',''); this.className += down? ' headerSortDown' : ' headerSortUp'; let col = 0; let column = 0; let ch = this.parentElement.children let cur = this; while (col < ch.length) { if(cur.previousSibling.hasOwnProperty('column')) { column = cur.previousSibling.column + col; col = ch.length } else { cur = cur.previousSibling }; col++; }; let table = $(this).closest('table').find('tbody')[0]; let rows = $(this).closest('table').find('tbody').find('tr[class*=empl]'); rows.sort(function(a,b) { if( a.children[column].innerText > b.children[column].innerText ) { return (down? -1 : 1) * 1; } else { return (down? -1 : 1) * -1; }}) rows.each(function() { table.appendChild(this) }); }); hd = thead.children[0].children[0].cloneNode(true); hd.innerText = 'Shift Code'; hd.classList.add('headerCompleter'); thead.children[0].insertBefore(hd,thead.children[0].children[is.y]); $(hd).click(function() { let down = true; if(this.classList.contains('headerSortDown')) { down = false; } this.className = this.className.replace('headerSortUp','').replace('headerSortDown',''); this.className += down? ' headerSortDown' : ' headerSortUp'; let col = 0; let column = 0; let ch = this.parentElement.children let cur = this; while (col < ch.length) { if(cur.previousSibling.hasOwnProperty('column')) { column = cur.previousSibling.column + col; col = ch.length } else { cur = cur.previousSibling }; col++; }; let table = $(this).closest('table').find('tbody')[0]; let rows = $(this).closest('table').find('tbody').find('tr[class*=empl]'); rows.sort(function(a,b) { if( a.children[column].innerText > b.children[column].innerText ) { return (down? -1 : 1) * 1; } else { return (down? -1 : 1) * -1; }}) rows.each(function() { table.appendChild(this) }); }); }; let rows = document.URL.split('imeOnTask').length > 1 ? tbls[i].querySelectorAll('tr.tot-row') : tbls[i].querySelectorAll('tr.empl-all'); let loops = document.URL.split('imeOnTask').length == 1? rows.length - 1 : rows.length; for ( var j = 0; j < loops; j++ ) { let td; if( !document.getElementById('ider' + i + rows[j].children[is.id].innerText) ) { td = rows[j].children[0].cloneNode(true); } else { td = document.getElementById('ider' + i + rows[j].children[is.id].innerText); }; td.innerText = logins.hasOwnProperty(rows[j].children[is.id].innerText) ? logins[rows[j].children[is.id].innerText].login : '-'; td.id = 'ider' + i + rows[j].children[is.id].innerText; rows[j].insertBefore(td,rows[j].children[is.y]); td.addEventListener("click", copyBadgeBarcode); if( !document.getElementById('sc' + i + rows[j].children[is.id].innerText) ) { td = rows[j].children[0].cloneNode(true); } else { td = document.getElementById('sc' + i + rows[j].children[is.id].innerText); }; td.innerText = logins.hasOwnProperty(rows[j].children[is.id].innerText) ? logins[rows[j].children[is.id].innerText].shiftCode : '-'; td.id = 'sc' + i + rows[j].children[is.id].innerText; rows[j].insertBefore(td,rows[j].children[is.y]); //td.addEventListener("click", copyBadgeBarcode); }; if ( document.URL.split('imeOnTask').length == 1 ) { if (!rows[rows.length-1].children[0].classList.contains('herer')) { rows[rows.length-1].children[0].colSpan = rows[rows.length-1].children[0].colSpan + 2; rows[rows.length-1].children[0].classList.add('herer'); }; } else { if (!tbls[i].querySelector('tr.total').children[0].classList.contains('herer')) { let row = tbls[i].querySelector('tr.total'); row.children[0].colSpan = row.children[0].colSpan + 2; row.children[0].classList.add('herer'); }; }; }; } , getEmployeeIds : function () { var tbls = document.getElementsByClassName('sortable result-table align-left').length > 0 ? document.getElementsByClassName('sortable result-table align-left') : document.getElementsByClassName('sortable result-table'); let AAs = []; var is = document.URL.split('imeOnTask').length > 1 ? { id: 0, y: 2 } : { id: 1, y: 4 }; for ( var i = 0; i < tbls.length; i++ ) { let rows = document.URL.split('imeOnTask').length > 1 ? tbls[i].querySelectorAll('tr.tot-row') : tbls[i].querySelectorAll('tr.empl-all'); for ( var j = 0; j < rows.length-1; j++ ) { if (Math.floor(j/100) >= AAs.length) { AAs.push([]); }; AAs[Math.floor(j/100)].push(rows[j].children[is.id].innerText); }; if ( document.URL.split('imeOnTask').length == 1 ) { if (!rows[rows.length-1].children[0].classList.contains('herer')) { rows[rows.length-1].children[0].colSpan = rows[rows.length-1].children[0].colSpan + 2; rows[rows.length-1].children[0].classList.add('herer'); }; }; }; this.getLogins(AAs); } , getLogins : function (logins) { for ( let i = 0; i < logins.length; i++ ) { let apiUrl = 'https://adapt-iad.amazon.com/api/employee-profile-svc/GetEmployeeProfiles?employeeLogins=' + JSON.stringify(logins[i]); callApi(apiUrl, function(data) { functionRollup.loginToEmployeeId(data);}); }; } , loginToEmployeeId: function (data) { let keys = Object.keys(data); for ( let i = 0; i < keys.length; i++ ) { logins[keys[i]] = { login: data[keys[i]].login, badgeBarcode: data[keys[i]].badgeBarcodeId, shiftCode: data[keys[i]].shiftCode } ; }; this.show(); } }; function copyBadgeBarcode() { var is = document.URL.split('imeOnTask').length > 1 ? { id: 0, y: 2 } : { id: 1, y: 4 }; let id = this.parentElement.children[is.id].innerText; copyTextToClipboard(logins[id].badgeBarcode); if(!!document.getElementById("snackbar")) { showSnackBar(); } else { document.getElementsByClassName('fcpn-init')[0].insertAdjacentHTML("afterend", 'Successfully Copied'); showSnackBar(); }; }; function showSnackBar() { var sb = document.getElementById("snackbar"); //this is where the class name will be added & removed to activate the css sb.className = "show"; setTimeout(()=>{ sb.className = sb.className.replace("show", ""); }, 3000); } function fallbackCopyTextToClipboard(text) { var textArea = document.createElement("textarea"); textArea.value = text; // Avoid scrolling to bottom textArea.style.top = "0"; textArea.style.left = "0"; textArea.style.position = "fixed"; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { var successful = document.execCommand('copy'); var msg = successful ? 'successful' : 'unsuccessful'; console.log('Fallback: Copying text command was ' + msg); } catch (err) { console.error('Fallback: Oops, unable to copy', err); } document.body.removeChild(textArea); } function copyTextToClipboard(text) { if (!navigator.clipboard) { fallbackCopyTextToClipboard(text); return; } navigator.clipboard.writeText(text).then(function() { console.log('Async: Copying to clipboard was successful!'); }, function(err) { console.error('Async: Could not copy text: ', err); }); } function callApi(url,code) { GM_xmlhttpRequest({ method: "GET", url: url, onload: function (response) { let data = response.responseText; data = ( typeof data === 'object' ) ? data : JSON.parse(data); if (!!code) { code(data,url) }; } }); }; functionRollup.getEmployeeIds(); })();