Skip to content

Instantly share code, notes, and snippets.

@tkawa
Created August 28, 2012 10:32
Show Gist options
  • Save tkawa/3497020 to your computer and use it in GitHub Desktop.
Save tkawa/3497020 to your computer and use it in GitHub Desktop.
はてブのコメントリストにTwitterコメントを合成表示する userscript
// ==UserScript==
// @name Hatebu Mix Twitter Comment
// @namespace http://www.4bit.net/
// @include http://b.hatena.ne.jp/entry/*
// @description はてブのコメントリストにTwitterコメントを合成表示
// @author tkawa
// @version 1
// ==/UserScript==
(function(d, func) {
var check = function() {
if (typeof unsafeWindow.jQuery == 'undefined') return false;
func(unsafeWindow.jQuery); return true;
}
if (check()) return;
var s = d.createElement('script');
s.type = 'text/javascript';
s.src = '//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js';
d.getElementsByTagName('head')[0].appendChild(s);
(function() {
if (check()) return;
setTimeout(arguments.callee, 100);
})();
})(document, function($) {
var TOPSY_PERPAGE = 50;
var pageurl_escaped = encodeURIComponent(document.getElementById('head-entry-link').href);
var unixtime_set = false;
$.getJSON('http://b.hatena.ne.jp/entry/jsonlite/?url=' + pageurl_escaped, function (hatebu_data) {
hatebu_data.bookmarks.forEach(function (bookmark) {
// if ($('#bookmark-user-' + bookmark.user + ' > .timestamp').length == 0) { console.log('missing user:' + bookmark.user); }
$('#bookmark-user-' + bookmark.user + ' > .timestamp').text(bookmark.timestamp);
$('#bookmark-user-' + bookmark.user).attr('data-unixtime', Date.parse(bookmark.timestamp)/1000);
});
unixtime_set = true;
});
$('<p></p>').css('text-align', 'right').append(
$('<a></a>').attr('href', 'http://topsy.com/trackback?url=' + pageurl_escaped)
.append($('<img src="http://corp.topsy.com/wp-content/uploads/2010/10/powered_v3_92.png" alt="powered by Topsy">')))
.appendTo('#new-bookmarks');
$.getJSON('http://otter.topsy.com/trackbacks.js?callback=?&url=' + pageurl_escaped, { perpage: TOPSY_PERPAGE }, function (topsy_data) {
unsafeWindow.topsy_data = topsy_data; // for test
var check = function () {
if (!unixtime_set) return false;
//tweet_total = topsy_data.response.total;
topsy_process(topsy_data.response.list);
if (topsy_data.response.total > TOPSY_PERPAGE) {
retrieve_more(2);
}
show_tweet_counter(topsy_data.response.total);
return true;
}
(function () {
if (check()) return;
setTimeout(arguments.callee, 100);
})();
});
var topsy_process = function (list) {
console.log('topsy process ready');
var tweets = $('.twitter > a, .retweet-tree .retweet-node > a').get()
.map(function (a) { return a.getAttribute('href').toLowerCase(); })
.filter(function (href) { return href.match(/\d+$/); });
var elem = $('#new-bookmarks ul.bookmark-list > li').get(0);
var title = $('#head-entry-link').attr('title');
for (var i=0; i < list.length; i++) {
var duplicate = false;
for (var j=1; i+j < list.length; j++) {
if (list[i].content == list[i+j].content) {
duplicate = true;
break;
}
}
if (duplicate) {
console.log('[' + i + '] duplicated: ' + list[i].author.nick + ' == ' + list[i+j].author.nick);
continue;
} else if (tweets.indexOf(list[i].permalink_url.toLowerCase()) != -1) {
console.log('[' + i + '] exists: ' + list[i].author.nick);
continue;
} else {
console.log('[' + i + '] unique: ' + list[i].author.nick);
}
var item = list[i];
//console.log('process: ' + item.author.nick + ' ' + item.date);
while (elem && ((elem.nodeName != 'LI' || !elem.hasAttribute('data-unixtime')) || (elem.getAttribute('data-unixtime') > item.date))) {
elem = elem.nextSibling;
//console.log('elem: ' + elem.id + ' ' + elem.class);
}
//if (elem) console.log('traverse: ' + elem.getAttribute('data-user') + ' ' + elem.getAttribute('data-unixtime'));
//else console.log('traverse: end');
var li = $('<li class="retweet-node"></li>').attr({ 'data-unixtime': item.date });
var header = $('<div class="header"></div>');
$('<a></a>').attr({ href: item.author.url })
.append($('<img class="profile-image twitter-profile-image" />').attr({ src: item.author.photo_url, width: 32, height: 32 }))
.appendTo(header);
header.appendTo(li);
if (item.content.match(/^RT @[_a-zA-Z0-9]+/)) {
li.attr('title', item.content).css({display: 'none', 'padding-right': 0});
} else if (item.content.indexOf(title) == 0 && item.content.match(/https?:\/\/[^\s]+\s*$/)) {
li.attr('title', item.content).css({display: 'none', 'padding-right': 0});
} else {
$('<a class="username"></a>').attr({ href: item.permalink_url }).css({ color: '#2276BB' }).text('@'+item.author.nick).append('<img src="http://cdn-ak.b.st-hatena.com/images/icon-twitter.png">').appendTo(header);
li.append("\n");
var comment = item.content.replace(title, '<q class="title">' + title + '</q>')
.replace(/https?:\/\/[-.!~*';\/?:@&=+\$,%#\w]+/g, '<a href="$&" class="url">$&</a>')
.replace(/(^|\s+)(#\w+)/g, '$1<span class="hashtag">$2</span>')
.replace(/RT @.+$/, '<q class="rt">$&</q>')
.replace(/via @\w+/, '<span class="mention">$&</span>')
.replace(/(^|\s+)@(\w+)/g, '$1<a href="http://twitter.com/$2" class="mention">@$2</a>');
$('<span class="comment retweet-comment"></span>').html(comment).appendTo(li);
li.append("\n");
$('<span class="timestamp"</span>').text(new Date(item.date * 1000).toLocaleFormat('%Y/%m/%d %H:%M:%S')).appendTo(li);
}
if (elem) {
li.insertBefore(elem);
} else {
li.appendTo('ul.bookmark-list');
}
//console.log('insert: ' + li.html() + ' before ' + elem.id);
}
$('.retweet-comment span, .retweet-comment q').css({ color: '#B9BBBF' });
$('.retweet-comment a').css({ color: '#B9BBBF', 'text-decoration': 'none' });
};
var retrieve_more = function (page) {
$.getJSON('http://otter.topsy.com/trackbacks.js?callback=?&url=' + pageurl_escaped, { perpage: TOPSY_PERPAGE, page: page }, function (topsy_data) {
topsy_process(topsy_data.response.list);
});
};
var show_tweet_counter = function (count) {
$('<span title="Tweet"><img alt="Tweet" src="http://cdn-ak.b.st-hatena.com/images/icon-twitter.png" />' + count + '</span>')
.appendTo('#public-bookmarks-header .inner');
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment