Last active
August 29, 2015 14:00
-
-
Save oflow/11361985 to your computer and use it in GitHub Desktop.
17sai.user.js
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
// -*- coding: utf-8 -*- | |
// ==UserScript== | |
// @name They are all 17 years old | |
// @author HIRATA Yasuyuki <[email protected]> | |
// @namespace http://yasu.asuka.net/ | |
// @version 1.1.1 | |
// @include http://www.google.tld/search?* | |
// @include https://www.google.tld/search?* | |
// @include https://www.google.tld/webhp?* | |
// @include https://www.google.tld/#q=* | |
// @released 2013-04-23 | |
// @updated 2015-02-27 | |
// ==/UserScript== | |
/* | |
* 17sai.user.js は https://gist.github.com/hirataya/5442881 を元にあれこれ修正してます。 | |
* なんかもう適当に追加していったらこうなったのでGoogleの仕様変わったら死にます。 | |
* class名が変わるのでXPathで強引に取得してます。やっぱり重いです。 | |
* 17歳でなく17才が正しいようです。 | |
*/ | |
(function(){ | |
/* 17歳教 - ニコニコ大百科 - http://dic.nicovideo.jp/a/17%E6%AD%B3%E6%95%99 */ | |
/* 17歳教☆チームT - Togetter - http://togetter.com/li/360631 */ | |
/* 23歳教 - ニコニコ大百科 - http://dic.nicovideo.jp/a/23%E6%AD%B3%E6%95%99 */ | |
/* 田中敦子17歳 - Twitter - https://twitter.com/atuwosyousa/status/220026704066904064 */ | |
/* 「17歳教」ではなく「17才教」が正しい - 井上喜久子 オフィシャルサイト http://www.manbow.com/profile */ | |
var members = { | |
/* 名前: {age: 年齢, y: 年, m: 月, d: 日} */ | |
'井上喜久子': {age: 17, y: 1964, m: 9, d: 25}, | |
'田村ゆかり': {age: 17, y: 1976, m: 2, d: 27}, | |
'野川さくら': {age: 17, y: 1978, m: 3, d: 1}, | |
'佐藤利奈': {age: 17, y: 1981, m: 5, d: 2}, | |
'堀江由衣': {age: 17, y: 1976, m: 9, d: 20}, | |
'石田燿子': {age: 17, y: 1973, m: 10, d: 7}, | |
'松澤由美': {age: 17, y: 1974, m: 3, d: 29}, | |
'こやまきみこ': {age: 17, y: 1979, m: 1, d: 27}, | |
'池澤春菜': {age: 17, y: 1975, m: 12, d: 15}, | |
'石黒千尋': {age: 17, y: 1983, m: 1, d: 24}, | |
'田中敦子': {age: 17, y: 1962, m: 11, d: 14}, | |
// '水落幸子': {age: 17, y: null, m: 12, d: 18}, わからんかった | |
'寺田はるひ': {age: 17, y: 1973, m: 2, d: 11}, | |
'榊原ゆい': {age: 19, y: 1980, m: 10, d: 13}, | |
'たかはし智秋': {age: 23, y: 1977, m: 5, d: 8}, | |
// '今井麻美': {age: 23, y: 1977, m: 5, d: 16}, 23歳教 元患部 | |
'後藤邑子': {age: 29, y: 1975, m: 8, d: 28} | |
}; | |
/* 正規表現で調べるやつ */ | |
var regExpStr = ''; | |
for (name in members) { | |
regExpStr += '|' + name; | |
} | |
var regExpName = new RegExp('(' + regExpStr.substring(1) + ')'); | |
function getElmXPath(elm, xpath) { | |
var result = document.evaluate(xpath, elm, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); | |
return result.singleNodeValue; | |
} | |
function fixAge(elm, member, regexp, br) { | |
if (!elm || !member) return; | |
if (regexp) { | |
if (regexp.test(elm.textContent)) { | |
var age = RegExp.$1; | |
elm.innerHTML = elm.innerHTML.replace(new RegExp(age, 'g'), get17days(member, br)); | |
} | |
} else { | |
elm.textContent = get17days(member); | |
} | |
} | |
function fix17sai() { | |
var mod = document.querySelectorAll('li.mod'); | |
/* ○○○ 年齢 と検索したとき上に出てくるやつ */ | |
// ○歳 (YYYY年MM月DD日) となってないと失敗 | |
if (!mod.length) return; | |
var a = getElmXPath(mod[1], './div/*[contains(text(), "歳")]'); | |
// 名前、年齢 | |
var n = getElmXPath(mod[1], './div/*[contains(text(), "年齢")]'); | |
if (a && n) { | |
if (/(.+)、\s年齢/.test(n.textContent)) { | |
var name = RegExp.$1.replace(/\s/g, ''); | |
fixAge(a, members[name], /^([\d]+歳)\s/); | |
} | |
} | |
/* おまけで3人ほど年齢表示されるので対応させる */ | |
/* className.contains('ellip') */ | |
var ellip = mod[2].getElementsByClassName('ellip'); | |
for (var i = 0; i < ellip.length; i += 2) { | |
fixAge(ellip[i + 1], members[ellip[i].textContent.replace(/\s/g, '')], /([\d]+歳)$/, true); | |
} | |
/* 右側のプロフィールも年齢間違えてる */ | |
var knoecrpt = document.getElementsByClassName('kno-ecr-pt')[0]; | |
if (knoecrpt) { | |
fixAge( | |
document.getElementsByClassName('kno-fv')[0], | |
members[knoecrpt.textContent.replace(/\s/g, '')], | |
/\(([\d]+歳)\)/ | |
); | |
} | |
/* 上のサムネみたいに表示される箇所 */ | |
var kltat = document.getElementsByClassName('kltat'); | |
for (var i = 0, elm; elm = kltat[i]; i++) { | |
if (member = members[elm.textContent.replace(/\s/g, '')]) { | |
fixAge( | |
elm.parentNode.getElementsByClassName('klfact')[0], | |
member | |
); | |
} | |
} | |
/* 検索結果 わりと適当 */ | |
var r = document.getElementsByClassName('r'); | |
for (var i = 0, h3; h3 = r[i]; i++) { | |
if (regExpName.test(h3.textContent) && h3.className.indexOf('17sai') == -1) { | |
var member = members[RegExp.$1]; | |
if (member) { | |
/* 説明文に年齢 */ | |
fixAge( | |
h3.parentNode.getElementsByClassName('st')[0], | |
member, | |
/[\((]([\d]+歳)[\))]/ | |
); | |
/* タイトルに年齢 */ | |
fixAge(h3, member, /([\d]+歳)/); | |
h3.className += ' 17sai'; | |
} | |
} | |
} | |
} | |
function get17days(member, br) { | |
if (member['age'] == 19) { | |
/* 今のところ榊原ゆいだけ */ | |
return '永遠のナインティーン'; | |
} else { | |
var baseBirthday = new Date( | |
member['age'] + member['y'], | |
member['m'] - 1, | |
member['d'] | |
); | |
var sai = (member['age'] == 17 ? '才' : '歳'); | |
var days = Math.floor((Date.now() - baseBirthday) / (60*60*24*1000)); | |
return member['age'] + sai + 'と' + (br ? '<br>' : '') + days + '日'; | |
} | |
} | |
fix17sai(); | |
var observer = new MutationObserver(function(mutations) { | |
mutations.forEach(function(mutation) { | |
if (mutation.type == 'childList') { | |
fix17sai(); | |
} | |
}); | |
}); | |
observer.observe(document.body, {childList: true}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment