Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function(){
var total = 0;
var year = '2012';
function init(num) {
num = (typeof num !== 'number' ? 0 : num);
if(num === 0) {
$('<div/>').css({
position: 'fixed',
left: 0,
top: 0,
width: '100%',
height: '100%',
zIndex: 1000,
backgroundColor: 'rgba(0,0,0,.7)',
color: '#fff',
fontSize: 30,
textAlign: 'center',
paddingTop: '15em'
}).attr('id', '___overlay').text('楽天市場いくら使った?').appendTo('body');
year = window.prompt('何年分の注文を集計しますか?\n半角数字4桁で入力してください', '2012');
if(!/^[0-9]+$/.test(year)) {
alert('正しい数値を入力してください');
$('#___overlay').remove();
return false;
}
}
var progress = load(num+1);
$('#___overlay').text('集計中… / '+(num+1)+'ページ目');
progress.done(function(price){
total += price;
init(num+1);
}).fail(function(){
alert('あなたは'+year+'年、合計'+ addFigure(total) + '円分の買い物を楽天市場でしました!');
$('#___overlay').remove();
});
}
function load(num) {
var df = $.Deferred();
var page = get(num);
page.done(function(data){
var dom = $.parseHTML(data);
var _total = 0;
$(dom).find('.price').each(function(){
var numchar = $(this).text().match(/[0-9]/g);
var price = 0;
if (numchar != null) {
price = Number(numchar.join(''));
}
_total += price;
});
if(_total === 0) df.reject();
else df.resolve(_total);
});
return df.promise();
}
function get(num) {
var df = $.Deferred();
$.ajax({
url: 'https://order.my.rakuten.co.jp/?page=myorder&fidomy=1&display_month=0&search_item=&page_num='+num+'&search_sender=&display_span='+year+'&search_shop=',
success: function(data){
df.resolve(data);
}
});
return df.promise();
}
function addFigure(str) {
var num = new String(str).replace(/,/g, "");
while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
return num;
}
if(typeof $ !== 'function') {
var d=document;
var s=d.createElement('script');
s.src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
s.onload=init;
d.body.appendChild(s);
} else {
init();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment