Raw Source
ashubhamjaiswal / VIMENTO 2- Virtual Guide

// ==UserScript==
// @name         VIMENTO 2- Virtual Guide
// @namespace    kalpdev
// @version      0.5
// @description  Provides a summary from Amazon Webpage
// @author       shjaisw
// @match        https://www.amazon.co.uk/dp/*
// @match        https://www.amazon.com/dp/*
// @match        https://www.amazon.in/dp/*
// @match        https://www.amazon.ca/dp/*
// @match        https://www.amazon.ae/dp/*
// @updateURL https://openuserjs.org/meta/kalpdev.1/VIMENTO_2-_Virtual_Guide.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

// ==/UserScript==

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

    var zNode = document.createElement('div');
    zNode.style.backgroundColor = ' #ebdef2';
    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);
    $('#wayfinding-breadcrumbs_container').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": [",", ":", ".","-","s"]
        },
        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": "Builtin",
            "Reference": "Available",
            "priority": 13
        },
        {
            "Keyword": "Whats in the box",
            "Reference": "Available",
            "priority": 14
        },
        {
            "Keyword": "What is in the box?",
            "Reference": "Available",
            "priority": 113
        },
         {
            "Keyword": "Spillable",
            "Reference": "Check Details",
            "priority": 114
        },
        {
            "Keyword": "Charging box",
            "Reference": "Check Restrictive details",
            "priority": 115
        },
        {
            "Keyword": "charging case",
            "Reference": "Check Restrictive details",
            "priority": 116
        },
        {
            "Keyword": "Pakcage Include",
            "Reference": "Check Restrictive details",
            "priority": 117
        },
        {
            "Keyword": "INGREDIENTS",
            "Reference": "Check IBC",
            "priority": 165
        },
        {
            "Keyword": "alkaline",
            "Reference": "Check Battery details",
            "priority": 1600
        },
        {
            "Keyword": "nimh",
            "Reference": "Check Battery details",
            "priority": 16080
        },
        {
            "Keyword": "nicd",
            "Reference": "Check Battery details",
            "priority": 1400
        },
        {
            "Keyword": "lr44",
            "Reference": "Check IBC",
            "priority": 1650
        },
        {
            "Keyword": "inbuilt",
            "Reference": "Check battery details",
            "priority": 132
        },
        {
            "Keyword": "Package include",
            "Reference": "Check battery details",
            "priority": 193
        },
         {
            "Keyword": "Lithium",
            "Reference": "Please check Battery Details",
            "priority": 14
        },
        {
            "Keyword": "cr2032",
            "Reference": "Please check Battery Details",
            "priority": 14
        },
        {
            "Keyword": "built-in",
            "Reference": "Check battery details",
            "priority": 132
        },
        {
            "Keyword": "kit includes",
            "Reference": "Check package details",
            "priority": 187
        },
         {
            "Keyword": "Included",
            "Reference": "Check package details",
            "priority": 187
        },
        {
            "Keyword": "Cleaner Kit",
            "Reference": "Check package details",
            "priority": 130
        },
        {
            "Keyword": "Cleaning Kit",
            "Reference": "Check package details",
            "priority": 132
        },
        {
            "Keyword": "in the box",
            "Reference": "Check package details",
            "priority": 132
        },
        {
            "Keyword": "remote",
            "Reference": "Check package details",
            "priority": 132
        },
        {
            "Keyword": "Rechargeable",
            "Reference": "Check battery details",
            "priority": 132
        },
        {
            "Keyword": "Packages",
            "Reference": "Check battery details",
            "priority": 132
        },
        {
            "Keyword": "AA Battery (Not included)",
            "Reference": "Check battery details",
            "priority": 138
        },




    ];
}