-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
105 lines (89 loc) · 2.49 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
104
105
const host = 'https://awbw.amarriner.com/index.php';
chrome.runtime.onInstalled.addListener(({
reason
}) => {
if (reason === 'install') {
chrome.storage.local.set({
apiSuggestions: ["cookies", "notifications", "alarms"]
});
}
});
const notificationToLogIn = (callback) => {
chrome.notifications.create({
iconUrl: 'images/icon-128.png',
type: 'basic',
message: 'you need to login into AWBW and click the extention to restart',
title: 'AWBW Notifier'
}, e => {
callback();
})
}
const notificationOfTurn = (username, callback) => {
chrome.notifications.create({
iconUrl: 'images/icon-128.png',
type: 'basic',
message: 'Hey ' + username + ' :)\nIt\'s your turn',
title: 'AWBW Notifier'
}, e => {
callback();
})
}
const notificationNonTurn = (callback) => {
chrome.notifications.create({
iconUrl: 'images/icon-128.png',
type: 'basic',
message: 'It\'s not your turn',
title: 'AWBW Notifier'
}, e => {
callback();
})
}
const fetchHtml = async () => {
const response = await fetch(host, {
method: 'GET'
});
return await response.text();
}
const start = (action) => {
chrome.cookies.get({
url: 'http://awbw.amarriner.com',
name: 'PHPSESSID'
}, cookie => {
if (cookie) {
const phpsessid = cookie.value;
chrome.cookies.get({
url: 'http://awbw.amarriner.com',
name: 'awbw_username'
}, async cookie => {
if (cookie) {
const username = cookie.value;
const html = await fetchHtml();
const alerts = html.match(/<span class="total-alerts">[0-9]*<\/span>/);
if (alerts && Array.isArray(alerts)) {
notificationOfTurn(username, ()=>{});
} else if(action == 'reset') {
notificationNonTurn(()=>{});
}
} else {
notificationToLogIn(()=>{});
}
});
} else {
notificationToLogIn(()=>{});
}
});
}
start();
chrome.action.onClicked.addListener(tab => {
if (tab) {
start('reset');
}
});
chrome.alarms.create("2min", {
delayInMinutes: 1
});
chrome.alarms.onAlarm.addListener(function (alarm) {
if (alarm.name === "2min") {
start();
}
});