kalpdev.1 / Job History phonetool amazon

// ==UserScript==
// @name            Job History phonetool amazon
// @description     Add Job History and Job Data into PhoneTool
// @author          shjaisw@
// @namespace       https://w.amazon.com/index.php/EnvImprovementNinjas/JobHistory
// @version         20221215
// @match           https://phonetool.amazon.com/people/*
// @match           https://phonetool.amazon.com/users/*
// @match           https://connect.amazon.com/people/*
// @match           https://connect.amazon.com/users/*
// @license MIT
// @updateURL https://openuserjs.org/meta/kalpdev.1/Job_History_phonetool_amazon.meta.js
// @downloadURL https://openuserjs.org/install/kalpdev.1/Job_History_phonetool_amazon.user.js

// @match           https://admindirectory.corp.amazon.com/users/*
// @match           https://admindirectory.corp.amazon.com/people/*
// @require         https://m.media-amazon.com/images/I/41aUf1WGWNL./handlebars.runtime_with_templates.min-v4.7.7.js#sha256=480441feaaf3810569c4f629d9bdca7ca10975fd30f53a7a3a1ae4cb04c3187d
// @require         https://m.media-amazon.com/images/I/61BHMfGpmbL./jquery-1.12.4.min.js#sha256=668b046d12db350ccba6728890476b3efee53b2f42dbb84743e5e9f1ae0cc404
// @require         https://m.media-amazon.com/images/I/412BflKV--L./openid.xhr.min.js#sha256=8a17dfbab69c3482e6308f5083cb5eb940a66d5baa1fc10b42953578549d7de6
// ==/UserScript==
// Note: The version string must be formatted YYYYMMDD to help Greasemonkey's version comparison logic

(function($, Handlebars) {
    'use strict';

    var fetchJobData = function(user) {
        return $.ajax({
            method: "GET",
            url: 'https://phonetool.amazon.com/users/' + user + '.json',
            xhrFields: {
                withCredentials: true
            },
            dataType: 'json'
        });
    };

    var fetchJobHistory = function(user) {
        return $.ajax({
            method: "GET",
            url: 'https://ekarulf.corp.amazon.com/cors/job-history/',
            data: {
                login: user
            },
            xhrFields: {
                withCredentials: true
            },
            dataType: 'json'
        });
    };

    var renderJobHistory = function(jobHistory) {
        var template = Handlebars.templates["job-history.html"];
        var viewModel = {};

        if ($.isArray(jobHistory) && jobHistory.length > 0) {
            viewModel.history = jobHistory;
        }

        return template(viewModel);
    };

    var renderJobData = function(jobData) {
        var template = Handlebars.templates["job-data.html"];
        var viewModel = {};

        if (typeof jobData.job_level === 'number') {
            viewModel.job_level = jobData.job_level;
        }

        if (typeof jobData.total_tenure_formatted === 'string') {
            viewModel.total_tenure_formatted = jobData.total_tenure_formatted;
        }

        return template(viewModel);
    };

    // Main function
    var user = $('.login').text().trim();

    // Patch to fix Firefox returning no data
    if (user.length == 0) {
        user = document.location.href.substring(document.location.href.lastIndexOf('/')+1, document.location.href.length);
    }

    $.when(fetchJobHistory(user)).done(function(jobHistory) {
        // Insert Job History after org chart in DOM
        var jobHistoryHtml = renderJobHistory(jobHistory);
        $('.well.org-chart').after(jobHistoryHtml);
    });

    $.when(fetchJobData(user)).done(function(jobData) {
        // Insert job data after employee id
        var jobDataHtml = renderJobData(jobData);
        $('.UserDetailsTable').append(jobDataHtml);
    });
}(jQuery.noConflict(true), Handlebars));