hoshang / DG Counter - AAI

// ==UserScript==
// @name         DG Counter - AAI
// @namespace    N/A
// @version      0.2.1
// @description  Counter for AAI
// @author       jiankunw
// @updateURL    https://openuserjs.org/meta/hoshang/DG_Counter_-_AAI.meta.js
// @downloadURL  https://openuserjs.org/install/hoshang/DG_Counter_-_AAI.user.js
// @license      MIT
// @include      https://argus.aka.amazon.com/*
// @grant        GM.xmlHttpRequest
// @require      https://internal-cdn.amazon.com/btk.amazon.com/ajax/libs/jquery/2.1.4/jquery-2.1.4.min.js

// ==/UserScript==

let $ = window.jQuery;

if (/#!\/dg\//i.test(location.hash)) {
    console.log('start counter');
    let Algalon = new MutationObserver(argusMonitor);
    let targetNode = document;
    let config = { 'childList': true, 'subtree': true };
    Algalon.observe(targetNode, config);
}

function argusMonitor() {
    let asin = asinId();
    let login = getLogin();
    if (asin && login) {
        monitorClick(asin, login);
    }
}

function asinId() {
    let asinElem = $('#workItemContainer span')[0];
    return $(asinElem).text().trim();
}

function getLogin() {
    let loginElem = $('.spoof-user-badge-pic');
    return loginElem.attr('alt');
}

function monitorClick(asin, login) {

    // Submit Next    
    let btnNext = $('#dtSubmitAndNextButton');
    btnNext.unbind('click');
    btnNext.on('click', function () {
        // console.log('--monitored click submit next');
        apiConnect(asin, login, 'submit next new');
    });
    // Submit Exit
    let btnExit = $('#dtSubmitAndExitButton');
    btnExit.unbind('click');
    btnExit.on('click', function () {
        // console.log('--monitored click submit exit');
        apiConnect(asin, login, 'submit exit new');
    });
    // Sideline
    let btnSideline = $(getElementByXpath('//button[@class="confirm btn btn-lg btn-danger" and contains(text(), "Yes")]'));
    btnSideline.unbind('click');
    btnSideline.on('click', function () {
        // console.log('--monitored click sideline');
        apiConnect(asin, login, 'sideline');
    })
    // Acknowledge Next
    let ackNext = $('#dtAcknowledgeAndNextButton');
    ackNext.unbind('click');
    ackNext.on('click', function () {
        // console.log('--monitored click acknowledge next');
        apiConnect(asin, login, 'acknowledge next new');
    });
    // Acknowledge Exit
    let ackExit = $('#dtAcknowledgeAndExitButton');
    ackExit.unbind('click');
    ackExit.on('click', function () {
        // console.log('--monitored click acknowledge exit');
        apiConnect(asin, login, 'acknowledge exit new');
    });

    // monitor alert
    monitorAlert();

}

function monitorAlert() {
    let count = 0;
    setInterval(() => {
        count++;
        if (count > 1) {
            $('#argus-nav-brand').css({
                'color': 'white',
            });
            count = 0;
        } else {
            $('#argus-nav-brand').css({
                'color': '#E47911',
                '-webkit-transition-property:': 'color',
                '-webkit-transition-duration': '4s',
            });
        }
    }, 4000);
}

function apiConnect(asin, login, type) {
    console.log('--try to contact API');
    GM.xmlHttpRequest({
        mechod: 'GET',
        url: 'http://desktop-bl4brt4.ant.amazon.com:8080/api/dg-argus-asin-counter?'
            + 'asin=' + asin
            + '&login=' + login
            + '&type=' + type,
        onload: function (res) {
            console.log(res);
        }
    })
}

function getElementByXpath(path) {
    return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}