Skip to content

Instantly share code, notes, and snippets.

@negipo
Created May 25, 2009 15:40
Show Gist options
  • Save negipo/117592 to your computer and use it in GitHub Desktop.
Save negipo/117592 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name CookpadCalcCalorie
// @namespace http://polog.org/
// @include http://cookpad.com/recipe/*
// @require http://gist.github.com/3242.txt
// ==/UserScript==
// this script is using YACA - Yet Another Calorie API http://makimoto.tsuyabu.in/YACA/
var ingredient_calories = eval(GM_getValue('cache')) || {};
GM_addStyle(
'div.cal_wrapper{position: relative;float:right;} div.cal{position: absolute; top: -1em; left: 20px; color: #AAA; font-size: 180%; opacity: 0.5;}'
);
$X('id("ingredients-list")/descendant::tr/td[1]/span').forEach(function(e){
if(/<span/.exec(e.innerHTML))
return;
var ingredient = e.innerHTML.replace(/(^\s+|[\((].*?[\))]|[☆★○●◎△▲▽▼♡♥※]|\s+$)/g, '');
set_calorie_by_ingredient(ingredient, e);
});
function set_calorie_by_ingredient(ingredient, element){
if(!!ingredient_calories[ingredient])
return appendCal(ingredient_calories[ingredient], element);
var url = 'http://makimoto.tsuyabu.in/YACA_dev/api.py?appid=cookpad_calc_calorie&food=' + encodeURI(ingredient);
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function(res){
var ret = eval("(" + res.responseText + ")");
var cal = ret.median;
if(!cal)
return;
ingredient_calories[ingredient] = cal;
GM_setValue('cache', uneval(ingredient_calories));
appendCal(cal, element);
}
});
}
function appendCal(text, element){
var w = document.createElement('div');
w.className = 'cal_wrapper';
var c = document.createElement('div');
c.className = 'cal';
c.innerHTML = '' + text + 'kcal';
w.appendChild(c);
element.appendChild(w);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment