Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Show the total cost of an Amazon Wish List.
//
// This script shows the total cost of an Amazon Wish List.
//
// Usage:
//
// 1. Open an Amazon Wish List
// 2. Select the "Compact" option in the "View" drop-down
// 3. Click the "GO" button
// 4. Copy and paste the code below in the browser console
// 5. Press Enter
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Murilo Pereira <murilo@murilopereira.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
(function($) {
function zip() {
var arrays = $.makeArray(arguments);
var length = Math.max.apply(Math, $.map(arrays, function(array) {
return(array.length);
}));
var zipped = Array(length);
for (var i = 0; i < length; i++) {
zipped[i] = $.map(arrays, function(array) { return(array[i]); });
}
return(zipped);
}
this.Amazon = {};
Amazon.Product = function(attributes) {
this.name = attributes.name;
this.price = attributes.price;
this.quantity = attributes.quantity;
};
Amazon.Product.prototype = (function() {
return({
cost: function() {
return(this.price * this.quantity);
}
});
})();
Amazon.WishList = function() {
};
Amazon.WishList.prototype = (function() {
var $baseSelector =
$('tr .g-selected input').parents('label').parents('td').parents('tr');
var $productNames = $baseSelector.find('.g-title');
var $productPrices = $baseSelector.find('.g-price');
var $productQuantities = $baseSelector.find('.g-requested input');
function productsNames() {
return($productNames.map(function(index, el) {
return($(el).text().trim().replace(/\s+/g, ' '));
}));
}
function productsPrices() {
return($productPrices.map(function(index, el) {
return(parseFloat($(el).text().trim().replace(/[^\d.]/g, '')) || 0.0);
}));
}
function productsQuantities() {
return($productQuantities.map(function(index, el) {
return(parseInt($(el).val(), 10) || 0);
}));
}
function productsAttributes() {
return(zip(productsNames(), productsPrices(), productsQuantities()));
}
return({
products: function() {
return($.map(productsAttributes(), function(productAttributes) {
return(new Amazon.Product({
name: productAttributes[0],
price: productAttributes[1],
quantity: productAttributes[2]
}));
}));
},
cost: function() {
return(this.products().reduce(function(totalCost, product) {
return(totalCost + product.cost());
}, 0));
}
});
})();
alert('Wish List total cost: ' + (new Amazon.WishList()).cost());
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.