Raw Source
kalpdev.1 / Apex Word Counter

// ==UserScript==
// @name         Apex Word Counter
// @namespace    kalpdev
// @version      0.1
// @description  Provides a summary of Battery related words from Amazon Webpage
// @author       shjaisw
// @match        https://*/dp/*
// @updateURL https://openuserjs.org/meta/kalpdev.1/Battery_Word_Counter.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
// @license MIT
// @copyright  kalpdev

// ==/UserScript==

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

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

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

    let table_content = generate_table_content();

    buildHtmlTable(table_content, $('#hazwordsContainerTable'));
    $('#hazwordsContainerTable').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.word.toLowerCase()] = { category: obj.category, priority : obj.priority };
        return map;
    }, {});

   $.each(marked_hazardous_words, function(key, value) {
        let data_node = {};
        data_node.word = key;
        data_node.category = hazardous_words_map[data_node.word].category;
        
        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.word);
    });

    return hazardous_words;
}

function get_hazardous_words_config() {

    return [
        {
            "word": "lithium",
            "category": "Battery",
            "priority": 1
        },
        {
            "word": "Textile",
            "category": "PTG",
            "priority": 2
        },
        {
            "word": "18650",
            "category": "categoryA",
            "priority": 3
        },
        {
            "word": "metal",
            "category": "Battery",
            "priority": 4
        },
        {
            "word": "gas spring",
            "category": "PTG",
            "priority": 5
        },
        {
            "word": "Built",
            "category": "package",
            "priority": 6
        },
        {
            "word": "Recharge",
            "category": "Battery",
            "priority": 7
        },
        {
            "word": "Shampoo",
            "category": "PTG",
            "priority": 8
        },
        {
            "word": "AC",
            "category": "categoryA",
            "priority": 9
        },
        {
            "word": "dabber",
            "category": "PTG",
            "priority": 10
        },
        {
            "word": "acetic-acid",
            "category": "categoryA",
            "priority": 11
        },
        {
            "word": "Quartz",
            "category": "PTG",
            "priority": 12
        },
        {
            "word": "Acoustic guitars",
            "category": "categoryA",
            "priority": 13
        },
        {
            "word": "Acoustic-guitars",
            "category": "categoryA",
            "priority": 14
        },
        {
            "word": "Aerosol",
            "category": "categoryA",
            "priority": 15
        },
        {
            "word": "AG10",
            "category": "NH",
            "priority": 16
        },
        {
            "word": "AG13",
            "category": "NH",
            "priority": 17
        },
        {
            "word": "AGM",
            "category": "categoryA",
            "priority": 18
        },
        {
            "word": "AGM battery",
            "category": "categoryA",
            "priority": 19
        },
        {
            "word": "AGM-battery",
            "category": "categoryA",
            "priority": 20
        },
        {
            "word": "rechargable",
            "category": "categoryA",
            "priority": 21
        },
        
    ];
}