当前位置 : 主页 > 网络编程 > JavaScript >

拉黑微博监督员,书签脚本

来源:互联网 收集:自由互联 发布时间:2021-06-30
block-weibo-red-guards.js javascript:!function() { let WEIBO_RED_GUARDS_LIST = 'https://raw.githubusercontent.com/yu961549745/WeiboBlackList/master/list.txt' , BLOCK_URI = '//weibo.com/aj/filter/block?ajwvr=6' , BLUEBIRD_URI = '//cdn.static
block-weibo-red-guards.js
javascript:
!function() {
  let WEIBO_RED_GUARDS_LIST = 'https://raw.githubusercontent.com/yu961549745/WeiboBlackList/master/list.txt'
    , BLOCK_URI = '//weibo.com/aj/filter/block?ajwvr=6'
    , BLUEBIRD_URI = '//cdn.staticfile.org/bluebird/3.5.0/bluebird.min.js'
    , UID_REGEXP = new RegExp(/^\d+$/)
    , MAX_CONCURRENCY = 1
    , PER_REQ_DELAY_MS = 2000
    , RESULT_SET = []
  ;

  if(!ensureDomain('weibo.com')) {
    return;
  }
  
  loadScript(BLUEBIRD_URI, doBatchBlock);
  
  function ensureDomain(domainName, schema) {
    if(domainName != location.host) {
      alert('请在 ' + domainName + ' 域名下执行本脚本');
      return false;
    }
    return true;
  }

  function loadScript(uri, callbackFn) {
    var body= document.getElementsByTagName('body')[0];
    var script= document.createElement('script');
    script.type= 'text/javascript';
    script.src= uri;
    body.appendChild(script);

    script.onload= callbackFn || noop;
    return script;
  }
  
  function noop() {}

  function isValidUid(uid) {
    return UID_REGEXP.test(uid);
  }
  
  function blockUserByUid(uid) {
    if(!isValidUid(uid)) {
      console.debug('invalid uid %d, skiped', uid);
      return Promise.resolve({
        code: -1,
        msg: 'skip'
      });
    }
    console.debug('processing uid: %d', uid);

    return Promise.delay(PER_REQ_DELAY_MS)
      .then(() => uid)
      .then(doBlockUserByUid);
  }
  
  function doBlockUserByUid(uid) {
    return sendBlockRequest(uid)
      .then(result => {
        RESULT_SET.push(result);
        return result;
      })
    ;
  }
  
  function sendBlockRequest(uid) {
    let xhr = new XMLHttpRequest();
    return new Promise((resolveFn, rejectFn) => {
      xhr.open('POST', BLOCK_URI, true);
      xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      xhr.send('uid=' + uid + '&filter_type=1&status=1&interact=1&follow=1');
      xhr.onreadystatechange = function() {
        let defaultData = {
          code: -1,
          msg: xhr.responseText
        };

        if (4 != xhr.readyState || 200 != xhr.status) {
          console.debug('xhr request failed: %d, resp: %s', xhr.status, xhr.responseText);
          return resolveFn(defaultData);
        }

        try {
          data = JSON.parse(xhr.responseText);
        } catch (err) { }

        resolveFn(data);
      }
    });
  }
  
  function fetchContentText(uri) {
    return Promise.resolve(
      fetch(uri).then(data => data.text())
    );
  }
  
  function doBatchBlock() {
    fetchContentText(WEIBO_RED_GUARDS_LIST)
    .then(data => data.split('\n'))
    .map(blockUserByUid, {concurrency: MAX_CONCURRENCY})
    .catch(err => {
      console.error(err);
    })
    .finally(printBlockResult)
    ;
  }
  
  function printBlockResult() {
    let totalCount = RESULT_SET.length
      , failedSet = RESULT_SET.filter(isBlockFailed)
      , failedCount = failedSet.length
    ;
    
    console.log('共处理%d条拉黑请求, 失败%d条', totalCount, failedCount);
    if(0 === failedCount) {
      return;
    }
    failedSet.forEach(result => {
      console.log('code: %d \nmsg: %s', result.code, result.msg);
    });
  }
  
  function isBlockFailed(resp) {
    return 100000 !== resp.code;
  }
}();
网友评论