-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.js
205 lines (180 loc) · 6.39 KB
/
interface.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
// Отрабатывать скрипт только на странице с комментариями
if (document.location.href.match("leprosorium.ru/comments/") ||
document.location.href.match("leprosorium.ru/my/inbox/")) {
console.log('Найден пост с комментариями леперов');
refreshAllUsers();
// Запрашиваем список людей из имеющегося списка
function refreshAllUsers() {
chrome.runtime.sendMessage({
cmd: 'get_user_list'
}, function(response) {
if (response && response.status=="OK") {
// Подсвечиваем М и Ж из полученного списка
_.forIn(response.userlist, function(value, key) {
setUserStatus(key, true);
});
}
});
}
// Ищем всех юзеннэймов на странице, оставивших комментарии
findUserComments('*', function(comment, username) {
var sex = $(comment).text()=='Написал';
// Вешаем обработку клика на "Написал"/"Написала" для всех людей на странице
$(comment).on('click', function(e) {
chrome.runtime.sendMessage({
cmd: 'click',
username: username,
sex: sex
}
);
});
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
// console.log(request);
if (request && request.cmd) {
switch(request.cmd) {
case 'get_by_keywords':
var userlist = {};
var comments = $('.c_body');
var i=0;
var keywords = '(' + _.filter(request.keywords, function(item) {return item!=''} ).join('|') + ')';
// console.log(keywords);
var re = new RegExp(keywords,'i');
// console.log(re);
$(comments).each(function(idx) {
var text = this.innerText;
i++;
// console.log(text.match(re));
if (text.match(re)) {
// console.log(text);
// console.log(i + ': ' + $(this).text());
var tmp = $(this.nextSibling).find('.c_wrote')[0];
// console.log($(tmp));
var sex = $(tmp).text()=='Написал';
var j=0;
while(tmp.className!='c_user' && j<4) {
tmp = tmp.nextSibling;
j++;
}
// console.log(tmp.innerText);
if (tmp.className=='c_user') {
userlist[tmp.innerText] = {name:tmp.innerText, sex:sex};
// console.log(tmp.innerText);
}
}
});
sendResponse({status:'OK', userlist:userlist});
break;
case 'refresh':
refreshAllUsers()
break;
case 'get_user_list':
var userlist = {};
findUserComments('*', function(comment, username) {
var sex = $(comment).text()=='Написал';
userlist[username] = {sex: sex, name: username};
});
sendResponse({
status:'OK', userlist: userlist
});
break;
case 'deleted':
setUserStatus(request.username, false);
break;
case 'added':
setUserStatus(request.username, true);
break;
case 'clear':
$(".c_wrote").css('background','');
$(".c_wrote").css('color','darkblue');
$(".c_wrote").each( function(idx) {
if ($(this).text()=="Написала") {
$(this).css('color','red');
}
});
break;
}
}
});
}
// Ищет все комментарии пользователя с именем username
// если username == '*' возвращает все коментарии всех пользователей
function findUserComments(username, callback) {
$(".c_wrote").each(function(item) {
var i=0;
var tmp = this.nextSibling;
var sex = $(this).text()=='Написал';
while(tmp.className!='c_user' && i<3) {
tmp = tmp.nextSibling;
i++;
}
if (tmp.className=='c_user' && (tmp.innerText==username || username=='*')) {
callback(this, tmp.innerText, sex);
}
});
}
// Инвертирует подсветку надписи Написал(а) в зависимости от статуса
function setUserStatus(username, status) {
if (status) {
findUserComments(username, function(object, name, sex) {
$(object).css('background', sex ? 'darkblue' : 'red');
$(object).css('color', 'white');
});
} else {
findUserComments(username, function(object, name, sex) {
$(object).css('background', '');
$(object).css('color', sex ? 'darkblue' : 'red');
});
}
}
if (document.location.href.match(/^https:\/\/leprosorium.ru\/users\/.+\/comments\/$/)) {
var usernameTag = $('.b-header_tagline > a');
var usernameTag2 = $('.b-user_name-link');
// console.log($(usernameTag).text());
// console.log($(usernameTag2).text());
if ($(usernameTag).text() == $(usernameTag2).text()) {
console.log('Найден список комментариев: ' + $(usernameTag).text());
var interval = setInterval(function() {
if ($("#js-loader").css('display')=='none') {
$('.vote_result').css('width','70px');
var statTags = $('.vote_result');
var i=0;
var comments = {};
statTags.each(function () {
var commentId = $(this).attr('onclick').match(/\d+/);
comments[parseInt(commentId)] = parseInt($(this).text());
// console.log(++i + '. ' + commentId + ': ' + $(this).text());
});
clearInterval(interval);
chrome.runtime.sendMessage({
cmd: 'get_comments_stat',
comments: comments
}, function(response) {
// console.log(response);
if (response && response.status=='OK') {
// _.forIn(response.userlist, function(value, key) {
// // setUserStatus(key, true);
// });
var color;
statTags.each(function () {
var commentId = $(this).attr('onclick').match(/\d+/);
// comments[parseInt(commentId)] = parseInt($(this).text());
if (response.comments[commentId]!=undefined && parseInt(response.comments[commentId])!=NaN) {
var delta = comments[commentId] - parseInt(response.comments[commentId]);
// console.log(delta);
//$(this).insertBefore(delta);
if (delta!=NaN && delta!=0) {
color = (delta>0) ? 'darkgreen' : 'red';
$(this).html(
$(this).text() + ' [<span style="color:'+color+'">' + ((delta>0)?'+':'') + delta + '</span>]'
);
}
}
// console.log(++i + '. ' + commentId + ': ' + $(this).text());
});
}
}.bind(this));
}
}, 200);
}
}