Created
October 11, 2010 06:52
-
-
Save xulapp/620142 to your computer and use it in GitHub Desktop.
Hatebu count on Google Reader
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
// ==UserScript== | |
// @name Hatebu count on Google Reader | |
// @namespace http://twitter.com/xulapp | |
// @include http://www.google.tld/reader/view/* | |
// @include https://www.google.tld/reader/view/* | |
// @author xulapp | |
// @license MIT License | |
// @version 2010/10/11 15:50 +09:00 | |
// ==/UserScript== | |
(function HatebuCountOnGoogleReader() { | |
const LINK_URL_PREF = 'http://b.hatena.ne.jp/entry/'; | |
const IMAGE_URL_PREF = LINK_URL_PREF + 'image/'; | |
var image = document.createElement('img'); | |
image.setAttribute('style', 'margin-left: 4px; border: none; vertical-align: middle;'); | |
var entries = document.getElementById('entries'); | |
var queue = Array.slice(entries.querySelectorAll('.entry')); | |
run(); | |
function start() { | |
entries.addEventListener('DOMNodeInserted', onDOMNodeInserted, false); | |
} | |
function pause() { | |
entries.removeEventListener('DOMNodeInserted', onDOMNodeInserted, false); | |
} | |
function run() { | |
pause(); | |
queue.forEach(addWidget); | |
queue.length = 0; | |
start(); | |
} | |
function onDOMNodeInserted({target}) { | |
var cl = target.classList; | |
if (!cl.contains('entry')) { | |
// リスト表示 → 全文表示: .entry > card が出現 | |
// 全文表示 → リスト表示: .entry > collapsed が出現 | |
if (!cl.contains('collapsed') && !cl.contains('card')) | |
return; | |
target = target.parentNode; | |
} | |
if (!queue.length) { | |
setTimeout(run, 0); | |
} | |
queue.push(target); | |
} | |
function addWidget(entry) { | |
var url = entry.querySelector('.entry-original, .entry-title-link').href; | |
var a = document.createElement('a'); | |
a.href = url.replace(/^http(s?):\/\/(.+)/, hatena); | |
var img = image.cloneNode(false); | |
img.src = IMAGE_URL_PREF + url; | |
a.appendChild(img); | |
var sibling = entry.querySelector('.entry-secondary > .entry-title, .entry-title-link'); | |
sibling.parentNode.insertBefore(a, sibling.nextSibling); | |
} | |
function hatena(url, secure, path) { | |
return LINK_URL_PREF + (secure && 's/') + path; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment