Created
June 27, 2010 02:39
-
-
Save cho45/454574 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 Togetter: Block all users | |
// @namespace http://lowreal.net/ | |
// @include http://togetter.com/li/* | |
// @require http://github.com/cho45/jsdeferred/raw/master/jsdeferred.userscript.js | |
// @require http://gist.github.com/3239.txt#createElementFromString | |
// ==/UserScript== | |
// https://gist.github.com/454574 | |
(function () { with (D()) { | |
function toData (q) { | |
var ret = []; | |
for (var k in q) if (q.hasOwnProperty(k)) { | |
ret.push(encodeURIComponent(k) + "=" + encodeURIComponent(q[k])); | |
} | |
return ret.join("&"); | |
} | |
var header = document.querySelector('.info_list_box .tag_box'); | |
var menu = createElementFromString('<a class="action_tag">【まとめられたユーザを全員block】</a>'); | |
header.appendChild(menu); | |
menu.addEventListener('click', function (e) { | |
var willBlockMap = {}; | |
Array.forEach(document.querySelectorAll('.list_body .status a[href^="http://twitter.com/"]'), function (e) { | |
willBlockMap[e.href.split('/')[3]] = 1; | |
}); | |
var willBlock = []; | |
for (var key in willBlockMap) if (willBlockMap.hasOwnProperty(key)) willBlock.push(key); | |
if (confirm("以下のユーザをblockします\n" + willBlock.join(' '))) { | |
var status = createElementFromString('<a class="action_tag"></a>'); | |
header.replaceChild(status, menu); | |
status.innerHTML = 'ブロック開始...'; | |
var count = 0; | |
blockUsers(willBlock, function (type, user) { | |
if (type == "start") { | |
status.innerHTML = user + 'をブロック中 (' + (++count) + '/' + willBlock.length + ')'; | |
} else | |
if (type == "finish") { | |
status.innerHTML = user + 'をブロックしました (' + count + '/' + willBlock.length + ')'; | |
} | |
}). | |
next(function (i) { | |
status.innerHTML = 'ブロック完了'; | |
}); | |
} | |
}, false); | |
function blockUsers (willBlock, cb) { | |
return xhttp.get('http://twitter.com/'). | |
next(function (res) { | |
return res.responseText.match(/value='([^']+)' name='authenticity_token'/)[1]; | |
}). | |
next(function (authenticity_token) { | |
if (!authenticity_token) throw "Failed to get authenticity_token"; | |
return loop(willBlock.length, function (i) { | |
var user = willBlock[i]; | |
cb('start', user); | |
return xhttp({ | |
method: "post", | |
url:'http://api.twitter.com/1/blocks/create.json', | |
data: toData({ | |
screen_name: user, | |
post_authenticity_token : authenticity_token, | |
twttr : true | |
}), | |
headers: { | |
"Content-Type" : "application/x-www-form-urlencoded", | |
"X-Requested-With" : "XMLHttpRequest", | |
"Accept" : "application/json, text/javascript, */*" | |
} | |
}). | |
next(function (res) { | |
var data; | |
try { | |
data = JSON.parse(res.responseText); | |
} catch (e) { | |
alert(res.responseText); | |
throw "Invalid response"; | |
} | |
if (data.name) { | |
cb('finish', user); | |
} | |
}). | |
wait(1) | |
}); | |
}). | |
error(function (e) { | |
alert(e); | |
}); | |
} | |
} })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment