Skip to content

Instantly share code, notes, and snippets.

@tkawa
Created April 8, 2011 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkawa/909316 to your computer and use it in GitHub Desktop.
Save tkawa/909316 to your computer and use it in GitHub Desktop.
はてブmoreページのコメントリストにTwitterコメントを合成表示する Greasemonkey Userscript
// ==UserScript==
// @name Hatebu More Twitter Comment
// @namespace http://www.4bit.net/
// @description はてブmoreページのコメントリストにTwitterコメントを合成表示
// @author tkawa
// @include http://b.hatena.ne.jp/entry?mode=more*
// ==/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 = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js';
d.getElementsByTagName('head')[0].appendChild(s);
(function() {
if (check()) return;
setTimeout(arguments.callee, 100);
})();
})(document, function($) {
var pageurl_escaped = window.location.href.match(/url=([^&]*)/)[1];
var unixtime_set = false;
//var tweet_total = 0;
$.getJSON('http://b.hatena.ne.jp/entry/jsonlite/?url=' + pageurl_escaped, function (hatebu_data) {
unsafeWindow.hatebu_data = hatebu_data; // for test
//console.log('loaded');
hatebu_data.bookmarks.forEach(function (bookmark) {
$('#bookmark-user-' + bookmark.user + ' > .timestamp').text(bookmark.timestamp);
$('#bookmark-user-' + bookmark.user).attr('data-unixtime', Date.parse(bookmark.timestamp)/1000);
});
unixtime_set = true;
$('li.nocomment img.profile-image').css('float', 'none');
$('li.nocomment span').hide();
$('li.nocomment .username').hide();
$('li.nocomment .plus_icon').hide();
$('li.nocomment').css({display: 'inline', 'padding-right': 0});
});
$('<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: 50 }, 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);
if (topsy_data.response.total > 50) {
retrieve_more(2);
}
show_tweet_counter(topsy_data.response.total);
return true;
}
(function () {
if (check()) return;
setTimeout(arguments.callee, 100);
})();
});
var topsy_process = function (topsy_data) {
console.log('topsy process ready');
var list = topsy_data.response.list;
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 = $('ul.bookmark-list > li').get(0);
var title = $('#head-entry-link').attr('title');
list.forEach(function (item) {
//console.log('process: ' + item.author.nick + ' ' + item.date);
if (tweets.indexOf(item.permalink_url.toLowerCase()) != -1) return;
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 }).css('font-size', '90%');
$('<a></a>').attr({ href: item.author.url })
.append($('<img class="twitter-profile-image" />').attr({ src: item.author.photo_url, width: 16, height: 16 }).css('margin-left', '-20px'))
.appendTo(li);
if (item.content.match(/^RT @[_a-zA-Z0-9]+/)) {
li.attr('title', item.content).css({display: 'inline', 'padding-right': 0});
} else if (item.content.indexOf(title) == 0 && item.content.match(/https?:\/\/[^\s]+\s*$/)) {
li.attr('title', item.content).css({display: 'inline', 'padding-right': 0});
} else {
$('<a></a>').attr({ href: item.permalink_url }).css({ color: '#2276BB', 'text-decoration': 'none' }).text('@'+item.author.nick).appendTo(li);
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="retweet-comment"></span>').html(comment).appendTo(li);
li.append("\n");
$('<span class="timestamp"</span>').css('font-size', '90%').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: 50, page: page }, function (topsy_data) {
topsy_process(topsy_data);
});
};
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>')
.insertAfter('#main-counter > span:last');
};
});
@tkawa
Copy link
Author

tkawa commented Apr 8, 2011

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