kalpdev.1 / Escalation Alert Guide_DG_v1

// ==UserScript==
// @name         Escalation Alert Guide_DG_v1
// @namespace    kalpdev
// @version      1.05
// @description  Provides the information about product if similar Asin has Escalation.
// @author       shjaisw
// @match        https://*/dp/*
// @match        https://*/gp/*
// @run-at document-body
// @downloadURL https://openuserjs.org/install/kalpdev.1/Escalation_Alert_Guide_DG_v1.user.js
// @updateURL https://openuserjs.org/meta/kalpdev.1/Escalation_Alert_Guide_DG_v1.meta.js
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/jquery.mark.es6.min.js
// @require      https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js
// @grant        GM_addStyle
// @license MIT

// ==/UserScript==

$(window).bind("load", function() {
    'use strict';

    var zNode = document.createElement('div');
    zNode.style.backgroundColor = ' #f73c7d';
    zNode.style.textAlign = 'center';
    zNode.innerHTML = '<h2> </h2>';
    zNode.setAttribute('id', 'hazwordsContainers');
    var tableNode = document.createElement('table');
    tableNode.setAttribute('id', 'hazwordsContainerTables');
    tableNode.setAttribute('border', 1);

    zNode.appendChild(tableNode);
    $('#title_feature_div.celwidget').append(zNode);
    $('#hazwordsContainer h2').click(function() {
        $('#hazwordsContainerTable_wrapper').toggle('slow');
    });

    let table_content = generate_table_content();

    buildHtmlTable(table_content, $('#hazwordsContainerTables'));
    $('#hazwordsContainerTables').DataTable({
        "aaSorting": [[3,'desc']]
    });

    $("head").append (
        '<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css">'
    );
});

function buildHtmlTable(myList, elementToAppend) {
    var columns = addAllColumnHeaders(myList, elementToAppend);

    var tbodyElement = $('<tbody/>');
    for (var i = 0 ; i < myList.length ; i++) {
        var row$ = $('<tr/>');
        for (var colIndex = 0 ; colIndex < columns.length ; colIndex++) {
            var cellValue = myList[i][columns[colIndex]];

            if (cellValue == null) { cellValue = ""; }

            row$.append($('<td/>').html(cellValue));
        }
        tbodyElement.append(row$);
    }
    elementToAppend.append(tbodyElement);
}


function addAllColumnHeaders(myList, elementToAppend)
{
    var columnSet = [];
    var thElement = $('<thead/>');
    var headerTr$ = $('<tr/>');

    console.log(myList);
    for (var i = 0 ; i < myList.length ; i++) {
        var rowHash = myList[i];
        for (var key in rowHash) {
            if ($.inArray(key, columnSet) == -1){
                columnSet.push(key);
                headerTr$.append($('<th/>').html(key));
            }
        }
    }
    thElement.append(headerTr$);
    elementToAppend.append(thElement);
    return columnSet;
}

function generate_table_content() {

    let marked_hazardous_words = mark_hazardous_words_on_page();
    let hazardous_words_config = get_hazardous_words_config();

    let data_to_load = [];
    var hazardous_words_map = hazardous_words_config.reduce(function(map, obj) {
        map[obj.Keyword.toLowerCase()] = { Reference: obj.Reference, priority : obj.priority };
        return map;
    }, {});

   $.each(marked_hazardous_words, function(key, value) {
        let data_node = {};
        data_node.Keyword = key;

        data_node.Reference = hazardous_words_map[data_node.Keyword].Reference;

        data_to_load.push(data_node);
    });
    return data_to_load;
}

function mark_hazardous_words_on_page() {

    let just_hazardous_words_from_config = get_just_hazardous_words_from_config();
    let hazardous_words_on_page = {};

    console.log(Date.now());

    $('#dp').mark(just_hazardous_words_from_config, {
        filter: function(text_node, keyword, total_counter, counter) {
            let lower_case_keyword = keyword.toLowerCase();
            hazardous_words_on_page[lower_case_keyword] = parseInt(counter + 1);

            return true;
        },
        accuracy: {
            "value": "exactly",
            "limiters": [",", ":", "."]
        },
        separateWordSearch: false
    });

    console.log(Date.now());

    return hazardous_words_on_page
}

function get_just_hazardous_words_from_config() {
    let hazardous_words_from_config = get_hazardous_words_config();
    let hazardous_words = [];

    $.each(hazardous_words_from_config, function(k, v) {
        hazardous_words.push(v.Keyword);
    });

    return hazardous_words;
}

function get_hazardous_words_config() {

    return [
        {
            "Keyword": "Copra",
            "Reference": "For dried halves of coconut, rings of coconut, powder, shredded/ desiccated coconut - ignore any SDSs, ex sheets declaring the ASIN as non-DG and mark it using appropriate UTC code with the comment: Do not change the classification to non-dg . Please consult respective node SME",
            "priority": 2
        },
        {
            "Keyword": "Kopra",
            "Reference": "For dried halves of coconut, rings of coconut, powder, shredded/ desiccated coconut - ignore any SDSs, ex sheets declaring the ASIN as non-DG and mark it using appropriate UTC code with the comment: Do not change the classification to non-dg . Please consult respective node SME",
            "priority": 3
        },
        

    ];
}