kalpdev.1 / Battery Word Counter

// ==UserScript==
// @name         Battery Word Counter
// @namespace    kalpdev
// @version      0.1
// @description  Provides a summary of Battery related words from Amazon Webpage
// @author       shjaisw
// @match        https://www.amazon.com/dp/*
// @match        https://www.amazon.in/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 = ' #f4f18d';
    zNode.style.textAlign = 'center';
    zNode.innerHTML = '<h2>Apex Words Counter</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.count = value;
        data_node.category = hazardous_words_map[data_node.word].category;
        data_node.priority = hazardous_words_map[data_node.word].priority;
        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": "Desktop",
            "category": "Product Bank",
            "priority": 24
        },
        {
            "word": "Motherboard",
            "category": "Product Bank",
            "priority": 25
        },
        {
            "word": "Quartz",
            "category": "Product Bank",
            "priority": 26
        },
        {
            "word": "UPS",
            "category": "Product Bank",
            "priority": 27
        },
        {
            "word": "Ride",
            "category": "Product Bank",
            "priority": 28
        },
        {
            "word": "iPhone",
            "category": "Product Bank",
            "priority": 29
        },
        {
            "word": "MacBook",
            "category": "Product Bank",
            "priority": 30
        },
        {
            "word": "iPod",
            "category": "Product Bank",
            "priority": 31
        },
        {
            "word": "case",
            "category": "Check Restrictive",
            "priority": 32
        },
        {
            "word": "Keyboard",
            "category": "Product Bank",
            "priority": 33
        },
        {
            "word": "Smartwatch",
            "category": "Product Bank",
            "priority": 34
        },
        {
            "word": "GPS",
            "category": "Product Bank",
            "priority": 35
        },
        {
            "word": "Vibrator",
            "category": "Product Bank",
            "priority": 36
        },
        {
            "word": "Tuner",
            "category": "Product Bank",
            "priority": 37
        },
        {
            "word": "Wand",
            "category": "Product Bank",
            "priority": 38
        },
        {
            "word": "Massager",
            "category": "Product Bank",
            "priority": 39
        },
        {
            "word": "Laptop",
            "category": "Product Bank",
            "priority": 40
        },
        {
            "word": "Body Wash",
            "category": "PTG",
            "priority": 41
        },
        {
            "word": "Keyfob",
            "category": "Product Bank",
            "priority": 42
        },
        {
            "word": "bank",
            "category": "Need Watt Hr",
            "priority": 43
        },
        {
            "word": "FITBIT",
            "category": "Product Bank",
            "priority": 44
        },
        {
            "word": "Bluetooth",
            "category": "Product Bank",
            "priority": 45
        },
         {
            "word": "poppers",
            "category": "N- Product bank",
            "priority": 46
        },
         {
            "word": "crackers",
            "category": "N- Product bank",
            "priority": 47
        },
         {
            "word": "pressure",
            "category": "N- Product bank",
            "priority": 48
        },
         {
            "word": "pre-charged",
            "category": "N- Product bank",
            "priority": 49
        },
         {
            "word": "Stove",
            "category": "N- Product bank",
            "priority": 50
        },
         {
            "word": "Fuel",
            "category": "N- Product bank",
            "priority": 51
        },
         {
            "word": "Dusters",
            "category": "N- Product bank",
            "priority": 52
        },
         {
            "word": "Camping",
            "category": "N- Product bank",
            "priority": 53
        },
         {
            "word": "Wipes",
            "category": "N- Product bank",
            "priority": 54
        },
         {
            "word": "Marker",
            "category": "N- Product bank",
            "priority": 55
        },
         {
            "word": "supplement",
            "category": "N- Product bank",
            "priority": 56
        },
         {
            "word": "extract",
            "category": "N- Product bank",
            "priority": 57
        },
         {
            "word": "Refrigerators",
            "category": "N- Product bank",
            "priority": 58
        },
         {
            "word": "freezers",
            "category": "N- Product bank",
            "priority": 59
        },
         {
            "word": "Refrigerating",
            "category": "N- Product bank",
            "priority": 60
        },
         {
            "word": "Bluetooth",
            "category": "N- Product bank",
            "priority": 61
        },
         {
            "word": "Deodorant",
            "category": "PTG",
            "priority": 62
        },
         {
            "word": "Shampoo",
            "category": "PTG",
            "priority": 63
        },
         {
            "word": "Hair",
            "category": "PTG",
            "priority": 64
        },
         {
            "word": "Leather Cleaner",
            "category": "PTG",
            "priority": 65
        },
         {
            "word": "Bandage",
            "category": "categoryA",
            "priority": 66
        },
         {
            "word": "Mosquito",
            "category": "PTG",
            "priority": 67
        },
         {
            "word": "Foam",
            "category": "PTG",
            "priority": 68
        },
         {
            "word": "Polish",
            "category": "PTG",
            "priority": 69
        },
         {
            "word": "Sun",
            "category": "PTG",
            "priority": 70
        },
         {
            "word": "Nasal",
            "category": "PTG",
            "priority": 71
        },
         {
            "word": "Repair",
            "category": "PTG",
            "priority": 72
        },
         {
            "word": "Spy",
            "category": "PTG",
            "priority": 73
        },
         {
            "word": "Nail",
            "category": "PTG",
            "priority": 74
        },
         {
            "word": "Strengthener",
            "category": "PTG",
            "priority": 75
        },
         {
            "word": "Cajeput",
            "category": "PTG",
            "priority": 76
        },
         {
            "word": "Galbanum",
            "category": "PTG",
            "priority": 77
        },
         {
            "word": "Tattoo",
            "category": "PTG",
            "priority": 78
        },
         {
            "word": "Perfume",
            "category": "PTG",
            "priority": 79
        },
         {
            "word": "Niaouli",
            "category": "PTG",
            "priority": 80
        },
         {
            "word": "Eue De",
            "category": "PTG",
            "priority": 81
        },
         {
            "word": "Eue da",
            "category": "PTG",
            "priority": 82
        },
         {
            "word": "EDT",
            "category": "PTG",
            "priority": 83
        },
         {
            "word": "Match",
            "category": "PTG",
            "priority": 84
        },
         {
            "word": "Developer",
            "category": "PTG",
            "priority": 85
        },
         {
            "word": "NICOTINE",
            "category": "PTG",
            "priority": 86
        },
         {
            "word": "Flint",
            "category": "PTG",
            "priority": 87
        },
         {
            "word": "Ferrocerium",
            "category": "PTG",
            "priority": 88
        },
         {
            "word": "Magnesium",
            "category": "PTG",
            "priority": 89
        },
         {
            "word": "dauber",
            "category": "PTG",
            "priority": 90
        },
         {
            "word": "dabber",
            "category": "PTG",
            "priority": 91
        },
         {
            "word": "Water",
            "category": "PTG",
            "priority": 92
        },
         {
            "word": "antiaging",
            "category": "PTG",
            "priority": 93
        },
         {
            "word": "Argan",
            "category": "PTG",
            "priority": 94
        },
        {
            "word": "Avocado",
            "category": "PTG",
            "priority": 95
        },
        {
            "word": "Basil",
            "category": "PTG",
            "priority": 96
        },
        {
            "word": "Soap",
            "category": "PTG",
            "priority": 97
        },
        {
            "word": "Bingo",
            "category": "PTG",
            "priority": 98
        },
        {
            "word": "CAPSULE",
            "category": "PTG",
            "priority": 99
        },
        {
            "word": "Detergent",
            "category": "PTG",
            "priority": 100
        },
        {
            "word": "Charcoal",
            "category": "PTG",
            "priority": 101
        },
        {
            "word": "Fabric",
            "category": "PTG",
            "priority": 102
        },
        {
            "word": "Textile",
            "category": "PTG",
            "priority": 103
        },
        {
            "word": "Cleanser",
            "category": "PTG",
            "priority": 104
        },
        {
            "word": "Foot",
            "category": "PTG",
            "priority": 105
        },
        {
            "word": "Foundation",
            "category": "PTG",
            "priority": 106
        },
        {
            "word": "Rocket",
            "category": "PTG",
            "priority": 107
        },
        {
            "word": "Top Coat",
            "category": "PTG",
            "priority": 108
        },
        {
            "word": "Base Coat",
            "category": "PTG",
            "priority": 109
        },
        {
            "word": "activater",
            "category": "PTG",
            "priority": 110
        },
        {
            "word": "Moisture",
            "category": "PTG",
            "priority": 111
        },

    ];
}