├── screen-shot.jpg ├── README.md └── WeiboAutoCommentTool.js /screen-shot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrhuo/WeiboAutoCommentTool/HEAD/screen-shot.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WeiboAutoCommentTool 2 | 3 | 微博自动评论工具。 4 | *警告:谨慎使用,仅供学习,不要做违法乱纪的事情。* 5 | 6 | ## 使用方法 7 | 8 | * 登录微博 9 | * 切换到发现页面 [https://d.weibo.com](https://d.weibo.com) 10 | * F12打开浏览器控制台,把脚本内容全部复制粘贴到控制台,按回车 11 | * 然后执行以下代码: 12 | 13 | ``` 14 | weiboAutoCommentTool.start({ 15 | //评论内容,不填默认当前时间 16 | content: "哇,不错不错,很赞!" , 17 |  //评论间隔时间,默认秒 18 |  delay: 10 * 1000 19 | }); 20 | ``` 21 | ## 其他工具 22 | ``` 23 | //停止自动评论 24 | weiboAutoCommentTool.stop() 25 | ``` 26 | 27 | ``` 28 | //查看评论统计 29 | weiboAutoCommentTool.stat() 30 | ``` 31 | ## 效果截图 32 | ![效果图](screen-shot.jpg) 33 | 34 | ## 测试日志(下列记录来自控制台复制) 35 | ``` 36 | weiboAutoCommentTool.start({ 37 | content: '哇,真不可思议~~' 38 | }); 39 | 40 | 2018-05-07 13:08:48: WeiboAutoCommentTool start running... 41 | 2018-05-07 13:08:48: Use option: {"delay":10000,"content":"哇,真不可思议~~"} 42 | 2018-05-07 13:08:48: WeiboAutoCommentTool running now. 43 | 2018-05-07 13:08:48: Comment thread started. 44 | -----------------------------------------> 45 | 2018-05-07 13:08:48: Commenting 4236962166085444 46 | 2018-05-07 13:08:48: Comment list is not loaded, start loading... 47 | 2018-05-07 13:08:48: Waiting comment list loading... 48 | 2018-05-07 13:08:51: Sending comment content completed. 49 | <----------------------------------------- 50 | -----------------------------------------> 51 | 2018-05-07 13:08:58: Commenting 4236975416123726 52 | 2018-05-07 13:08:58: Comment list is not loaded, start loading... 53 | 2018-05-07 13:08:58: Waiting comment list loading... 54 | 2018-05-07 13:09:01: Sending comment content completed. 55 | <----------------------------------------- 56 | -----------------------------------------> 57 | 2018-05-07 13:09:08: Commenting 4236986120511160 58 | 2018-05-07 13:09:08: Comment list is not loaded, start loading... 59 | 2018-05-07 13:09:08: Waiting comment list loading... 60 | 2018-05-07 13:09:11: Sending comment content completed. 61 | <----------------------------------------- 62 | 63 | weiboAutoCommentTool.stat(); 64 | 2018-05-07 13:09:13: Comment queue: 65 | ["4236648663107765", "4236692271631492", "4236733615086129", "4236813700804304", "4236618468476778", "4236037263784663", "4235992158606659", "4236962166085444", "4236975416123726"] 66 | 2018-05-07 13:09:13: Commented list: 67 | ["4236962166085444", "4236975416123726", "4236986120511160"] 68 | 2018-05-07 13:09:13: STAT: Pending comment 9, Total commented 3 69 | 70 | -----------------------------------------> 71 | 2018-05-07 13:09:18: Commenting 4236648663107765 72 | 2018-05-07 13:09:18: Comment list is not loaded, start loading... 73 | 2018-05-07 13:09:18: Waiting comment list loading... 74 | 2018-05-07 13:09:21: Sending comment content completed. 75 | <----------------------------------------- 76 | 77 | weiboAutoCommentTool.stop(); 78 | 2018-05-07 13:09:21: User stoped 79 | 2018-05-07 13:09:28: Comment action stoped, exit. 80 | 2018-05-07 13:09:28: Comment thread action stoped, exit. 81 | ``` 82 | -------------------------------------------------------------------------------- /WeiboAutoCommentTool.js: -------------------------------------------------------------------------------- 1 | var weiboAutoCommentTool = (function(){ 2 | var running = false; 3 | var defaultOption = { 4 | //评论间隔时间 5 | delay: 10000, 6 | //评论内容 7 | content: (function(){ 8 | return "测试时间 " + +new Date(); 9 | })() 10 | }; 11 | var needCommentIdList = []; 12 | var commentedIdList = []; 13 | var dateFormat = function (fmt,date){ 14 | var o = { 15 | "M+" : date.getMonth()+1, //月份 16 | "d+" : date.getDate(), //日 17 | "h+" : date.getHours(), //小时 18 | "m+" : date.getMinutes(), //分 19 | "s+" : date.getSeconds(), //秒 20 | "q+" : Math.floor((date.getMonth()+3)/3), //季度 21 | "S" : date.getMilliseconds() //毫秒 22 | }; 23 | if(/(y+)/.test(fmt)) 24 | fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); 25 | for(var k in o) 26 | if(new RegExp("("+ k +")").test(fmt)) 27 | fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 28 | return fmt; 29 | } 30 | var logger = function(msg){ 31 | console.log(dateFormat('yyyy-MM-dd hh:mm:ss', new Date()) + ": " + msg); 32 | }; 33 | var comment = function(id){ 34 | console.log("----------------------------------------->"); 35 | logger("Commenting " + id); 36 | var feedItem = document.querySelector('div[mid="' + id + '"]'); 37 | var commentButton = feedItem.querySelector('a[action-type="fl_comment"]'); 38 | //没有打开的话,模拟点击打开 39 | if(commentButton.parentNode.className != "curr") { 40 | commentButton.click(); 41 | logger("Comment list is not loaded, start loading..."); 42 | } 43 | logger("Waiting comment list loading..."); 44 | //等待评论框出现 45 | setTimeout(function(){ 46 | var textArea = feedItem.querySelector('.W_input'); 47 | textArea.value = defaultOption.content; 48 | 49 | var sendButton = feedItem.querySelector('.W_btn_a'); 50 | sendButton.click(); 51 | logger("Sending comment content completed."); 52 | 53 | //折叠起来 54 | commentButton.click(); 55 | commentedIdList.push(id); 56 | console.log("<-----------------------------------------"); 57 | }, 3000); 58 | }; 59 | var commentThread = function(){ 60 | logger("Comment thread started."); 61 | 62 | var innerAction = function(){ 63 | var id = needCommentIdList.shift(); 64 | if(id) { 65 | comment(id); 66 | } else { 67 | logger("WARNING: No feed to process..."); 68 | } 69 | setTimeout(function(){ 70 | if(!running) { 71 | logger("Comment thread action stoped, exit."); 72 | return; 73 | } 74 | innerAction(); 75 | }, defaultOption.delay); 76 | }; 77 | innerAction(); 78 | }; 79 | var isThisItemCommented = function(id){ 80 | return needCommentIdList.indexOf(id) >= 0; 81 | }; 82 | var getAllFeeds = function(){ 83 | var ret = []; 84 | var feedWrap = document.querySelector('[node-type="feed_list"]'); 85 | for(var c in feedWrap.children) { 86 | var child = feedWrap.children[c]; 87 | if(typeof(child.getAttribute) === "function") { 88 | var childId = child.getAttribute('mid'); 89 | if(!isThisItemCommented(childId)) { 90 | ret.push(childId); 91 | } 92 | } 93 | } 94 | return ret; 95 | }; 96 | var commentAction = function(){ 97 | if(!running) { 98 | logger("Comment action stoped, exit."); 99 | return; 100 | } 101 | var allFeeds = getAllFeeds(); 102 | //如果没有记录了,则需要滚动屏幕 103 | if(!allFeeds.length) { 104 | document.body.scrollTop = document.body.scrollTop + 500; 105 | //然后再获取一次 106 | allFeeds = getAllFeeds(); 107 | } 108 | for(var item in allFeeds) { 109 | var id = allFeeds[item]; 110 | if(id){ 111 | needCommentIdList.push(id); 112 | } 113 | } 114 | setTimeout(function(){ 115 | commentAction(); 116 | }, 20 * 1000); 117 | }; 118 | this.start = function(op){ 119 | needCommentIdList = []; 120 | commentedIdList = []; 121 | logger("WeiboAutoCommentTool start running..."); 122 | if(typeof(op) !== "undefined") { 123 | defaultOption.delay = op.delay || defaultOption.delay; 124 | defaultOption.content = op.content || defaultOption.content; 125 | 126 | logger("Use option: " + JSON.stringify(defaultOption)); 127 | } else { 128 | logger("Use default option: " + JSON.stringify(defaultOption)); 129 | } 130 | 131 | logger("WeiboAutoCommentTool running now."); 132 | running = true; 133 | commentAction(); 134 | commentThread(); 135 | }; 136 | this.stop = function(){ 137 | running = false; 138 | needCommentIdList = []; 139 | logger("User stoped"); 140 | }; 141 | this.stat = function(){ 142 | logger("Comment queue: "); 143 | console.log(needCommentIdList); 144 | logger("Commented list: "); 145 | console.log(commentedIdList); 146 | logger("STAT: Pending comment " + needCommentIdList.length + ", Total commented " + commentedIdList.length); 147 | }; 148 | return this; 149 | })(); --------------------------------------------------------------------------------