-
Notifications
You must be signed in to change notification settings - Fork 34
/
WeiboAutoCommentTool.js
149 lines (144 loc) · 4.45 KB
/
WeiboAutoCommentTool.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
var weiboAutoCommentTool = (function(){
var running = false;
var defaultOption = {
//评论间隔时间
delay: 10000,
//评论内容
content: (function(){
return "测试时间 " + +new Date();
})()
};
var needCommentIdList = [];
var commentedIdList = [];
var dateFormat = function (fmt,date){
var o = {
"M+" : date.getMonth()+1, //月份
"d+" : date.getDate(), //日
"h+" : date.getHours(), //小时
"m+" : date.getMinutes(), //分
"s+" : date.getSeconds(), //秒
"q+" : Math.floor((date.getMonth()+3)/3), //季度
"S" : date.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
var logger = function(msg){
console.log(dateFormat('yyyy-MM-dd hh:mm:ss', new Date()) + ": " + msg);
};
var comment = function(id){
console.log("----------------------------------------->");
logger("Commenting " + id);
var feedItem = document.querySelector('div[mid="' + id + '"]');
var commentButton = feedItem.querySelector('a[action-type="fl_comment"]');
//没有打开的话,模拟点击打开
if(commentButton.parentNode.className != "curr") {
commentButton.click();
logger("Comment list is not loaded, start loading...");
}
logger("Waiting comment list loading...");
//等待评论框出现
setTimeout(function(){
var textArea = feedItem.querySelector('.W_input');
textArea.value = defaultOption.content;
var sendButton = feedItem.querySelector('.W_btn_a');
sendButton.click();
logger("Sending comment content completed.");
//折叠起来
commentButton.click();
commentedIdList.push(id);
console.log("<-----------------------------------------");
}, 3000);
};
var commentThread = function(){
logger("Comment thread started.");
var innerAction = function(){
var id = needCommentIdList.shift();
if(id) {
comment(id);
} else {
logger("WARNING: No feed to process...");
}
setTimeout(function(){
if(!running) {
logger("Comment thread action stoped, exit.");
return;
}
innerAction();
}, defaultOption.delay);
};
innerAction();
};
var isThisItemCommented = function(id){
return needCommentIdList.indexOf(id) >= 0;
};
var getAllFeeds = function(){
var ret = [];
var feedWrap = document.querySelector('[node-type="feed_list"]');
for(var c in feedWrap.children) {
var child = feedWrap.children[c];
if(typeof(child.getAttribute) === "function") {
var childId = child.getAttribute('mid');
if(!isThisItemCommented(childId)) {
ret.push(childId);
}
}
}
return ret;
};
var commentAction = function(){
if(!running) {
logger("Comment action stoped, exit.");
return;
}
var allFeeds = getAllFeeds();
//如果没有记录了,则需要滚动屏幕
if(!allFeeds.length) {
document.body.scrollTop = document.body.scrollTop + 500;
//然后再获取一次
allFeeds = getAllFeeds();
}
for(var item in allFeeds) {
var id = allFeeds[item];
if(id){
needCommentIdList.push(id);
}
}
setTimeout(function(){
commentAction();
}, 20 * 1000);
};
this.start = function(op){
needCommentIdList = [];
commentedIdList = [];
logger("WeiboAutoCommentTool start running...");
if(typeof(op) !== "undefined") {
defaultOption.delay = op.delay || defaultOption.delay;
defaultOption.content = op.content || defaultOption.content;
logger("Use option: " + JSON.stringify(defaultOption));
} else {
logger("Use default option: " + JSON.stringify(defaultOption));
}
logger("WeiboAutoCommentTool running now.");
running = true;
commentAction();
commentThread();
};
this.stop = function(){
running = false;
needCommentIdList = [];
logger("User stoped");
};
this.stat = function(){
logger("Comment queue: ");
console.log(needCommentIdList);
logger("Commented list: ");
console.log(commentedIdList);
logger("STAT: Pending comment " + needCommentIdList.length + ", Total commented " + commentedIdList.length);
};
return this;
})();