-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
299 lines (241 loc) · 9.39 KB
/
Copy pathscript.js
File metadata and controls
299 lines (241 loc) · 9.39 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: magic;
let email = ""
let password = ""
let runLoadSite = true
let widgetRawInput = args.widgetParameter;
if (widgetRawInput !== null) {
widgetInput = widgetRawInput.toString().split(',')
email = widgetInput[0]
password = widgetInput[1]
}else{
runLoadSite = false
if(config.runsInWidget){
init(false, null, 'Bitte gib deine E-Mail und dein Passwort für Dualis im Widget-Parameter an')
}
}
async function loadSite() {
if(email.length === 0 || password.length === 0){
init(false, null, 'Bitte gib vollständige Daten im Widget-Parameter an')
}else{
let data;
let url='https://dualis.dhbw.de/scripts/mgrqispi.dll?APPNAME=CampusNet&PRGNAME=EXTERNALPAGES&ARGUMENTS=-N000000000000001,-N000324,-Awelcome'
var wbv = new WebView()
await wbv.loadURL(url)
//javasript to grab data from the website
let jsc = `
var arr = new Array()
document.getElementById('field_user').value = "${email}"
document.getElementById('field_pass').value = "${password}"
document.getElementById('cn_loginForm').submit()
`
//Run the javascript
await wbv.evaluateJavaScript(jsc)
await wbv.waitForLoad()
let tm = new Timer()
tm.timeInterval = 2000
tm.schedule(function() {
let jsc2 = `document.getElementsByClassName('link000310')[0].click()`
wbv.evaluateJavaScript(jsc2)
wbv.waitForLoad().then(r => { wbv.getHTML().then(r => {
let tab = r.split("table")[1]
tab = `<table ${tab}table>`
tab = tab.split('tr')
let schnitt = r.split("table")[3].split("</th")[1].split('>')[2].trim()
let grades = []
let totalCredits
// console.log(tab[1])
for (let to = 1; to < tab.length-1; to++) {
// console.log(tab[to])
if (tab[to].includes('T3')) {
try {
let modul = tab[to].split('td')[1].trim().substr(15).split('<')[0]
let kurs = tab[to].split(',800,600);">')[1].split('</a>')[0]
let zahlen = tab[to].split('<td class="tbdata" style="text-align:right;">')
let credits = zahlen[2].trim().split('<')[0]
let note = zahlen[3].trim().split('<')[0]
grades.push({ modul: modul, kurs: kurs, credits: credits, note: note })
} catch(e) {
// console.log('error')
}
} else if (tab[to].includes('Friedrichshafen') && tab[to].includes('Summe')) {
totalCredits = tab[to].split('nowrap;"> ')[1].split(',0')[0]
}
}
// Output
init(true, {noten: grades, totalCredits: totalCredits, schnitt: schnitt })
})})
})
}
}
async function init(success, data, additionalData){
// TODO: Check if login works
// TODO: IF login works, then fetch the data and submit it to createWidget()
// TODO: Otherwise, show in the widget that the login did not work
// console.log(data)
if (success === true) {
const widget = await createWidget(data)
widget.setPadding(0,4,0,4)
if (config.widgetFamily === 'medium'){
await widget.presentMedium()
} else {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
}else{
const errorWidget = new ListWidget()
const headerRow = errorWidget.addStack()
headerRow.setPadding(8, 6, 6, 6)
headerRow.centerAlignContent()
const headline = headerRow.addStack()
const headlineText = headline.addText("✏️ Dualis")
headlineText.textColor = new Color('e74c3c', 1)
headlineText.font = Font.mediumSystemFont(16)
headline.addSpacer()
const errorBlock = errorWidget.addStack()
errorBlock.setPadding(4,4,4,4)
const errorText = errorBlock.addText(additionalData)
errorText.font = Font.mediumSystemFont(12)
errorText.textColor = new Color('e74c3c', 1)
await errorWidget.presentSmall()
Script.setWidget(errorWidget)
Script.complete()
}
}
// after crawl:
async function createWidget(data){
console.log(data)
const list = new ListWidget()
if (config.widgetFamily === 'small' || config.widgetFamily === undefined){
const headerRow = list.addStack()
headerRow.setPadding(8, 6, 6, 6)
headerRow.centerAlignContent()
const headline = headerRow.addStack()
const headlineText = headline.addText("✏️ Dualis")
headlineText.textColor = new Color('e74c3c', 1)
headlineText.font = Font.mediumSystemFont(16)
headline.addSpacer()
const averageBlock = headerRow.addStack()
averageBlock.layoutHorizontally()
averageBlock.setPadding(0, 8, 0, 0)
const averageText = averageBlock.addText("Ø " + data["schnitt"])
averageText.font = Font.mediumSystemFont(16)
const gradeRow = list.addStack()
gradeRow.setPadding(0, 0, 0, 0)
gradeRow.layoutHorizontally()
gradeRow.centerAlignContent()
createGradeBlock(data["noten"], 0, 3, gradeRow)
const footer = list.addStack();
footer.addSpacer();
footer.setPadding(4, 6, 8, 6)
const footerText = footer.addText(data.totalCredits + " ECTS Credits")
footerText.font = Font.mediumSystemFont(10)
}
if (config.widgetFamily === 'medium') {
const wrapperStack = list.addStack();
wrapperStack.layoutHorizontally()
wrapperStack.addSpacer();
const leftStack = wrapperStack.addStack();
leftStack.layoutVertically()
leftStack.centerAlignContent()
const headlineText = leftStack.addText("✏️ Dualis")
headlineText.textColor = new Color('e74c3c', 1)
headlineText.font = Font.mediumSystemFont(16)
leftStack.addSpacer(8)
createGradeBlock(data["noten"], 0, 3, leftStack)
leftStack.addSpacer(8)
wrapperStack.addSpacer()
const rightStack = wrapperStack.addStack()
rightStack.layoutVertically()
rightStack.centerAlignContent()
const averageBlock = rightStack.addStack();
averageBlock.addSpacer()
averageBlock.setPadding(0, 0, 0, 8)
const averageText = averageBlock.addText("Ø " + data["schnitt"])
averageText.font = Font.mediumSystemFont(16)
rightStack.addSpacer(8)
createGradeBlock(data["noten"], 4, 7, rightStack)
rightStack.addSpacer(8)
const footer = rightStack.addStack()
footer.setPadding(0, 0, 0, 8)
footer.addSpacer()
const footerText = footer.addText(data.totalCredits + " ECTS Credits")
footerText.font = Font.mediumSystemFont(10)
wrapperStack.addSpacer()
}
return list
}
// Future Options: Get different sets of courses, etc.
function createGradeBlock(dataSet, minRange, maxRange, gradeRow){
const gradeBlock = gradeRow.addStack();
gradeBlock.layoutVertically()
gradeBlock.centerAlignContent()
for (let i = minRange; i < maxRange + 1; i++){
let note;
if (dataSet[i].note === 'b'){
note = "✓"
}else{
note = dataSet[i].note
}
gradeBlock.addText(convertLongNameToShortName(dataSet[i].kurs) + ": " + note).font = Font.mediumSystemFont(14)
}
gradeBlock.backgroundColor = new Color('cccccc', 0.1)
gradeBlock.cornerRadius = 8
gradeBlock.setPadding(8,8,8,8)
return gradeBlock;
}
function convertLongNameToShortName(name) {
let shortName
switch (true){
case name.contains("Mathematik II"):
shortName = "Mathe 2"
break;
case name.contains("Mathematik I"):
shortName = "Mathe 1"
break;
case name.contains("Technische Informatik II"):
shortName = "TI 2"
break;
case name.contains("Technische Informatik I"):
shortName = "TI 1"
break;
case name.contains("Theoretische Informatik III"):
shortName = "TheoInf 3"
break;
case name.contains("Theoretische Informatik II"):
shortName = "TheoInf 2"
break;
case name.contains("Theoretische Informatik I"):
shortName = "TheoInf 1"
break;
case name.contains("Schlüsselqualifikationen"):
shortName = "Schlüsselquali."
break;
case name.contains("Praxisprojekt III"):
shortName = "T1000"
break;
case name.contains("Praxixprojekt II"):
shortName = "T2000"
break;
case name.contains("Praxisprojekt I"):
shortName = "T1000"
break;
default:
shortName = name;
}
return shortName;
}
String.prototype.contains = function(substr) {
return this.indexOf(substr) > -1;
}
if (config.runsInWidget){
if (runLoadSite){
await loadSite()
}
}else{
//redirect to Dualis?
Safari.open("https://github.com/corusm/dualis-widget")
Script.complete()
}