Skip to content

Instantly share code, notes, and snippets.

@agustinhaller
Created November 20, 2012 03:22
Show Gist options
  • Select an option

  • Save agustinhaller/4115751 to your computer and use it in GitHub Desktop.

Select an option

Save agustinhaller/4115751 to your computer and use it in GitHub Desktop.
ext
/************************************************************************************
This is your Page Code. The appAPI.ready() code block will be executed on every page load.
For more information please visit our wiki site: http://docs.crossrider.com
*************************************************************************************/
/*
* Description:
* This extension asynchronous requests to get data from and post data to any domain
* (bypassing cross-domain browser restrictions), and handles the success/failure
* of the request.
*
* Usage:
* Invoked each time a page is viewed.
*
* Reference:
* http://docs.crossrider.com/#!/api/appAPI.request
*/
appAPI.ready(function($) {
// Place your code here (you can also define new functions above this scope)
// The $ object is the extension's jQuery object
var scrapper = (function(){
// var pepe = 'pepe';
return {
getDATA : function(retailers, product_keyword){
var all_products = [];
retailers.each(function(index, retailer){
console.log(retailer);
var urlGet = retailer.endpoint.replace("{{KEYWORD}}",product_keyword),
retailer_products = [];
appAPI.request.get(urlGet,
function(response, responseHeaders)
{
// console.log(response);
// console.log("HTML!", response);
// debugger;
$(response).find(retailer.container).each(function(index, elem){
var product_price = $(elem).find(retailer.product_price).text().trim(),
product_name = $(elem).find(retailer.product_name).text().trim();
retailer_products.push({price:product_price,name:product_name});
});
// Add all retailer products to the general array
all_products.push({retailer:retailer.name,products:retailer_products});
// return all_products_aux;
},
function(e)
{
console.log('Unable to get url ' + urlGet);
// return null;
}
);// END CORS CALL
});// END FOREACH RETAILER
debugger;
console.log("ALL PRODUCTS POSTA", all_products);
return all_products;
}// END getDATA function
}
}());
var retailers_urlGet = "http://makeitsolutions.com/labs/ext/base.json?type=product",
// product_keyword = "lean+startup";
product_keyword = "macbook+pro";
// Lets get retailers from backend API
appAPI.request.get(retailers_urlGet,
function(response, responseHeaders)
{
debugger;
var $retailers = $(JSON.parse(response)),
all_products = scrapper.getDATA($retailers, product_keyword);
console.log("ALL PRODUCTS", all_products);
},
function(e)
{
console.log('Unable to get url ' + retailers_urlGet);
// return null;
}
);// END CORS CALL
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment