forked from gilatnaveh1/time-watch-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
158 lines (146 loc) · 4.78 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
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
// background.js
let connectedToPopup = false;
chrome.runtime.onConnect.addListener(function () {
console.log("Connected .....");
if (!connectedToPopup) {
connectedToPopup = true;
chrome.runtime.onMessage.addListener(handleMessage);
}
});
const handleMessage = async function (msg, sender) {
if (!msg || msg.to !== "TW_BACKGROUND") {
console.log("dropping message - not to me");
return;
}
console.log("message received " + msg.body);
switch (msg.body) {
case "SET_REMINDER":
await addAlarm(msg.date);
return;
case "START":
const tab = await login();
await addOnUpdatedListener(tab);
return;
case "FINISH":
removeOnUpdateListener(sender);
return
default:
console.log("dropping invalid message");
return;
}
}
const removeOnUpdateListener = function (tabId) {
chrome.tabs.onUpdated.removeListener(arguments.callee)
}
const alarmName = "TimeWatchAlarm";
const addAlarm = async function (date) {
let num = new Date(date).getTime();
console.log("A reminder will be shown at " + new Date(date));
chrome.alarms.create(alarmName, {when: num});
await chrome.alarms.onAlarm.addListener(alarmListener);
};
async function alarmListener(alarm) {
if (alarm.name !== alarmName) {
return;
}
await chrome.alarms.clearAll();
await chrome.alarms.onAlarm.removeListener(alarmListener);
chrome.notifications.create('tw-monthly', {
type: 'basic',
requireInteraction: true,
iconUrl: 'assets/images/icon512.png',
title: 'Time watch puncher',
message: 'It\'s time..',
buttons: [{
title: 'start'
}]
});
await chrome.notifications.onButtonClicked.addListener(notificationListener);
await chrome.notifications.onClosed.addListener(async ()=>{
await addAlarm();
});
}
async function notificationListener(notificationId, btnIdx) {
if (notificationId === "tw-monthly") {
if (btnIdx === 0) {
await chrome.notifications.onButtonClicked.removeListener(notificationListener);
const tab = await login();
await addOnUpdatedListener(tab);
}
await addAlarm();
}
}
const login = async function () {
try {
console.log("login - start");
const tab = await chrome.tabs.create({
url: 'https://checkin.timewatch.co.il/punch/punch.php',
active: true
});
await ensureConditionIsSet(async () => {
let tempTab = await chrome.tabs.get(tab.id);
return tempTab.status === "complete";
})
await chrome.debugger.attach({tabId:tab.id}, "1.2", function(debugg) {
chrome.debugger.sendCommand({tabId:tab.id}, "Input.dispatchMouseEvent",
{
type:"mousePressed",
button: "left",
x:parseFloat("200"),
y:parseFloat("200")
});
});
await chrome.scripting.executeScript(
{
target: {tabId: tab.id},
files: ['inject-login.js'],
});
console.log("login - end");
return tab;
} catch (e) {
console.log(e.message);
}
}
const goToPunchData = async function (tabId) {
console.log("goToPunchData - start");
let tempTab = await chrome.tabs.get(tabId);
while (tempTab.status !== "complete") {
tempTab = await chrome.tabs.get(tabId);
}
await chrome.scripting.executeScript(
{
target: {tabId: tabId},
files: ['inject-go-to-punch-data.js'],
});
console.log("goToPunchData - end");
}
const punchWholeMonth = async function (tabId) {
await chrome.scripting.executeScript(
{
target: {tabId: tabId},
files: ['inject-start.js'],
});
}
const addOnUpdatedListener = async function (tab) {
chrome.tabs.onUpdated.addListener(async function (tabId, changeInfo) {
if (tabId === tab.id && changeInfo.status && changeInfo.status === "complete") {
const tab = await chrome.tabs.get(tabId);
await chrome.debugger.detach({tabId});
if (tab.url && tab.url.match(".*punch/punch2_e.php.*")) {
await goToPunchData(tabId);
return;
}
if (tab.url && tab.url.match(".*punch/editwh.php.*")) {
await punchWholeMonth(tabId);
}
}
});
}
async function ensureConditionIsSet(callback) {
return new Promise(function (resolve) {
(async function waitForFoo() {
if (await callback() === true) return resolve();
setTimeout(waitForFoo, 30);
})();
});
}