Skip to content

Instantly share code, notes, and snippets.

@crimx
Created February 2, 2016 14:23
Show Gist options
  • Select an option

  • Save crimx/90b9fcd145137b3afa1c to your computer and use it in GitHub Desktop.

Select an option

Save crimx/90b9fcd145137b3afa1c to your computer and use it in GitHub Desktop.

Revisions

  1. crimx created this gist Feb 2, 2016.
    170 changes: 170 additions & 0 deletions weibo-hide.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    // 把所有微博设为“自己可见”。基于档案娘助手修改,提高稳定减少卡死。
    $(function() {
    var mids = [];
    var pageRecord = [];
    var oldSearch = window.location.search;
    var page = getPage();

    // 获得当前页码
    function getPage() {
    return parseSearch().page;
    }

    function parseSearch() {
    var obj = {}
    window.location.search
    .slice(1)
    .split('&')
    .forEach(function(item) {
    var t = item.split('=');
    obj[t[0]] = isNaN(parseInt(t[1])) ? t[1] : parseInt(t[1]);
    })
    return obj
    }

    // 把一个微博设为“自己可见”
    function hideOne(mid) {
    return new Promise(function(resolve, reject) {
    $.ajax({
    url: 'http://weibo.com/p/aj/v6/mblog/modifyvisible?ajwvr=6&domain=100505&__rnd=' + Math.random(),
    type: 'POST',
    async: true,
    dataType: 'json',
    // 1自己可见 2好友可见
    data: {
    visible: 1,
    mid: mid
    },
    fail: function(result) {
    reject(result);
    },
    success: function(result) {
    resolve(result);
    }
    });
    })
    }

    // 把一整页的微博设为“自己可见”
    function hidePage () {
    var faildCount = 0;
    var totalCount = 0;
    var p = page;

    console.log('Page' + p + ': Sending hide-request');
    return new Promise(function(resolve, reject) {
    if (mids[p].length <= 0) {
    resolve(0);
    return;
    }

    pageRecord[p] = {
    finished: false,
    action: resolve
    };

    // 超时还没接收完就直接跳下一页
    setTimeout(function() {
    if (pageRecord[page] && !pageRecord[page].finished) {
    pageRecord[page].finished = true;
    pageRecord[page].action();
    }
    }, (mids[p].length+2)*2000);

    // 3s 发一个请求
    var time = 2000;
    mids[p].forEach(function(m) {
    setTimeout(function() {
    console.log('Page' + p + ': requests sent');
    hideOne(m).then(function() {
    totalCount += 1;
    console.log('Page' + p + ': requests reveived');
    // 偶尔会收不到一个
    if (totalCount >= mids[p].length - 1) {
    if (!pageRecord[p].finished) {
    pageRecord[p].finished = true;
    pageRecord[p].action();
    }
    }
    }, function() {
    faildCount += 1;
    })
    }, time);
    time += 2000;
    })
    });
    }

    // 跳到页尾
    function pageEnd() {
    return new Promise(function (resolve, reject) {
    var interval = setInterval(function() {
    $('html, body').animate({ scrollTop: $(document).height() }, 'fast');
    if ($('.W_pages .page.next').length > 0) {
    resolve();
    clearInterval(interval);
    }
    }, 2000);
    })
    }

    // 跳到下一页
    function nextPage() {
    var $np = $('.W_pages .page.next');
    if ($np.length > 0) {
    $np[0].click();
    return true;
    }
    return false;
    }

    // 获得当前页面所有微博的 mid 值
    function getMid() {
    mids[page] = [];
    // 当前页面所有的微博
    var cardwrap = $('.WB_feed_profile div.WB_cardwrap.WB_feed_type');
    if (cardwrap.length < 0) {
    return;
    }

    cardwrap.each(function() {
    var mid = $(this).attr('mid');
    //mid 正确,且不是“自己可见”
    if (!isNaN(mid) && !$(this).find('.icon_type_self').length) {
    mids[page].push(mid)
    return true;
    }
    });
    }

    // 监听页面跳转
    setInterval(function(){
    if (oldSearch !== window.location.search) {
    oldSearch = window.location.search;
    page = getPage();

    // 休息 15s 再处理下一页
    console.log('Page' + page + ': wait 15s');
    setTimeout(function() {
    main();
    }, 15000);
    }
    }, 1000);

    function main() {
    console.log('Page' + page + ': Weibo Hide Initiated');
    pageEnd()
    .then(getMid)
    .then(hidePage)
    .then(function() {
    console.log('==== page:' + page + ' mids: ' + mids[page].length + ' ====');
    if (!nextPage()) {
    console.log('success');
    }
    });
    }

    // 开始
    main();

    });