Created
May 25, 2009 15:40
-
-
Save negipo/117592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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