aboutsummaryrefslogtreecommitdiffstats
path: root/twittperator/twsidebar-expand-url.tw
diff options
context:
space:
mode:
authorebith2012-08-08 21:35:29 +0900
committerebith2012-08-08 21:35:29 +0900
commitc4c6a2f0df63d0c9eced030b0bd87af0dac8f027 (patch)
treeaa7f11be836e4766e587909066428ada5d341d52 /twittperator/twsidebar-expand-url.tw
parent1b7554d3c50035bed96c105605d5a5b3dc325f17 (diff)
downloadvimperator-plugins-c4c6a2f0df63d0c9eced030b0bd87af0dac8f027.tar.bz2
短縮URLを展開するTwittperatorプラグイン(twsidebar向け)
Diffstat (limited to 'twittperator/twsidebar-expand-url.tw')
-rw-r--r--twittperator/twsidebar-expand-url.tw77
1 files changed, 77 insertions, 0 deletions
diff --git a/twittperator/twsidebar-expand-url.tw b/twittperator/twsidebar-expand-url.tw
new file mode 100644
index 0000000..ae84110
--- /dev/null
+++ b/twittperator/twsidebar-expand-url.tw
@@ -0,0 +1,77 @@
+(function () {
+ let Config = liberator.globalVariables.twittperator_sidebar_expand_url_config || {
+ host: ['ff.im', 'is.gd', 't.co', 'bit.ly', 'j.mp', 'htn.to', 'goo.gl', 'ow.ly'],
+ filter: function (url) {
+ return url.length > 60 ? url.substr(0, 60) + '...' : url;
+ }
+ }
+
+ function isShorten(uri)
+ Config.host.indexOf(uri.host) >= 0 ? true : 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 twsText (id)
+ sidebar().getElementById(id).childNodes[1].childNodes[3].childNodes[3].firstChild;
+
+ 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 (eUrl , expand) {
+ tweet.text = tweet.text.replace(sUrl, eUrl);
+ let fUrl = expand ? sUrl : eUrl;
+ let interval = setInterval(function () {
+ if (twsText(id)) {
+ twsText(id).textContent = escapeBreakers(twsText(id).textContent.replace(fUrl, Config.filter(eUrl)));
+ scroll();
+ clearInterval(interval)
+ }
+ }, 100);
+ });
+ }
+ }
+
+ 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);
+ }
+ }
+
+ plugins.twittperator.ChirpUserStream.addListener(onMsg);
+ plugins.twittperator.TrackingStream.addListener(onMsg);
+})();
+
+// vim: ft=javascript: