Skip to content

Instantly share code, notes, and snippets.

@zhuth
Created November 5, 2012 06:06
Show Gist options
  • Save zhuth/4015596 to your computer and use it in GitHub Desktop.
Save zhuth/4015596 to your computer and use it in GitHub Desktop.
豆瓣读书“山倒-抽丝-即焚”与预计读完日期显示
// ==UserScript==
// @name douban book estimator
// @description douban book reading
// @namespace http://tianhua.me/
// @auth break
// @version 0.122
// @license Public Domain
// @include http://www.douban.com/*
// @include http://book.douban.com/*
// ==/UserScript==
function main(){
function replace_strings(detailed) {
detailed = detailed.replace(/想读/ig, '山倒');
detailed = detailed.replace(/在读/ig, '抽丝');
detailed = detailed.replace(/读过/ig, '即焚');
return detailed;
}
function setPrompt(msg) {
var date = new Date();
date.setTime(date.getTime() + (1 * 60 * 60 * 1000));
$.cookie('db_book_msg', msg, { path: '/', expires: date });
$('.aside').before(msg);
}
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();
}
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
var btns = $('#interest_sect_level a[rel="nofollow"]');
if (btns.length > 0) {
$(btns[0]).find('span').html('山倒');
$(btns[1]).find('span').html('抽丝');
$(btns[2]).find('span').html('即焚');
}
var detailed = '' + $('#book span').html();
if (detailed != 'undefined') {
$('#book span').html(replace_strings(detailed));
}
var nav_list = $('#db-usr-profile');
if (nav_list.length > 0) {
p = nav_list.html();
p = replace_strings(p);
nav_list.html(p);
}
var msg = '' + $.cookie('db_book_msg');
if (msg != 'undefined' && msg != '' && msg != 'null') {
$('.aside').before(msg);
return;
}
$.get('http://book.douban.com/mine?status=collect&mode=list', function(doc) {
var books = $(doc).find('.date');
var n = books.length;
var lastDate = books[n-1].innerHTML.trim();
var firstDate = books[0].innerHTML.trim();
var diff = Date.parse(firstDate) - Date.parse(lastDate);
if (n > 2) {
diff = diff/3600/1000/24/(n-2);
$.get('http://book.douban.com/mine?status=wish&mode=list', function(doc2) {
var totalCount = parseInt($(doc2).find('h1').html().match(/\d+/)[0]);
$.get('http://book.douban.com/mine?status=do&mode=list', function(doc3) {
totalCount += parseInt($(doc3).find('h1').html().match(/\d+/)[0]);
if (totalCount > 0) {
var estDays = Math.round(totalCount / diff);
setPrompt('<h2>您目前在读和想读的书预计在' + estDays + '天后看完</h2>');
} else {
setPrompt('<h2>小心:三天不看书,智商输给猪!</h2>');
}
});
});
} else {
setPrompt('<h2>您目前在读和想读的书大概怎么也看不完了</h2>');
}
});
};
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//code.jquery.com/jquery-latest.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
// load jQuery and execute the main function
addJQuery(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment