-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
105 lines (86 loc) · 3.02 KB
/
background.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
var userlist = get_userlist();
function get_userlist() {
var userlist = localStorage['userlist'];
if (!userlist || userlist=='') {
userlist = {};
localStorage['userlist'] = JSON.stringify(userlist);
} else {
userlist = JSON.parse(userlist);
}
return userlist;
}
var masklist = get_masklist();
function get_masklist() {
var masklist = localStorage['masklist']
if (!masklist || masklist=='' || masklist=='[]') {
masklist = ['inbox', 'инбокс', 'запиши', 'впиши'];
localStorage['masklist'] = JSON.stringify(masklist);
} else {
masklist = JSON.parse(masklist);
}
return masklist;
}
var comments = get_comments();
function get_comments() {
var comments = localStorage['comments'];
if (!comments || comments=='') {
comments = {};
localStorage['comments'] = JSON.stringify(comments);
} else {
comments = JSON.parse(comments);
}
return comments;
}
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
// console.log(request);
if (request && request.cmd) {
switch(request.cmd) {
case 'get_user_list':
userlist = get_userlist();
sendResponse({status:'OK', userlist});
break;
case 'click':
var username = request.username;
var sex = request.sex;
userlist = get_userlist();
if (!userlist[username]) {
userlist[username] = {name:username, sex:sex};
// sendResponse({status:'added'});
forTabsWithUrl("leprosorium.ru/comments/",
function(tab) {
messageToTab(tab.id, {cmd:'added', username:username});
}.bind(this)
);
} else {
delete userlist[username];
// sendResponse({status:'deleted'});
forTabsWithUrl("leprosorium.ru/comments/",
function(tab) {
messageToTab(tab.id, {cmd:'deleted', username:username});
}.bind(this)
);
}
userlist = JSON.stringify(userlist);
localStorage['userlist'] = userlist;
break;
case 'copy':
userlist = get_userlist();
//var userArray = _.sortBy(userlist, ['sex', 'name']);
var userArray = _.keys(userlist).join(', ');
var input = document.createElement('textarea');
document.body.appendChild(input);
input.value = userArray;
input.focus();
input.select();
document.execCommand('Copy');
input.remove();
break;
case 'get_comments_stat':
var comments = get_comments();
sendResponse({status:'OK', comments:comments});
comments = JSON.stringify(request.comments);
localStorage['comments'] = comments;
break;
}
}
});