Created
October 11, 2017 06:52
-
-
Save xbdotl/4e566b210e36d60db8eb17459ca60f1c to your computer and use it in GitHub Desktop.
拉黑微博监督员,书签脚本
This file contains hidden or 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
| 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; | |
| } | |
| }(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
叫supervisor和officer都不太合适,想了想还是叫他们红卫兵吧