Instantly share code, notes, and snippets.
Created
May 2, 2012 04:15
-
Save ebith/2573594 to your computer and use it in GitHub Desktop.
短縮URLを展開するtwittperatorプラグイン(twsidebar向け)
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
(function () { | |
plugins.twittperator.ChirpUserStream.addListener(onMsg); | |
plugins.twittperator.TrackingStream.addListener(onMsg); | |
function isShorten(uri) { | |
switch (uri.host) { | |
case 'ff.im': | |
case 'is.gd': | |
case 't.co': | |
case 'bit.ly': | |
case 'j.mp': | |
case 'htn.to': | |
case 'goo.gl': | |
case 'ow.ly': | |
return true; | |
default: | |
return false; | |
} | |
} | |
function expandURL (url, callback) { | |
let uri = util.createURI(url); | |
if (isShorten(uri)) { | |
let xhr = new XMLHttpRequest; | |
xhr.open('HEAD', uri.spec, true); | |
xhr.setRequestHeader('User-Agent', 't.co'); // t.co対策 | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState == 4) { | |
callback(xhr.channel.URI.spec, url); | |
} | |
} | |
xhr.send(); | |
} else { | |
callback(url, false); | |
} | |
} | |
// twsidebar.twの直接呼びたい | |
function escapeBreakers (text) | |
text.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]+/g, function(c) uneval(c)); | |
function sidebar () | |
document.getElementById('sidebar')._contentWindow.document; | |
function scroll () { | |
let tws = sidebar().getElementById('tw-anekos-sb-home-list'); | |
let len = tws.itemCount; | |
tws.scrollToIndex(len - 1); | |
} | |
function replaceURL (tweet, id, urls) { | |
for (let i in urls) { | |
let sUrl = urls[i].url; | |
expandURL(urls[i].expanded_url, function (reu , expand) { | |
tweet.text = tweet.text.replace(sUrl, reu); | |
if (expand) { | |
sidebar().getElementById(id).childNodes[1].childNodes[3].childNodes[3].firstChild.textContent = escapeBreakers(tweet.text); | |
scroll(); | |
} | |
}); | |
} | |
} | |
function onMsg (msg, raw) { | |
if (!msg.entities) { return; } | |
let tweet = (msg.retweeted_status) ? msg.retweeted_status : msg; | |
if (tweet.entities.urls[0]) { | |
replaceURL(tweet, msg.id, tweet.entities.urls); | |
} | |
if (tweet.entities.media && tweet.entities.media[0]) { | |
replaceURL(tweet, msg.id, tweet.entities.media); | |
} | |
} | |
})() | |
// vim: ft=javascript: |
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
diff --git a/twittperator/twsidebar.tw b/twittperator/twsidebar.tw | |
index 13f5b06..b2dbff2 100644 | |
--- a/twittperator/twsidebar.tw | |
+++ b/twittperator/twsidebar.tw | |
@@ -130,6 +130,7 @@ liberator.modules.TWAnekoSB = ANekoSB = (function () { | |
richlistitemClasses.push(className('tweet-protected')); | |
xml = | |
<richlistitem | |
+ id={t.id} | |
class={richlistitemClasses.join(' ')} | |
style={[ | |
"font-size: " + px(Config.fontSize - (t.text.length > 70 ? 2 : 0)), | |
@@ -317,6 +318,7 @@ liberator.modules.TWAnekoSB = ANekoSB = (function () { | |
if (msg.direct_message) { | |
t = { | |
+ id: msg.id, | |
name: msg.direct_message.sender.screen_name, | |
img: msg.direct_message.sender.profile_image_url, | |
text: msg.direct_message.text, | |
@@ -325,6 +327,7 @@ liberator.modules.TWAnekoSB = ANekoSB = (function () { | |
}; | |
} else if (msg.retweeted_status) { | |
t = { | |
+ id: msg.id, | |
name: my ? msg.user.screen_name : msg.retweeted_status.user.screen_name, | |
img: my ? msg.user.profile_image_url : msg.retweeted_status.user.profile_image_url, | |
text: msg.retweeted_status.text, | |
@@ -368,6 +371,7 @@ liberator.modules.TWAnekoSB = ANekoSB = (function () { | |
dummy = true; | |
} else if (msg.user && msg.text && msg.in_reply_to_screen_name == screenName) { | |
t = { | |
+ id: msg.id, | |
name: msg.user.screen_name, | |
img: msg.user.profile_image_url, | |
text: msg.text, | |
@@ -375,6 +379,7 @@ liberator.modules.TWAnekoSB = ANekoSB = (function () { | |
}; | |
} else if (msg.user && msg.text) { | |
t = { | |
+ id: msg.id, | |
name: msg.user.screen_name, | |
img: msg.user.profile_image_url, | |
text: msg.text, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment