<commit_before>// ==UserScript==
// @name          coinc_amazon_discount_show
// @namespace     danibaena
// @description   Script to add Coinc Discount Price next to the original price
// @include       http://www.amazon.es/*
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require       https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant         none
// ==/UserScript==

function addCoincPrice () {

    var priceId = "priceblock_ourprice";
    var priceDealId = "priceblock_dealprice";
    var priceSaleId = "priceblock_saleprice";
  
    if(document.getElementById(priceId)!==null) {
       var itemPrice = document.getElementById(priceId);    
    } else {
        if(document.getElementById(priceDealId)!==null) { 
           var itemPrice = document.getElementById(priceDealId);
        } else {
            var itemPrice = document.getElementById(priceSaleId);
        }
      
    }
    
    var str = itemPrice.innerHTML;

    if( ( str !== "No disponible." ) && ( str.indexOf("-") === -1 ) ){
        var res = str.replace("EUR", "").trim().replace(".","").replace(",",".");
        res = parseFloat(res);  
        coincPrice = (res/1.04).toFixed(2).toString().replace(".",",").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");

        var coincPriceTextSpan = document.createElement("span");
        coincPriceTextSpan.setAttribute("class", "a-color-secondary a-size-base a-text-left");
        var text = document.createTextNode("Precio Coinc:")
        coincPriceTextSpan.appendChild(text);
        coincPriceTextSpan.style.verticalAlign = "top";
        coincPriceTextSpan.style.padding = "6px 6px";

        var coincPriceSpan = document.createElement("span");
        coincPriceSpan.setAttribute("class", "a-size-medium a-color-price");
        coincPriceSpan.setAttribute("id", "coinc_price");
        var text2 = document.createTextNode("EUR " + coincPrice);
        coincPriceSpan.appendChild(text2);
        
        itemPrice.appendChild(coincPriceTextSpan);
        itemPrice.appendChild(coincPriceSpan);
    }
}

waitForKeyElements ("#priceblock_ourprice", addCoincPrice, false);
waitForKeyElements ("#priceblock_dealprice", addCoincPrice, false);
waitForKeyElements ("#priceblock_saleprice", addCoincPrice, false);<commit_msg>Refactor discount show script<commit_after>// ==UserScript==
// @name          coinc_amazon_discount_show
// @namespace     danibaena
// @description   Script to add Coinc Discount Price next to the original price
// @include       http://www.amazon.es/*
// @include       https://www.amazon.es/*
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require       https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant         none
// ==/UserScript==

function addCoincPrice() {
    var parser = new DOMParser();
    var itemPriceIds = [
        "priceblock_ourprice",
        "priceblock_dealprice",
        "priceblock_saleprice",
    ];

    var itemPrice = itemPriceIds.map(function(currentValue, index) {
        return document.getElementById(currentValue);
    }).filter(function(item) {
        return ((item !== null) && (item.innerHTML !== "No disponible.") && (item.innerHTML.indexOf("-") === -1));
    }).reduce(function(previousValue, currentValue) {
        if(previousValue !== null) {
            return (getPrice(previousValue) < getPrice(currentValue)) ? previousValue : currentValue;
        } else {
            return currentValue;
        }
    });

    var res = getPrice(itemPrice);
    var coincPrice = (res/1.04).toFixed(2).toString().replace(".",",").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");

    var coincPriceTextSpan = '<span class="a-color-secondary a-size-base a-text-left" style="vertical-align: top; padding: 6px;">Precio Coinc:</span>';
    var coincPriceSpan = '<span class="a-size-medium a-color-price" id="coinc_price">EUR ' + coincPrice + '</span>';

    $(itemPrice).append(coincPriceTextSpan);
    $(itemPrice).append(coincPriceSpan);
}

function getPrice(itemPrice) {
    return parseFloat(itemPrice.innerHTML.replace("EUR", "").trim().replace(".","").replace(",","."));
}

waitForKeyElements ("#priceblock_ourprice", addCoincPrice, false);
waitForKeyElements ("#priceblock_dealprice", addCoincPrice, false);
waitForKeyElements ("#priceblock_saleprice", addCoincPrice, false);
{'fraction_non_alphanumeric': 0.1639569498511564, 'fraction_numerical': 0.008930615983512708, 'mean_word_length': 3.346268656716418, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6803757', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1315, 'n_words': 227}