-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
191 lines (169 loc) · 4.41 KB
/
script.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
let ENDPOINT = "http://localhost:8000/endpoint/";
ENDPOINT = "https://api.petroly.co/";
const HISTORY_URL =
"https://banner9-registration.kfupm.edu.sa/StudentRegistrationSsb/ssb/registrationHistory/registrationHistory";
window.onload = function () {
document.getElementById("clone-btn").addEventListener("click", setupAndClone);
document
.getElementById("terms-btn")
.addEventListener("click", showHiddenTerms);
};
function showHiddenTerms() {
chrome.tabs.create({
url: HISTORY_URL,
});
}
function setupAndClone() {
showWaiting();
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const tab = tabs[0];
// const url = new URL(tab.url);
chrome.scripting
.executeScript({
target: { tabId: tab.id },
func: () => {
console.log(sessionStorage);
return JSON.stringify(sessionStorage);
},
})
.then(tokenCallback)
.then(getCookies)
.then(saveCookies)
.catch((err) => {
console.error(err);
showErrorAlert('Make sure you follow the instructions bellow.')
});
});
// save cookies into Petroly
}
var token = "";
function tokenCallback(res) {
token = JSON.parse(res[0].result).token;
// retreive user from Petrly as a check test
// checkApi(token);
return token;
}
function getCookies(token) {
// get cookies
return chrome.cookies.getAll({ domain: "kfupm.edu.sa" });
}
function saveCookies(cookies) {
console.log(cookies);
console.log(token);
const query = `
mutation Save($cookiesStr: String!) {
saveBannerSession(cookies: $cookiesStr)
}
`;
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `JWT ${token}`,
},
body: JSON.stringify({
query,
variables: { cookiesStr: JSON.stringify(cookies) },
}),
};
const cookiesExist = cookies.some((obj) => obj.name == "commonAuthId");
chrome.cookies.remove(
{ url: "https://login.kfupm.edu.sa/", name: "commonAuthId" },
(removedCookie) => {
console.log(removedCookie);
},
);
if (cookiesExist) {
fetch(ENDPOINT, options)
.then((response) => response.json())
.then((data) => {
// Handle the GraphQL response data here
console.log(data);
if (data.data) {
showSuccessAlert();
} else {
console.log(data.errors);
if (data.errors) {
showErrorAlert(data.errors[0].message);
}
}
})
.catch((error) => {
// Handle any errors that occurred during the request
console.error("Error:", error);
});
} else {
showErrorAlert(
"Cookies've been eaten, sign out and in again in Banner/Portal.",
);
}
}
function checkApi(token) {
const query = `
query {
me {
username
}
}
`;
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
authorization: `JWT ${token}`,
},
body: JSON.stringify({ query }),
};
fetch(ENDPOINT, options)
.then((response) => response.json())
.then((data) => {
// Handle the GraphQL response data here
console.log(data);
})
.catch((error) => {
// Handle any errors that occurred during the request
console.error("Error:", error);
});
}
function showWaiting() {
const el = document.getElementById("alerts");
const msgDiv = `
<img src="progress-bar.png">
`;
el.innerHTML = msgDiv;
}
function showSuccessAlert() {
const el = document.getElementById("alerts");
const msgDiv = `<div
class="alert alert-success alert-dismissible fade show"
role="alert"
>
<strong>Done !</strong> We cloned your Banner session,
now we'll manage it for you :)
<button
type="button"
class="btn-close"
data-bs-dismiss="alert"
aria-label="Close"
></button>
</div>
`;
el.innerHTML = msgDiv;
}
function showErrorAlert(msg) {
const el = document.getElementById("alerts");
const msgDiv = `<div
class="alert alert-danger alert-dismissible fade show"
role="alert"
>
<strong>Error!</strong> ${msg}
<button
type="button"
class="btn-close"
data-bs-dismiss="alert"
aria-label="Close"
></button>
</div>
`;
el.innerHTML = msgDiv;
}