批量删除xing浪微bo的js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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控制台执行这段代码
// 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 = '<h1>Deleting all posts of your weibo.com, to cancel deleting, please press F5 key. <br /> 正在删除新浪微博所有的帖子,如果要停止删除,请按 F5 键.</h1>';
    masker.innerHTML = maskerText;
    masker.style.opacity = '0.7';
    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);
    // prepare click event
    var eventScroll = document.createEvent('HTMLEvents');
    eventScroll.initEvent('scroll', 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;
                var oriHref = nextPageBtn.getAttribute('href');
                var newHref = oriHref.replace(/(.*)&#038;page=.*/, "$1") + 'page=1';;
                nextPageBtn.setAttribute('href', newHref);
                nextPageBtn.setAttribute('action-data', 'page=1');
            }
        }
        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;
                var ch = document.documentElement.clientHeight;
                window.scroll(0, sh - ch - 30);
                // fire body scroll event
                // 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 &#038;&#038; (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 &#038;&#038; (this.status == 200)) {
                    // decreate the active xhr counter
                    countActiveXhr--;
                    if (!countActiveXhr) {
                        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] + '&#038;' + '_t=0');

            // increate active xhr counter
            countActiveXhr++;
        }
    }

}

// rock it!!!!
deleteAllMyPost();

蓝冰的博客看到的。

更多
4 Responses Post a comment
  1. 牧风

    这个是js强大, 我自己是用api写了个删除, 不过新浪api蛋疼,每次最多只能删除200条, nnd还要加个循环才行

  2. 比木

    嘿嘿,这个果断要分享出去。身边不少朋友都需要这个。

Leave a Reply

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS