v.maliy / tracker defects

// ==UserScript==
// @name         tracker defects
// @namespace    systech/tracker
// @version      0.5
// @description  add task from tfs to tracker
// @author       MT
// @match        https://tfssrv.systtech.ru/tfs/*
// @grant        none
// @require https://raw.githubusercontent.com/uzairfarooq/arrive/master/minified/arrive.min.js
// @copyright 2020, semapv (https://openuserjs.org/users/semapv)
// @license MIT
// ==/UserScript==

function getFormattedTodayDate() {
    const today = new Date().toLocaleDateString('ru');

    return today.split('.').reverse().join('-');
}

function getFormattedCurrentTime() {
    const currentTime = new Date().toLocaleTimeString('ru', { hour: '2-digit', minute: '2-digit' });

    return encodeURIComponent(currentTime);
}

function postDataToTracker(url, taskId) {
    const formattedTodayDate = getFormattedTodayDate();
    const trackerUrl = `${url}?date=${formattedTodayDate}`;
    const formattedCurrentTime = getFormattedCurrentTime();

    return fetch(trackerUrl, {
        "headers": {
            "accept": "*/*",
            "accept-language": "ru,en-US;q=0.9,en;q=0.8",
            "content-type": "application/x-www-form-urlencoded",
            "sec-fetch-mode": "cors"
        },
        "referrer": trackerUrl,
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": `Id=0&Date=${formattedTodayDate}&Begin=${formattedCurrentTime}&End=00%3A00&TaskId=${taskId}&Comment=`,
        "method": "POST",
        "mode": "no-cors",
        "credentials": "include"
    });

}

function processResponse(response, taskId) {
    if (response.status === 200) {
        alert(`Задача добавлена ${taskId}`);
    } else {
        alert('Произошла ошибка, попробуйте позже');
    }
}

function addTaskToTracker() {
    const taskId = document.querySelector('.work-item-form-id span').textContent;

    postDataToTracker('https://tfssrv.systtech.ru/tracker/Home/Edit', taskId)
        .then((res) => processResponse(res, taskId))
        .catch((err) => alert('Произошла ошибка, попробуйте позже'));
}

function createButton(text) {
    const button = document.createElement('button');

    button.classList.add('tracker-btn');
    button.textContent = text;
    button.style.marginRight = '5px';
    button.style.height = '26px';
    button.style.backgroundColor = '#008CBA';
    button.style.color = "#fff";
    button.onclick = addTaskToTracker;

    return button;
}

function addButton(containerTag, text) {
    if (!document.querySelector('.tracker-btn')) {
        const infoNode = document.querySelector(containerTag);
        const button = createButton(text);

        infoNode.append(button);
    }
}

(function() {
    'use strict';

    var buttonsContainerClass = '.work-item-form-toolbar-container';

    document.arrive(buttonsContainerClass, () => addButton(buttonsContainerClass, 'Добавить в трекер'));
})();