Created
July 13, 2010 15:07
-
-
Save shiba-yu36/474003 to your computer and use it in GitHub Desktop.
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
// ==UserScript== | |
// @name expand_twitter_notation | |
// @description はてなダイアリー編集中にtwitter記法を展開する | |
// @namespace http://shiba-yu36.net/ | |
// @include http://d.hatena.ne.jp/* | |
// ==/UserScript== | |
(function() { | |
var tweet_cache = {}; //tweetのキャッシュ | |
function parseTweetId (string) { | |
var regexp1 = new RegExp("\\[?http://twitter.com/\\w+/status/(\\d+):twitter:\\w+\\]?$"); | |
var regexp2 = new RegExp('\\[?twitter:(\\d+):\\w+\\]?'); | |
if (string.match(regexp1)) { | |
return RegExp.$1; | |
} | |
else if (string.match(regexp2)) { | |
return RegExp.$1; | |
} | |
return ""; | |
} | |
function showTip (res, mouse_x, mouse_y) { | |
var json = eval("(" + res + ")"); | |
var tip_text = json.user.screen_name + ' / ' + json.text; | |
var tip = document.getElementById('twitter-tip'); | |
if(!tip) { | |
tip = document.createElement('div'); | |
tip.id = 'twitter-tip'; | |
document.body.appendChild(tip); | |
} | |
tip.style.cssText | |
= 'position: absolute;' | |
+ 'top: ' + (mouse_y + 5) + ';' | |
+ 'left: ' + (mouse_x + 10) + ';' | |
+ 'width: 300;' | |
+ 'background-color: #fffacd;' | |
+ 'border: solid 1px #000000;' | |
+ 'padding: 5px;'; | |
tip.textContent = tip_text; | |
} | |
function expand_twitter_notation (target, mouse_x, mouse_y) { | |
//初期化 | |
var tip = document.getElementById('twitter-tip'); | |
if (tip) { | |
tip.textContent = ''; | |
tip.style.cssText = ''; | |
} | |
if(!target.value) { | |
return; | |
} | |
var select = target.value.substring(target.selectionStart, target.selectionEnd); | |
var tweet_id = parseTweetId(select); | |
if (!tweet_id) { | |
return; | |
} | |
// キャッシュしてあるならそれを使う | |
if (tweet_cache[tweet_id]) { | |
showTip(tweet_cache[tweet_id], mouse_x, mouse_y); | |
} | |
else { | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'http://api.twitter.com/1/statuses/show/' + tweet_id + '.json', | |
headers: { | |
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey' | |
}, | |
onload: function(responseDetails) { | |
var res = responseDetails.responseText; | |
tweet_cache[tweet_id] = res; //キャッシュする | |
showTip(res, mouse_x, mouse_y); | |
} | |
}); | |
} | |
} | |
// 実行するURIを制限 | |
var allow_link1 = new RegExp('^http://d.hatena.ne.jp/\\w+(?:\\+\\w+)?/(?:\\d+(?:/\\d+)?)?(?:#edit_in_place)?$'); | |
var allow_link2 = new RegExp('^http://d.hatena.ne.jp/\\w+(?:\\+\\w+)?/edit.*'); | |
if (!location.href.match(allow_link1) && !location.href.match(allow_link2)) { | |
return; | |
} | |
document.addEventListener('mouseup', function (e) { | |
expand_twitter_notation(e.target, e.pageX, e.pageY); | |
}, true); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment