Skip to content

Instantly share code, notes, and snippets.

@tkawa
Created January 31, 2011 10:07
Show Gist options
  • Save tkawa/803847 to your computer and use it in GitHub Desktop.
Save tkawa/803847 to your computer and use it in GitHub Desktop.
非公式RTの引用元を探して表示する twicli プラグイン
var seekRTOrigin;
(function () {
// RTをクリックできるようにする
function makeRTClickable(element, tw) {
if (tw.retweeted_status && !display_as_rt) {
tw = tw.retweeted_status;
}
var status = element.getElementsByClassName('status'); // getElementsByClassName使ってるのでIEでは動かない?
if (status.length > 0) {
status = status[0];
for (var i = 0; i < status.childNodes.length; i++) {
var node = status.childNodes[i];
if (node.nodeName == '#text'
&& node.textContent.match(/^(.*)(RT|QT)(\s*)$/)
&& node.nextSibling
&& node.nextSibling.nodeName.toUpperCase() == 'A'
&& node.nextSibling.textContent.indexOf('@') == 0) {
//console.log(['RT detected', tw.id_str, status]);
node.textContent = RegExp.$1;
var seekrt = document.createElement('span');
seekrt.textContent = RegExp.$2 + RegExp.$3;
seekrt.className = 'urt';
seekrt.style.cursor = 'pointer';
seekrt.style.color = '#DD0000';
seekrt.setAttribute('onclick',
"seekRTOrigin('"
+ tw.id_str
+ "',this);return false;"
);
node.parentNode.insertBefore(seekrt, node.nextSibling);
}
}
}
}
// ボタンから呼ばれる
seekRTOrigin = function (id, elem) {
var sibl = elem.nextSibling;
var user = sibl.textContent.slice(1);
var text = '';
while (sibl = sibl.nextSibling) {
text += sibl.textContent;
}
text = text.replace(/(^:?\s*)/, '');
var seektext = text.replace(/https?:\/\/.*$/, ''); // URLは書き換えられているかもしれないので捨てる
seektext = seektext.replace(/\s*[\((].{0,2}\s*$/, ''); // 適当に捨てる
seektext = seektext.replace(/\s*(\.+|…+)\s*$/, ''); // 適当に捨てる
//console.log(['seekRTOrigin', id, user, seektext]);
searchUserTweet(user, seektext, id, function (tws) {
dispOverlay(tws, elem);
});
};
// max_id より前の200件について調べる
function searchUserTweet(user, text, max_id, callback) {
var url = twitterAPI + 'statuses/user_timeline.json?screen_name=' + user + '&suppress_response_codes=true&count=200';
if (max_id) {
url += ('&max_id=' + max_id);
}
$('loading').style.display = 'block';
xds.load(url, function (tws) {
console.log(['searchUserTweet', user, text, tws]);
var matched = tws.filter(function (tw) { return tw.text.indexOf(text) == 0; });
$('loading').style.display = 'none';
callback(matched);
});
}
function dispOverlay(tws, elem) {
if (tws.length == 0) return;
console.log(['dispOverlay', tws]);
var elem_top = cumulativeOffset(elem)[1] + 20;
rep_top = elem_top;
$('reps').innerHTML = '';
$('rep').style.top = elem_top;
for (var i = 0; i < tws.length; i++) {
var tw = tws[i];
var el = document.createElement('div');
el.id = 'reps-'+tw.id_str;
el.innerHTML = makeHTML(tw, false, 'reps');
el.tw = tw;
callPlugins('newMessageElement', el, tw, 'reps');
$('reps').appendChild(el);
}
$('rep').style.display = 'block';
scrollToDiv($('rep'));
}
registerPlugin({
newMessageElement: makeRTClickable
//afterTwShowToNode: autoEmbed
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment