-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
101 lines (97 loc) · 3.06 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
let resId1, resId2;
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const { status } = message;
if (status === 200) {
// Change Status ( confirm, temporary, cancel )
resId1 = message.resId1;
resId2 = message.resId2;
loopForMode(message);
} else if (status === 'save') {
chrome.tabs.create(
{
url: 'https://salto.uno-r.edu.ph/IMC/Reservation/',
active: false
},
(newTab) => {
// Creates new tab to save edited values
chrome.scripting.executeScript({
target: { tabId: newTab.id },
func: () => window.close(),
});
}
);
} else if (status === 300) {
chrome.tabs.create({ url: 'equipments.html' });
resId1 = message.resId1;
resId2 = message.resId2;
} else if (status === 'add') {
sendResponse('done');
loopForEquip(message);
} else if (status === 'close') {
async function closeTabs() {
let tabs = await chrome.tabs.query({
url: 'https://salto.uno-r.edu.ph/IMC/Reservation/Edit?ResID=*',
currentWindow: true,
});
for (let tab of tabs) {
chrome.tabs.remove(tab.id);
}
}
closeTabs();
}
});
async function loopForMode(message) {
for (let i = resId1; i <= resId2; i++) {
await automateMode(i, message);
}
}
async function loopForEquip(message) {
for (let i = resId1; i <= resId2; i++) {
await automateEquip(i, message);
}
}
function automateMode(id, message) {
return new Promise((resolve) => {
chrome.tabs.create(
{
url: `https://salto.uno-r.edu.ph/IMC/Reservation/Edit?ResID=${id}`,
active: false
},
(newTab) => {
chrome.scripting.executeScript(
{
target: { tabId: newTab.id },
files: ['automation.js'],
},
async () => {
let response = await chrome.tabs.sendMessage(
newTab.id,
message
);
resolve(response);
}
);
}
);
});
}
function automateEquip(id, message) {
return new Promise((resolve) => {
chrome.tabs.create(
{
url: `https://salto.uno-r.edu.ph/IMC/Reservation/Edit?ResID=${id}`,
active: false
},
async (tab) => {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['add-equipments.js'],
});
let response = await chrome.tabs.sendMessage(tab.id, {
equipments: message.equipments,
});
resolve(response);
}
);
});
}