-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
53 lines (51 loc) · 2.28 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
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {hostEquals: 'noi.openjudge.cn'}
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
chrome.storage.sync.set({students: []});
chrome.storage.sync.set({problems: []});
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.operation) {
case 'add_student':
console.log('add student');
chrome.storage.sync.get('students', data => {
let new_student = {userId: request.userId, userName: request.userName}
data.students.push(new_student);
chrome.storage.sync.set({students: data.students});
});
break;
case 'del_student':
console.log('del student');
chrome.storage.sync.get('students', data => {
data.students = data.students.filter(s => s.userId != request.userId || s.userName != request.userName);
chrome.storage.sync.set({students: data.students});
});
break;
case 'add_problem':
console.log('add ' + request.problems.length + ' problem');
chrome.storage.sync.get('problems', data => {
data.problems = data.problems.concat(request.problems);
chrome.storage.sync.set({problems: data.problems});
});
break;
case 'del_problem':
console.log('del ' + request.problems.length + ' problem');
chrome.storage.sync.get('problems', data => {
request.problems.forEach(p => {
data.problems = data.problems.filter(pp => pp.chapterId != p.chapterId || pp.problemId != p.problemId);
});
chrome.storage.sync.set({problems: data.problems});
});
break;
}
}
);