-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_detector.js
242 lines (216 loc) · 7.91 KB
/
chat_detector.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
function stringHexEncode(s)
{
var hex, i;
var result = "";
for (i=0; i<s.length; i++) {
hex = s.charCodeAt(i).toString(16);
result += "0x" + ("000"+hex).slice(-4) + " ";
}
return result
}
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
function preprocess_string(s)
{
s = s.replace(/[\x00-\x1F\x7F-\x9F]/g, "");
s = s.replace(/[\s\'\"\:\,]*/g, "");
return s;
}
var observer = new MutationObserver(function(mutations, observer) {
// fired when a mutation occurs
mutations.forEach(function(mutation){
if (mutation.addedNodes.length > 0)
{
//console.log(mutation.addedNodes);
for(i = 0; i < mutation.addedNodes.length; ++i)
{
var item = mutation.addedNodes.item(i);
// check for gift and normal comments
if (item.className == 'jschartli' || item.className == 'jschartli new-bs-li' || item.className == 'jschartli chartli')
{
// first check if the comments needs to be read out,
// i.e. class==lw_imgs
var found = false;
var j;
for (j = 0; j < item.childElementCount; ++j)
{
var child = item.children.item(j);
if (child.childElementCount > 0)
{
var k;
for(k = 0; k < child.childElementCount; ++k)
{
var grandChild = child.children.item(k);
if (grandChild.nodeName == 'IMG' && grandChild.className == 'lw-imgs')
{
found = true;
console.log("FOUND!!!");
}
if (grandChild.nodeName == 'IMG' && grandChild.src.indexOf('yw.png') >= 0)
{
found = true;
console.log("FOUND type 2!!!");
}
}
}
}
if (found)
{
var speaker;
var j;
// find speaker name
for (j = 0; j < item.childElementCount; ++j)
{
var child = item.children.item(j);
var k;
for(k = 0; k < child.childElementCount; ++k)
{
var grandChild = child.children.item(k);
if (grandChild.className == 'nick js-nick')
{
speaker = grandChild.textContent;
}
}
}
// find speaking content
var textContent = preprocess_string(item.textContent);
if (textContent.length != 0)
{
console.log(textContent);
// if (textContent.indexOf('来到本直播间') < 0 &&
if (textContent.indexOf('次在线领鱼丸时') < 0 &&
// textContent.indexOf('次在线领鱼丸时') < 0 &&
// textContent.indexOf('高级酬勤') < 0 &&
textContent.indexOf('系统广播') < 0)
{
//speaker = speaker || " ";
textContent = textContent || " ";
chrome.runtime.sendMessage({name: speaker, content: textContent, gift: true}, function(response) {
console.log(response);
});
}
}
}
else
{
var ii;
for(ii = 0; ii < item.childElementCount; ++ii)
{
var item_ii = item.children.item(ii);
// normal and vip user
if (item_ii.className == 'text-cont' || item_ii.className == 'text-cont cqback')
{
// find childs with class=name and class=text-cont
for (j = 0; j < item_ii.childElementCount; ++j)
{
var speaker;
var child = item_ii.children.item(j);
if (child.className == 'name')
{
for (k = 0; k < child.childElementCount; ++k)
{
var grandChild = child.children.item(k);
if (grandChild.className == 'nick js-nick')
{
// omit the last semi colon
speaker = grandChild.textContent;
speaker = speaker.substring(0, speaker.length-1);
}
}
}
else if (child.className == 'text-cont')
{
var textContent = preprocess_string(child.textContent);
console.log(textContent);
// prevent reading emoji, which cause tts bugs
if (textContent.length != 0)
{
//speaker = speaker || " ";
textContent = textContent || " ";
chrome.runtime.sendMessage({name: speaker, content: textContent, gift: false}, function(response) {
// console.log(response);
});
}
}
}
}
}
}
}
if (item.className == 'my-cont')
{
for (j = 0; j < item.childElementCount; ++j)
{
var speaker;
var child = item.children.item(j);
if (child.className == 'name')
{
for (k = 0; k < child.childElementCount; ++k)
{
var grandChild = child.children.item(k);
if (grandChild.className == 'nick js-nick')
{
// omit the last semi colon
speaker = grandChild.textContent;
speaker = speaker.substring(0, speaker.length-1);
}
}
}
else if (child.className == 'm')
{
var textContent = preprocess_string(child.textContent);
console.log(textContent);
// prevent reading emoji, which cause tts bugs
if (textContent.length != 0)
{
speaker = speaker || " ";
textContent = textContent || " ";
chrome.runtime.sendMessage({name: speaker, content: textContent, gift: false}, function(response) {
console.log(response);
});
}
}
}
}
}
}
}
);
// ...
});
var observing = true;
// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(document, {
subtree: true,
attributes: true,
childList: true
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (!sender.tab)
{
if (request.check_tts_enabled)
{
sendResponse({code: observing});
}
// from the extension itself
else if (request.enable_tts)
{
observer.observe(document, {
subtree: true,
attributes: true,
childList: true
});
observing = true;
sendResponse({code: "connected"});
}
else {
observer.disconnect();
observing = false;
sendResponse({code: "disconnected"});
}
}
});