Created
February 23, 2012 00:29
-
-
Save moyashi/1888665 to your computer and use it in GitHub Desktop.
リンクからTwitterのusernameらしきものを抽出
This file contains hidden or 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
// ↓bookmarklet化したもの | |
// javascript:function isMayBeContainsScreenName(a,b,c){var d="^#$|^#!/$|^tos|^home|^jobs|^about$|^intent|^privacy$|^statuses|^settings|^session|^replies|^favorites|^inbox|^#!/download|^search|^#!/search.+|^\\?max_id=|/followers$|/following$|^#!/following|^#!/followers|^#!/i/connect|^#!/i/discover|^about/resources$|followers_you_follow|^#!/who_to_follow/suggestions";return!a.match(new RegExp(d))}Array.prototype.uniq=function(a){this.sort();ys=[];for(var b=0;b<this.length;b++){if(ys.length==0||this[b]!=ys[0])ys.unshift(this[b])}return ys};var collection=[];var dl=document.links;for(var i=0;i<dl.length;i++){if(dl[i].href.match(new RegExp("https?://(?:mobile.|m.)?twitter.com/(.+)"))){collection.push(RegExp.$1)}}collection=collection.filter(isMayBeContainsScreenName);var confirmedScreenName=[];var somethingElse=[];var screenName="([0-9a-zA-Z_]{1,15})";for(var j=0;j<collection.length;j++){if(collection[j].match(new RegExp("(?:#!/)"+screenName+"(?:/status/[0-9]+|/lists)"))){confirmedScreenName.push(RegExp.$1)}else if(collection[j].match(new RegExp(screenName+"(?:/status/[0-9]+|/lists)"))){confirmedScreenName.push(RegExp.$1)}else if(collection[j].match(new RegExp(screenName))){confirmedScreenName.push(RegExp.$1)}else{somethingElse.push(collection[j])}}confirmedScreenName=confirmedScreenName.uniq();var w=window.open("about:blank","");var body="<!DOCTYPE html><html><head>"+"<meta name='viewport' content='width=device-width, initial-scale=1' />"+"</head><body>"+"<form name='form1'><div><textarea name='t1' rows='20' cols='30'>"+confirmedScreenName.map(function(a,b,c){return"@"+a}).join("\n")+"</textarea></div>"+"<div><textarea rows='20' cols='30'>"+somethingElse.map(function(a,b,c){return"@"+a}).join("\n")+"</textarea></div></form></body></html>";w.document.write(body);w.document.form1.t1.focus();w.document.form1.t1.select();void 0 | |
// 配列の中の重複要素を削除するメソッドをArrayに追加 | |
Array.prototype.uniq = function (predicate) { | |
this.sort(); | |
ys = []; | |
for (var i = 0; i < this.length; i++) { | |
if (ys.length == 0 || this[i] != ys[0]) ys.unshift(this[i]); | |
} | |
return ys; | |
} | |
// URLの中から、Screen Nameを含まないURLを弾く | |
// Array.filterに渡す関数 | |
function isMayBeContainsScreenName(element, index, array) { | |
// Screen Nameを含まないであろうURLの正規表現 | |
// 可読性のために改行している | |
var regex = "\ | |
^#$|\ | |
^#!/$|\ | |
^tos|\ | |
^home|\ | |
^jobs|\ | |
^about$|\ | |
^intent|\ | |
^privacy$|\ | |
^statuses|\ | |
^settings|\ | |
^session|\ | |
^replies|\ | |
^favorites|\ | |
^inbox|\ | |
^#!/download|\ | |
^search|\ | |
^#!/search.+|\ | |
^\\?max_id=|\ | |
/followers$|\ | |
/following$|\ | |
^#!/following|\ | |
^#!/followers|\ | |
^#!/i/connect|\ | |
^#!/i/discover|\ | |
^about/resources$|\ | |
followers_you_follow|\ | |
^#!/who_to_follow/suggestions\ | |
"; | |
// 正規表現にマッチしないもの、つまり | |
// ブラックリストに該当しないもののみを返す | |
return !element.match(new RegExp(regex)); | |
} | |
// Twitter関連のURLのリストを格納する配列 | |
var collection = []; | |
// ブラウザで表示中のページの中に含まれるリンク先URLのリスト | |
var dl = document.links; | |
// URLのリストを精査 | |
for (var i = 0; i < dl.length; i++) { | |
// Twitter関連のURLだったら | |
if (dl[i].href.match(new RegExp("https?://(?:mobile\.|m\.)?twitter\.com/(.+)"))) { | |
// 変数に継ぎ足していく | |
collection.push(RegExp.$1); | |
} | |
} | |
// Twitter関連のURLのリストの中から、Screen Nameを含まないであろうものを削除 | |
collection = collection.filter(isMayBeContainsScreenName); | |
// Screen Nameであろう文字列を集める配列 | |
var confirmedScreenName = []; | |
// それ以外のものを集める配列 | |
var somethingElse = []; | |
// Screen Nameを表現する正規表現 | |
var screenName = "([0-9a-zA-Z_]{1,15})"; | |
// Twitter関連のURLを集めたリストを精査 | |
for (var j = 0; j < collection.length; j++) { | |
if (collection[j].match(new RegExp("(?:#!/)" + screenName + "(?:/status/[0-9]+|/lists)"))) { | |
// 「#!/」と「/status/000000」に囲まれたものは確実にScreen Nameなので | |
// Screen Nameであろう文字列を集める配列に継ぎ足す | |
confirmedScreenName.push(RegExp.$1); | |
} else if (collection[j].match(new RegExp(screenName + "(?:/status/[0-9]+|/lists)"))) { | |
// 後ろに「/status/000000」と続くものもまた確実にScreen Nameなので | |
// Screen Nameであろう文字列を集める配列に継ぎ足す | |
confirmedScreenName.push(RegExp.$1); | |
} else if (collection[j].match(new RegExp(screenName))) { | |
// Twitter関連のURLのリストの中から、 | |
// Screen Nameを含まないであろうものを削除した上で | |
// Screen Nameを表現する正規表現にマッチするものも | |
// 恐らくScreen Nameなので | |
// Screen Nameであろう文字列を集める配列に継ぎ足す | |
confirmedScreenName.push(RegExp.$1); | |
} else { | |
// それ以外のものは別の配列に継ぎ足す | |
somethingElse.push(collection[j]); | |
} | |
} | |
// 重複を削除 | |
confirmedScreenName = confirmedScreenName.uniq(); | |
// 出力用のウインドウを開く | |
var w = window.open("about:blank", ""); | |
// ウインドウの中身を構築 | |
var body = "<!DOCTYPE html><html><head>" + | |
"<meta name='viewport' content='width=device-width, initial-scale=1' />" + | |
"</head><body>" + | |
"<form name='form1'><div><textarea name='t1' rows='20' cols='30'>" + | |
confirmedScreenName.map(function (element, index, array) { return "@" + element }).join("\n") + | |
"</textarea></div>" + | |
"<div><textarea rows='20' cols='30'>" + | |
somethingElse.map(function (element, index, array) { return "@" + element }).join("\n") + | |
"</textarea></div></form></body></html>"; | |
// ウインドウに中身をセット | |
w.document.write(body); | |
// 最初のtextareaにフォーカスを合わせる | |
w.document.form1.t1.focus(); | |
// 最初のtextareaの中身を全選択 (iPhoneでは動かない?) | |
w.document.form1.t1.select(); | |
void(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment