// ==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}); }); $('

').css('text-align', 'right').append( $('').attr('href', 'http://topsy.com/trackback?url=' + pageurl_escaped) .append($('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 = $('
  • ').attr({ 'data-unixtime': item.date }).css('font-size', '90%'); $('').attr({ href: item.author.url }) .append($('').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 { $('').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, '' + title + '') .replace(/https?:\/\/[-.!~*';\/?:@&=+\$,%#\w]+/g, '$&') .replace(/(^|\s+)(#\w+)/g, '$1$2') .replace(/RT @.+$/, '$&') .replace(/via @\w+/, '$&') .replace(/(^|\s+)@(\w+)/g, '$1@$2'); $('').html(comment).appendTo(li); li.append("\n"); $('').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) { $('Tweet' + count + '') .insertAfter('#main-counter > span:last'); }; });