kalpdev.1 / Smart Argus Battery

// ==UserScript==
// @name     Smart Argus Battery
// @version     2.0
// @run-at   document-body
// @author    @shjaisw @ibrkanan
// @description  This script shows only relevent argusargus battery and pca blurbs
// @match    https://argus.aka.amazon.com/*
// @include  https://argus.aka.amazon.com/*
// @updateURL 
// @downloadURL 
// @license MIT
// @grant    GM_addStyle
// @grant    GM_xmlhttpRequest
// ==/UserScript==

function waitForKeyElements (
    selectorTxt,
    actionFunction,
    bWaitOnce,
    iframeSelector
) {
    var targetNodes, btargetsFound;

    if (typeof iframeSelector == "undefined")
        targetNodes     = $(selectorTxt);
    else
        targetNodes     = $(iframeSelector).contents ()
                                           .find (selectorTxt);

    if (targetNodes  &&  targetNodes.length > 0) {
        btargetsFound   = true;

        targetNodes.each ( function () {
            var jThis        = $(this);
            var alreadyFound = jThis.data ('alreadyFound')  ||  false;

            if (!alreadyFound) {

                var cancelFound     = actionFunction (jThis);
                if (cancelFound)
                    btargetsFound   = false;
                else
                    jThis.data ('alreadyFound', true);
            }
        } );
    }
    else {
        btargetsFound   = false;
    }


    var controlObj      = waitForKeyElements.controlObj  ||  {};
    var controlKey      = selectorTxt.replace (/[^\w]/g, "_");
    var timeControl     = controlObj [controlKey];


    if (btargetsFound  &&  bWaitOnce  &&  timeControl) {

        clearInterval (timeControl);
        delete controlObj [controlKey]
    }
    else {

        if ( ! timeControl) {
            timeControl = setInterval ( function () {
                    waitForKeyElements (    selectorTxt,
                                            actionFunction,
                                            bWaitOnce,
                                            iframeSelector
                                        );
                },
                300
            );
            controlObj [controlKey] = timeControl;
        }
    }
    waitForKeyElements.controlObj   = controlObj;
}

 const keyElementSelector = '#dtSubmitAndNextButton';
    const specificText = 'false';

    const getSecondElement = () => {
        const elements = document.querySelectorAll('.text-weight-300.md-subhead.details-list-item-text.md-truncate');
        return elements.length >= 2 ? elements[1] : null;
    };

    const waitForElement = (selector, callback) => {
        const intervalId = setInterval(() => {
            const element = document.querySelector(selector);
            if (element) {
                clearInterval(intervalId);
                callback(element);
            }
        }, 500);
    };

    const showModal = (message) => {
        // Create a modal element
        const modal = document.createElement('div');
        modal.classList.add('modal');
        modal.innerHTML = `
            <div class="modal-content">
                <span class="close">&times;</span>
                <p>${message}</p>
            </div>
        `;
        document.body.appendChild(modal);

        // Close the modal when the close button is clicked
        const closeButton = modal.querySelector('.close');
        closeButton.addEventListener('click', () => {
            modal.style.display = 'none';
        });

        // Show the modal
        modal.style.display = 'block';
    };

    const checkForElement = () => {
        const secondElement = getSecondElement();
        if (secondElement && secondElement.textContent.includes(specificText)) {
            // Show modal instead of console log
            showModal('Double check the product Quantity (PCA False)');
        }
    };

    waitForElement(keyElementSelector, (keyElement) => {
        checkForElement();

        // Set an interval to check for the element every 10 seconds
        setInterval(() => {
            checkForElement();
        }, 10000);
    });

function addCustomSearchResult(jNode) {
    //$(".submit-product-card-classification").hide ();
    $('#dt-select-100-product-powered').click(function() {
        //$('md-tab-item[aria-label="Extended battery attributes from the catalog"]').trigger('click');
        setTimeout(function() {
            add_description();
        }, 100);
        function add_description() {
            //alert("Hey");
            //console.log("Heyyyyyyy");
            $('md-tab-item[aria-label="Extended battery attributes from the catalog"]').trigger('click');
            setTimeout(function() {
                var n = attribute();
                console.log("Str is : " + n);
                var keyword = "American";
                if (n.includes("lithium_polymer") == true || n.includes("lithium_ion") || n.includes("lithium_metal") == true || n.includes("lithium_manganese_dioxide") || n.includes("lithium") == true || n.includes("cr2") || n.includes("cr123a") == true || n.includes("cr123") || n.includes("LiMnO2") == true || n.includes("batteries_packed_with_equipment") || n.includes("batteries_contained_in_equipment") == true || n.includes("batteries_only")) {
                    // alert('Please Check Battery Attributes');
                    document.querySelector('#dt-list-select-400-alkaline').style.display = "none";
                    document.querySelector('#dt-list-select-400-ni-mh').style.display = "none";
                    document.querySelector('#dt-list-select-400-ni-cd').style.display = "none";
                    document.querySelector('#dt-list-select-300-alkaline').style.display = "none";
                    document.querySelector('#dt-list-select-300-ni-mh').style.display = "none";
                    document.querySelector('#dt-list-select-300-ni-cd').style.display = "none";
                    document.querySelector('#dt-list-select-400-unknown').style.display = "none";
                }

            }, 5);
            function attribute() {
                var active_data7 = $('.ng-scope').text();
                //console.log("Battery string is : " + active_data7);
                return (active_data7);
            }

            ////var active_data = $('.ng-isolate-scope').text();
            //console.log("Active string is : " + active_data);
            ////var batt = $('.md-dg-theme').text();
            ////console.log("Bat Attri is : " + batt);
        }
        //alert("Hey");
        //console.log("Heyyyyyyy");
    });
}
var url = window.location.href;
var isArgusDG = /^https:\/\/argus.aka.amazon.com\/#!\/dg(.*)/i.test(url);
if (isArgusDG) {
    console.log("smart battery active");
    waitForKeyElements("#dt-select-100-product-powered", addCustomSearchResult);
} else {
    console.log("smart battery not active");
}