Last active
December 22, 2015 01:09
-
-
Save saitamanodoruji/6394754 to your computer and use it in GitHub Desktop.
はてブの entry ページにショートカットキーを追加する. m でお気に入りユーザーのコメントを表示・非表示, h でタブの切替, o で元ページを開く.
This file contains hidden or 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== | |
// @id Hatebu Entry Page Shortcut Keys | |
// @name Hatebu Entry Page Shortcut Keys | |
// @version 1.5.2 | |
// @namespace http://b.hatena.ne.jp/saitamanodoruji/ | |
// @author saitamanodoruji | |
// @description はてブの entry ページにショートカットキーを追加 | |
// @include http://b.hatena.ne.jp/entry/* | |
// @run-at document-end | |
// @update 2013-09-17 | |
// ==/UserScript== | |
// m: お気に入りユーザーコメントの表示・非表示 | |
// h: タブの切り替え | |
// o: 元ページを開く | |
(function() { | |
const HATENA_STAR_URL = 'http://s.hatena.com/entry.json'; | |
var displayComments = function() { | |
var li = document.querySelectorAll('#favorite-bookmarks > li'); | |
li = Array.prototype.slice.call(li); | |
li.forEach(function(e){ | |
e.style.display = 'block'; | |
var onmouseoverAttr = e.getAttribute('onmouseover') | |
var re = /^Hatena\.Bookmark\.Tooltip\.BookmarkEntry\.create\(this, (.*)\);$/; | |
var json = onmouseoverAttr.replace(re, '$1'); | |
try { | |
var d = JSON.parse(json); | |
} catch(e) { | |
var d = {}; | |
} | |
var time = new Date(Number(d.epoch)*1000).toLocaleFormat('%T'); | |
var s = document.createElement('span'); | |
s.className = 'gm-favorite-comment'; | |
s.innerHTML = [ | |
'<span style="color:#0086DE;">', d.user, '</span>', | |
d.comment_raw, | |
'<span style="color:#AAAAAA;">', d.timestamp, time, '</span>', | |
].join(' '); | |
s.style.marginLeft = '5px'; | |
s.style.fontSize = '14px'; | |
e.appendChild(s); | |
}); | |
li.forEach(function(e){ | |
var permalink = e.querySelector('a').href; | |
var favComment = e.querySelector('.gm-favorite-comment'); | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: HATENA_STAR_URL + '?uri=' + encodeURIComponent(permalink), | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
onload: function(res) { | |
var data = JSON.parse(res.responseText); | |
var starContainer = document.createElement('span'); | |
favComment.appendChild(starContainer); | |
if (data.entries[0].colored_stars) { | |
data.entries[0].colored_stars.forEach(function(cstars) { | |
cstars.stars.forEach(function(star) { | |
var uid = star.name; | |
var anchor = document.createElement('a'); | |
anchor.href = 'http://b.hatena.ne.jp/' + uid + '/'; | |
anchor.target = '_blank'; | |
anchor.title = uid; | |
var icon = document.createElement('img'); | |
icon.src = [ | |
'http://www.hatena.ne.jp/users/', | |
uid.substring(0, 2), '/', uid, | |
'/profile_s.gif' | |
].join(''); | |
icon.style.margin = '0 3px'; | |
icon.style.outline = '2px solid ' + cstars.color; | |
anchor.appendChild(icon); | |
starContainer.appendChild(anchor); | |
}); | |
}); | |
} | |
data.entries[0].stars.forEach(function(star){ | |
var uid = star.name; | |
var anchor = document.createElement('a'); | |
anchor.href = 'http://b.hatena.ne.jp/' + uid + '/'; | |
anchor.target = '_blank'; | |
anchor.title = uid; | |
var icon = document.createElement('img'); | |
icon.src = [ | |
'http://www.hatena.ne.jp/users/', | |
uid.substring(0, 2), '/', uid, | |
'/profile_s.gif' | |
].join(''); | |
icon.style.margin = '0 3px'; | |
anchor.appendChild(icon); | |
starContainer.appendChild(anchor); | |
}); | |
}, | |
}); | |
}) | |
button.innerHTML = '<span id="gm-favorites-comments-button" class="hide">[hide comments]</span>'; | |
button.removeEventListener('click', displayComments); | |
button.addEventListener('click', hideComments); | |
} | |
var hideComments = function() { | |
var c = document.querySelectorAll('.gm-favorite-comment'); | |
c = Array.prototype.slice.call(c); | |
c.forEach(function(e) { | |
e.parentNode.style.display = 'inline'; | |
e.parentNode.removeChild(e); | |
}); | |
button.innerHTML = '<span id="gm-favorites-comments-button" class="display">[display comments]</span>'; | |
button.removeEventListener('click', hideComments); | |
button.addEventListener('click', displayComments); | |
} | |
var keypressHandler = function(e) { | |
var aElm = document.activeElement; | |
if ( (aElm.nodeName.toLowerCase() == 'input' && aElm.type == 'text') | |
|| aElm.nodeName.toLowerCase() == 'textarea') return; | |
var tag = e.target.tagName.toUpperCase(); | |
if ( tag == 'HTML' || 'BODY' || 'DIV' ) { | |
if ( e.altKey || e.ctrlKey || e.metaKey || e.shiftKey ) return; | |
var keyCode = e.charCode || e.keyCode; | |
var c = String.fromCharCode(e.charCode || e.keyCode); | |
switch( c ) { | |
case 'm': | |
var state = document.getElementById('gm-favorites-comments-button').className; | |
if (state == 'display') { | |
displayComments(); | |
} else if (state == 'hide') { | |
hideComments(); | |
} | |
break; | |
case 'h': | |
var tabs = document.querySelectorAll('.tab-navi > li:not(.disable)'); | |
tabs = Array.prototype.slice.call(tabs); | |
var currentTabIndex = tabs.reduce(function(pre, cur, ind, arr) { | |
if (/(?:^| )current(?:$| )/.test(cur.className)) { | |
return ind; | |
} else { | |
return pre; | |
} | |
}, 0); | |
tabs[(currentTabIndex + 1) % tabs.length].click(); | |
break; | |
case 'o': | |
var headEntryLink = document.getElementById('head-entry-link'); | |
GM_openInTab(headEntryLink.href, true); | |
break; | |
} | |
} | |
} | |
var button = document.createElement('span'); | |
button.innerHTML = '<span id="gm-favorites-comments-button" class="display">[display comments]</span>'; | |
button.addEventListener('click', displayComments); | |
var ul = document.querySelector('#favorite-bookmarks'); | |
if (ul) { ul.appendChild(button); } | |
document.addEventListener('keypress', keypressHandler); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment