Skip to content

Instantly share code, notes, and snippets.

@YungSang
Last active January 3, 2016 04:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YungSang/8410382 to your computer and use it in GitHub Desktop.
Save YungSang/8410382 to your computer and use it in GitHub Desktop.
Taberareloo 用パッチ: 日付に六曜を追加する
// ==Taberareloo==
// {
// "name" : "Add Rokuyou"
// , "description" : "Add Rokuyou to Date"
// , "include" : ["background", "content"]
// , "match" : ["*://*/*"]
// , "version" : "2.0.0"
// , "downloadURL" : "https://gist.github.com/YungSang/8410382/raw/userscript.add.rokuyou.tbrl.js"
// }
// ==/Taberareloo==
// http://hitode909.hatenablog.com/entry/2014/01/12/205605
// https://github.com/hitode909/hatenablog-unofficial-modules/blob/master/rokuyo.js
// http://api.sekido.info/qreki?output=usage
// http://let.hatelabo.jp/taizooo/let/hJmc8PG74bcM
(function() {
var DEFAULT_STYLE = ".rokuyou{font-family:serif;font-weight:bold;font-size:smaller;} .rokuyou_0{color:#F66;} .rokuyou_1{color:#666;} .rokuyou_2{} .rokuyou_3{color:#F66;} .rokuyou_4{} .rokuyou_5{color:#666;}";
var SITEINFO = [
{
"name" : "はてなブログ",
"data" : {
"url" : "^http://[^.]+\\.hate(?:na(?:(?:blog|diary)\\.(?:com|jp))|blo\\.jp)/",
"date" : ".//time[span[@class='date-year']]",
"year" : "./span[@class='date-year']",
"month" : "./span[@class='date-month']",
"day" : "./span[@class='date-day']",
"style" : ".date{width:auto;} .rokuyou{display:inline;vertical-align:middle;}"
}
},
{
"name" : "はてなダイアリー",
"data" : {
"url" : "^http://d\\.hatena\\.ne\\.jp/",
"date" : ".//span[@class='date']",
"year" : "./text()",
"style" : ".rokuyou{display:inline;margin-left:0.5em;}"
}
},
{
"name" : "YungSangblr",
"data" : {
"url" : "^http://yungsang\\.tumblr\\.com/",
"date" : ".//div[contains(concat(' ',@class,''),' date ')]",
"year" : "./div[@class='date-year']",
"month" : "./div[@class='date-month']",
"day" : "./div[@class='date-day']",
"style" : ""
}
},
{
"name" : "ghost.yungsang.com",
"data" : {
"url" : "^http://ghost\\.yungsang\\.com/",
"date" : ".//p[@class='post-date']/time",
"year" : "substring-after(substring-after(substring-after(./text(),'Published '),' '),' ')",
"month" : "substring-before(substring-after(./text(),'Published '),' ')",
"day" : "substring-before(substring-after(substring-after(./text(),'Published '),' '),' ')",
"style" : ".rokuyou{display:inline;margin-left:0.5em;}"
}
}
];
if (inContext('background')) {
var QREKI_API = 'http://api.sekido.info/qreki';
var CACHE = {};
TBRL.setRequestHandler('getRokuyou', function (req, sender, func) {
var date = new Date(req.date);
var strDate = date.getFullYear() + ' ' + (date.getMonth() + 1) + ' ' + date.getDate();
var rokuyou = CACHE[strDate];
if (rokuyou) {
func(rokuyou);
return;
}
return request(QREKI_API, {
queryString : {
year : date.getFullYear(),
month : date.getMonth() + 1,
day : date.getDate(),
output : 'json'
},
responseType : 'json'
}).then(function (res) {
var rokuyou = res.responseText;
CACHE[strDate] = rokuyou;
func(rokuyou);
});
});
return;
}
function setStyle(style) {
var head = $X('//head')[0];
head.appendChild($N('style', {}, style));
}
function setRokuyou(siteinfo, doc) {
doc = doc || document;
var dates = siteinfo.date && [].concat($X(siteinfo.date, doc));
if (!dates) {
return;
}
dates.forEach(function (date) {
if (date.dataset.rokuyou) {
return;
}
var year = siteinfo.year && [].concat($X(siteinfo.year, date)).shift();
var month = siteinfo.month && [].concat($X(siteinfo.month, date)).shift();
var day = siteinfo.day && [].concat($X(siteinfo.day, date)).shift();
if (!year && !month && !day) {
return;
}
var strDate = joinText([
valueOfNode(year),
valueOfNode(month),
parseInt(valueOfNode(day), 10)
], ' ');
//console.log('[' + strDate + ']');
//console.log((new Date(strDate)));
chrome.runtime.sendMessage(TBRL.id, {
request : "getRokuyou",
date : strDate
}, function (rokuyou) {
if (date.dataset.rokuyou) {
return;
}
date.dataset.rokuyou = rokuyou.rokuyou_text;
date.appendChild($N('div', {
class : 'rokuyou rokuyou_' + rokuyou.rokuyou
}, rokuyou.rokuyou_text));
});
});
}
var loaded = false;
SITEINFO.forEach(function (siteinfo) {
if (!loaded && location.href.match(siteinfo.data.url)) {
loaded = true;
setStyle(DEFAULT_STYLE);
if (siteinfo.data.style) {
setStyle(siteinfo.data.style);
}
setRokuyou(siteinfo.data);
document.body.addEventListener('AutoPagerize_DOMNodeInserted', function (event) {
setRokuyou(siteinfo.data, event.target);
});
/*
document.body.addEventListener('DOMNodeInserted', function (event) {
setRokuyou(siteinfo.data, event.target);
});
*/
}
});
function valueOfNode(node) {
if (!node) {
return node;
}
if ((typeof node === 'string') || !isNaN(node)) {
return node;
}
if (node.nodeType === node.ELEMENT_NODE) {
if (node.tagName.match(/^(a|link)$/i)) {
return node.getAttribute('href');
} else if (node.tagName.match(/img/i)) {
return node.getAttribute('src');
} else {
return node.textContent.trim();
}
} else if (node.nodeType === node.ATTRIBUTE_NODE) {
return node.nodeValue;
} else if (node.nodeType === node.TEXT_NODE) {
return node.nodeValue;
}
}
})();
@YungSang
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment