floodmeadows / DTX project shortcuts

// ==UserScript==
// @name         DTX project shortcuts
// @description  Book your time faster using a list of frequently used projects and time entries
// @namespace    https://openuserjs.org/users/floodmeadows
// @author       andy@floodmeadows.com
// @copyright    2021, floodmeadows (https://openuserjs.org/users/floodmeadows)
// @license      MIT
// @version      0.2
// @match        https://missbhadtx03.corp.capgemini.com/DTX.NET/item.aspx*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=capgemini.com
// @updateURL    https://openuserjs.org/meta/floodmeadows/DTX_project_shortcuts.meta.js
// @downloadURL  https://openuserjs.org/install/floodmeadows/DTX_project_shortcuts.user.js
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */

var projects = [
/* Template
    {
        name: "", // Friendly name to show on the shortcut button
        projectCode: "",
        taskNumber: "",
        businessReason: ""
    },
*/
    {
        name: "My main project",
        projectCode: "123456789",
        taskNumber: "ABC.123.4",
        businessReason: "My project description"
    },
    {
        name: "Holiday",
        projectCode: "HOL00",
        taskNumber: "1",
        businessReason: "Annual leave"
    },
    {
        name: "Sickness",
        projectCode: "SCK00",
        taskNumber: "1",
        businessReason: ""
    },
    {
        name: "Medical appointments",
        projectCode: "SCK10",
        taskNumber: "1",
        businessReason: ""
    },
];

(function() {
    'use strict';

    addShortcutsContainer();
    projects.forEach(p => addButton(p));
})();

function addShortcutsContainer() {
    const textNode = document.createTextNode('Project shortcuts:');

    const label = document.createElement("p");
    label.setAttribute("style","margin:0; padding:0;");
    label.setAttribute("class","aspLabelCharacterMedium");
    label.append(textNode);

    const d = document.createElement("div");
    d.setAttribute("id","shortcuts-container");
    d.setAttribute("style","padding:2px; margin-bottom:8px;");
    d.append(label);

    const target = document.getElementById('lblProjectCaption').parentElement.parentElement.parentElement.parentElement.parentElement;
    target.prepend(d);
}

function addButton(project) {
    const textNode = document.createTextNode(project.name);

    const b = document.createElement("button");
    b.setAttribute("type","button");
    b.setAttribute("style","margin:0.3em 0.3em 0 0");
    b.addEventListener("click", function() {
        document.getElementById("drpProjectCode_input").value = project.projectCode;
        // Then we need to fake going into the text field and back out again, to trigger some other code elsewhere on the page that takes the text field value and applies it to another JS variable.
        document.getElementById("drpProjectCode_input").focus();
        document.getElementById("drpProjectCode_input").blur();

        document.getElementById("txtTaskNumber").value = project.taskNumber;
        document.getElementById("txtComments").value = project.businessReason;
    }, false);
    b.append(textNode);

    const target = document.getElementById('shortcuts-container');
    target.append(b);
}