jess.a.manngmail.com / Filter AMZRT

// ==UserScript==
// @name         Filter AMZRT
// @namespace    http://jess-mann.com
// @version      1.6
// @description  Filter results from Amazon Review Trader (AMZRT). This hides discounts that are not very good, and makes discounts that are OKAY (but not great) less eye-catching. It also hides offers for supplements and diet pills. Offers that are completely free are highlighted in blue.
// @author       Jess Mann, jess.a.mann@gmail.com
// @copyright    2016, Jess Mann, http://jess-mann.com
// @match        https://www.amzreviewtrader.com/
// @match        https://www.amzreviewtrader.com/product-list.php
// @match        https://www.amzonereviews.com/review-product-list.php
// @match        http://www.ireviewhome.com/index.html
// @match        http://www.ireviewhome.com/product-list.html
// @match        https://www.amzreviews.co.uk/
// @match        https://www.amzreviews.co.uk/product-list.php
// @grant        none
// @require      https://raw.githubusercontent.com/uzairfarooq/arrive/master/minified/arrive.min.js
// ==/UserScript==

(function() {
    'use strict';

    /**
     * This function hides, fades, and highlights offers when they meet the provided thresholds.
     */
    $.fn.amplify = function( percent ) {
        // Hide offers below a 70% discount. Change the 70 to any other number if you want a different percentage.
        var percent_to_hide = 70;
        // Fade offers (make them transparent) below a 90% discount. Change the 90 to any other number if you'd like.
        var percent_to_fade = 90;

        /** Check the thresholds, and use classes to fade/hide/highglight offers */
        //hide
        if (percent <= percent_to_hide)
        {
            $(this).closest('.box').addClass('discount_hidden');
        }
        //fade
        else if (percent <= percent_to_fade)
        {
            //console.log('fading at '+percent);
            var fade = ((percent / 100) - 0.6) * 1.8;
            $(this).closest('.box').addClass('discount_fade').css('opacity',fade);
        }
        //highlight
        else if (percent > 98)
        {
            $(this).closest('.box').addClass('discount_free');
        }
        return $(this);
    };

    //Add hidden text to help debugging problems (if they arise).
    $.fn.hiddenText = function( price, shipping, percent ) {
        $(this).find('.discount').after('<p class="hidden_text">$: '+price+', ship: '+shipping+', %: '+percent+'</p>');
        return $(this);
    };

    $(document).ready(function() {
        /** Append a few css rules for styling */
        $("<style type='text/css'> #floating-button, .product-expiry { display: none; } .my_discount { color: red; } .box .price-box { height: 116px; } .box { background: #FFF } .discount_free { background: #EEF } .discount_fade { opacity: 0.1; } .discount_fade:hover { opacity: 1 !important; } .discount_was_hidden, .supplement_was_hidden { opacity: 0.3; background: #eee; } .discount_was_hidden:hover, .supplement_was_hidden:hover { opacity: 0.8; } .discount_hidden, .discount_supplement { opacity: 0.2; } .discount_hidden::after, .discount_supplement::after { display: block; content: ''; border: 1px solid #666; background: #0063a6; background: #99A; width: 100%; height: 100%; opacity: 1; position: absolute; top: 0; left: 0; right: 0; bottom: 0; } #show-all, #hide-all { background-color: #666; color: #fff; top: -1px; position: relative; margin-left: 10px; } #show-all:hover, #hide-all:hover { opacity: 0.8; } #hide-all { display: none; } .hidden_text { display: none; } </style>").appendTo("head");

        /**
         * When the button at the bottom of the page loads in, run our code on the rest of the page.
         * It is important we run this code every time the bottom button loads, because the AMZRT page is refreshed with AJAX.
         */
        $('#box-container').arrive('#btn-next-page-large', function() {
            //Add a "hide" and "show" button to the search bar.
            $('#filters button.btn-warning, #filters button.btn-primary').after('<button type="button" id="show-all" class="btn" style="">Show All</button><button type="button" id="hide-all" class="btn" style="">Hide All</button>');

            //Show/hide button functionality.
            $('#show-all').click(function(e) {
                e.preventDefault();
                $('.discount_hidden').addClass('discount_was_hidden').removeClass('discount_hidden');
                $('.discount_supplement').addClass('supplement_was_hidden').removeClass('discount_supplement');
                $(this).hide();
                $('#hide-all').show();
            });
            $('#hide-all').click(function(e) {
                e.preventDefault();
                $('.discount_was_hidden').addClass('discount_hidden').removeClass('discount_was_hidden');
                $('.supplement_was_hidden').removeClass('supplement_was_hidden').addClass('discount_supplement');
                $(this).hide();
                $('#show-all').show();
            });

            //Iterate over each offer, and run amplify if appropriate.
            $('#box-container .price-box').each(function() {

                //Get the original discount, price, and shipping text values, for processing by regex later.
                var oDiscount = $(this).find('.discount').text();
                var oPrice = $(this).find('.price').text();
                var oShipping = $(this).find('.shipping').text();

                //Set our percentage discount to 0, until we discover what it really is later.
                var percent = 0;

                //Find the shipping amount. Do this first, because AMZRT doesn't include it in their percentage off
                reg = /[+]\s*[£$]\s*([\d\.]+)\s*shipping/gi;
                var shipMatch = reg.exec(oShipping);
                var shipping = 0;

                //If we found a shipping price.
                if (shipMatch !== null)
                {
                    shipping = parseFloat(shipMatch[1]);
                    $(this).addClass('discount_shipping');
                }
                //console.log('Shipping is '+shipping+' from '+shipMatch);

                //The asking price (after the discount)
                reg = /[£$]\s*([\d\.]+)/gi;
                var priceMatch = reg.exec(oPrice);
                //Start with shipping... if it's free, then it's the shipping cost.
                var price = shipping;
                //console.log(priceMatch);
                
                //If we did not find a dollar amount...
                if (priceMatch === null)
                {
                    //Check if the price is listed as free.
                    reg = /FREE/gi;
                    var free = oPrice.search(reg);
                    
                    //If not, then I'm not sure WHAT we have. This should never happen.
                    if (free == -1)
                    {
                        console.log('I\'m not able to find an asking price: '+price);
                        $(this).hiddenText(price+'?',shipping,off);
                        return;
                    }
                    
                    //The price was free, so we already set it to the shipping. Good to go!
                }
                else
                {
                    //Set the price, which is the asking price, plus shipping.
                    price = parseFloat(priceMatch[1]) + shipping;
                }

                //Find all percentage discounts
                var reg = /([\d\.]+)\s*%\s*off/gi;
                var match = reg.exec(oDiscount);

                //If we found a percentage discount, we recalculate it with shipping, then run amplify and move on.
                if (match !== null)
                {
                    //Round the percent down. 50.01% should read 50%.
                    percent = Math.floor(match[1]);

                    //If there is a shipping cost, recalculate the percentage (AMZRT lies)
                    if (shipping > 0)
                    {
                        //Find the original price. Don't include shipping, because AMZRT doesn't.
                        var original_price = (price - shipping) / (1 - (match[1] / 100));
			            //calculate a new percentage, including shipping
			            percent = ( price / (original_price + shipping) ) * 100;
			            //Round again
			            percent = Math.floor(percent);
                    	$(this).hiddenText('O:'+original_price,shipping,percent);
                    }

		            //Add the (corrected) percentage at the bottom of the offer, and amplify
                    $(this).find('.discount').after('<p class="my_discount">'+percent+'% off</p>');
                    $(this).amplify(percent);
                    $(this).hiddenText('NA',shipping,percent);
                    return;
                }

                //Find all discounts that are a dollar amount. The variable spacing (\s*) is important to make this work with AMZOne and IReviewHome.
                reg = /[£$]\s*([\d\.]+) off/gi;
                match = reg.exec(oDiscount);
                //console.log(match);

                //If we found a dollar amount
                if (match !== null)
                {
                    //The dollar amount off the regular price
                    var off = parseFloat(match[1]);

                    //Calculate percentage discount.
                    percent = (1 - (price / (price + off))) * 100;
                    //Round down (50.01% should be 50%)
                    percent = Math.floor(percent);

                    //Add a "discount" section to the display, so we ALWAYS see a percentage discount for every item.
                    $(this).find('.discount').after('<p class="my_discount">'+percent+'% off</p>');
                    //console.log("off ("+off+") price ("+price+") percent ("+percent+'%)');

                    //Run amplify to hide/fade/highlight the offer as appropriate.
                    $(this).amplify(percent);
                    $(this).hiddenText(price,shipping,percent);
                    return;
                }

                //If we didn't find anything above, then we don't need to do anything.
                //console.log('Did not find percentage discount or a dollar discount.');
            });

            /**
             * Hide supplements. This doesn't work 100% effectively, and it would be difficult to make it work too much better without hiding other products too.
             * If you DON'T want to hide supplements, you can comment this whole section out.
             */
            $('#box-container .box').each(function() {
                //Only check items in the Beauty and Personal Care categories, to make sure we're not hiding anything we don't want to.
                var group = $(this).find('.product-group').text();
                var reg = /(Beauty|Personal *Care)/gi;
                var match = group.search(reg);

                //If the offer is in the appropriate category...
                if (match !== -1)
                {
                    //Get the offer title. Keep in mind this is a truncated title. It is NOT the full title, which is actually loaded (later) with javascript by AMZRT.
                    //We do not have access to the full title... which is why we can't detect some of the supplements.
                    var text = $(this).find('.title').text();

                    //Get anything with generic words (like tablets) in the title, AND anything with specific words (like Turmeric) that would only show up in offers for pills.
                    reg = /(Nootropic|Nitric Oxide|Multivitamins|Supplements?|Tablets?|Capsules?|Colon Cleanse|Turmeric Curcumin|Forskolin|Allicin|Bioperine|Anti Inflammatory|Anti Oxidant|Natural Brain Function|Testosterone|Neonatal|Biotin|\b\d+\s*mc?g\b|\b\d+\s*caps?\b|Probiotics|Brain *(Function)? *Booster|Coq10|Appetite Suppressant|Caralluma|Bean Extract|Weight Loss Supplement|diet pills|Garcinia Cambogia|Forskolin Plus|Resveratrol|Immune System Boost)/gi;
                    match = text.search(reg);

                    //If we matched to a supplement...
                    if (match !== -1)
                    {
                        //Add the class. Right now, this makes them hidden (just like bad discounts).
                        $(this).closest('.box').addClass('discount_supplement');
                    }
                }
            });
        });

    });
})();