// batch delete posts script for weibo.com
// 批量删除新浪微博帖子代码
//
// By Paul Lan, 基督教北京守望教会弟兄小方
// Last Modified: 2011年11月13日00时51分03秒
//
// usage:
// 1. you should go to your personal page first
// 2. excute this script via firebug console in Firefox, or via console in Chrome -- sorry for IE users (I can't believe you guys who are able to run this script using IE as your faverite browser)
// 3. wait until the page alert "It's done! Love you! Tibco Paul."
// 使用方法:
// 1. 先进入你的个人主页
// 2. 在Firebuy 控制台或者Chrome控制台执行这段代码 (按F12,然后把此代码拷贝进弹出的控制台里。在Firefox下点击运行 / 在Chrome下回车即可)
// 3. 等待, 直到页面弹出"It's done! Love you! Tibco Paul."
var deleteAllMyPost = function() {
// check browser
if (!document.createEvent) {
alert('Sorry, but you should run my script under Firefox or Chrome, NO IE!\n\n对不起, 你只能在火狐或者谷歌浏览器Chrome下运行这段代码!\n\nTo download Firefox, please go to: "http://www.mozilla.org/en-US/firefox/new/", then install "Firebug" to run it;\n\n下载火狐,请打开:http://www.mozilla.org/en-US/firefox/new/');
return false;
}
// seriously warning!
var dbconfirm = confirm('Warning: to run this script, all of your posts in Weibo.com will be deleted! ARE YOU SURE?\n\n警告: 如果继续执行这个代码, 那么你新浪微博里面的所有的帖子都会被删除, 你确定么?');
if (!dbconfirm) {
return false;
}
// check if current page is personal page
var pageUrl = location.href;
if (!/.*\/profile\?.*/.test(pageUrl)) {
alert('Sorry, but, to run this script to delete your all of your posts, you should go to your personal page by clicking your avatar or your nic-name next to your avatar;\n\n对不起, 要运行这段代码去删除你所有的新浪微博帖子, 请点击你的头像或者你头像旁边的你的昵称跳到你的个人微博主页.');
return false;
}
// deleting indicator
var masker = document.createElement('div');
var maskerText = 'Deleting all posts of your weibo.com, to cancel deleting, please press F5 key.
正在删除新浪微博所有的帖子,如果要停止删除,请按 F5 键.
';
masker.innerHTML = maskerText;
masker.style.opacity = '0.4';
masker.style.backgroundColor = '#000000';
masker.style.position = 'fixed';
masker.style.height = '100%';
masker.style.width= '100%';
masker.style.top= '0';
masker.style.left= '0';
masker.style.zIndex= '9999';
masker.style.color= '#ffffff';
masker.style.padding= '100px';
document.body.appendChild(masker);
// prepare click event
var event = document.createEvent('HTMLEvents');
event.initEvent('click', true, true);
// get next page handler
var getNextPageBtn = function() {
var tmpBtns = document.getElementsByTagName('span');
len = tmpBtns .length;
window.nextPageBtn = null;
for (var i = 0; i < len; i++) {
nextPageBtnTmp = tmpBtns[i];
if (/下一页/.test(nextPageBtnTmp.innerHTML)){
nextPageBtn = nextPageBtnTmp.parentNode;
}
}
return nextPageBtn;
}
// periodly check next page button is available
// trigger lazyload to load all post
// trigger
// scroll down to trigger lazyLoad
var scrollDown = function() {
setInterval(function(){
// if pre-cached nextPageBtn is removed from dom, then rock it again
getNextPageBtn();
if (!nextPageBtn || !nextPageBtn.parentNode.parentNode) {
var sh = document.body.scrollHeight;
window.scroll(0, sh);
// if failed, retry
var retryLoadLinkTmp = document.getElementsByTagName('a');
len = retryLoadLinkTmp.length;
for (var i = 0; i < len; i++) {
var retryLoadLink = retryLoadLinkTmp[i];
if (/请重试/.test(retryLoadLink.innerHTML)){
retryLoadLink.dispatchEvent(event);
}
}
} else if (typeof countActiveXhr == 'undefined' || countActiveXhr === 0) {
main();
}
}, 1000);
}
// invoke scrollDown
scrollDown();
var main = function() {
// all array or dom obj len
var len = 0;
// new get all dl list, the post id is attached on dl element
var elements = document.getElementsByTagName('dl');
var myE = null;
// all post id (message id) to delete
var midToDel = [];
// filter dl that doesn't have feed_list_item attribute
var dlAttrFileter = '';
// catch all dom obj to array, speed up javascript
var elementArr = [];
len = elements.length;
for (var i = 0; i < len; i++ ) {
elementArr[i] = elements[i];
}
len = elementArr.length;
for (var i = 0; i < len; i++) {
myE = elementArr[i];
dlAttrFileter = myE.getAttribute('action-type');
// if has action-type attribute and it's value is equal to feed_list_item
if (dlAttrFileter && (dlAttrFileter == 'feed_list_item')) {
// push all post id in midToDel array
midToDel.push(myE.getAttribute('mid'));
}
}
var xhrs = [];
// count current active xhr, if none active, then turn to next page
window.countActiveXhr = 0;
len = midToDel.length;
for (var i = 0; i < len; i++) {
// create new xhr
xhrs[i] = new XMLHttpRequest();
xhrs[i].open('POST', 'http://weibo.com/aj/mblog/del');
// should set these headers, to simulate the xhr is generated by native script in weibo.com
xhrs[i].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
xhrs[i].setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhrs[i].onreadystatechange = function() {
if (this.readyState == 4 && (this.status == 200)) {
// decreate the active xhr counter
countActiveXhr--;
if (!countActiveXhr) {
// clear all cached xhr, prevent memory leak
xhrs = [];
// turn to next page, fire click event
nextPageBtn.dispatchEvent(event);
}
}
};
// send data, keep in mind, the _t is extra data required by weibo.com
xhrs[i].send('mid=' + midToDel[i] + '&' + '_t=0');
// increate active xhr counter
countActiveXhr++;
}
}
}
// rock it!!!!
deleteAllMyPost();