-
Notifications
You must be signed in to change notification settings - Fork 0
/
aula-contact-qr.user.js
147 lines (124 loc) · 4.5 KB
/
aula-contact-qr.user.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
// ==UserScript==
// @name Aula Contact QR
// @namespace https://github.com/hcsaustrup/tampermonkey
// @version 0.4
// @description Add missing features to the Aula system
// @author You
// @match https://www.aula.dk/portal/*
// @grant GM_addStyle
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js
// ==/UserScript==
//*** @require https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js
(function () {
'use strict';
var o = {}
o.id = 0;
o.encodeVcard = function (d) {
var v = "BEGIN:VCARD\n" +
"VERSION:4.0\n";
if (d.name) {
var nParts = d.name.split(" ", 2);
v += "N:" + nParts[1] + ";" + nParts[0] + ";;;\n";
v += "FN:" + d.name + "\n";
}
// v += "ORG:Foo Org\n";
v += "TITLE:" + d.title + "\n";
if (d.mobilePhone) {
v += "TEL;TYPE=cell;VALUE=uri:tel:" + d.mobilePhone + "\n";
}
if (d.phone) {
v += "TEL;TYPE=home,voice;VALUE=uri:tel:" + d.phone + "\n";
}
if (d.workPhone) {
v += "TEL;TYPE=work,voice;VALUE=uri:tel:" + d.workPhone + "\n";
}
if (d.address) {
v += "ADR;TYPE=HOME;LABEL=\"\":;;" + d.address + ";" + d.city + ";;" + d.zipcode + ";\n";
}
if (d.email) {
v += "EMAIL:" + d.email + "\n";
}
v += "END:VCARD";
return v;
};
o.qr = function (jInfo, jQR) {
var d = {};
jInfo.children(".mb-1").each(function (i, element) {
d.role = element.innerText.trim();
});
jInfo.find(".contact-text span").each(function (i, element) {
if (i == 0) {
d.name = element.innerText.replace(/\(.*\)/, "").trim();
}
if (i == 1) {
var m = element.innerText.match(/^(.*?), (\d{4}) (\w+.*)$/);
if (m.length == 4) {
d.address = m[1].trim();
d.zipcode = m[2].trim();
d.city = m[3].trim();
}
}
});
jInfo.find(".contact-item").each(function (i, element) {
var spans = $(element).children("span");
if (spans.length != 2) return;
var key = spans[0].innerText.toLowerCase().replace(/\W/g, ' ').trim().replace(' ', '-');
var value = spans[1].innerText.trim().toLowerCase();
if (key == "hjemme-tlf") {
d.phone = value;
} else if (key == "mobil-tlf") {
d.mobilePhone = value;
} else if (key == "arbejds-tlf") {
d.workPhone = value;
} else if (key == "email") {
d.email = value;
} else {
console.log("Aula Tweaks: Unknown field: " + key);
}
});
var jName = jInfo.closest(".contact").find(".contact-name");
if (jName.length == 1) {
var childNames = jName[0].innerText.replace(/\(.*\)/, '').trim().split(" ");
if (childNames.length < 3) {
d.childShortName = childNames[0];
} else {
childNames.pop();
d.childShortName = childNames.join(" ");
}
d.title = d.childShortName + (d.childShortName.match(/s$/) ? "es" : "s") + " " + d.role.toLowerCase();
}
console.log("Data:", d);
if (d.mobilePhone) {
var vcard = o.encodeVcard(d);
console.log("VCARD:", vcard);
jQR.qrcode({ "text": vcard, width: 600, height: 600 });
}
jQR.show();
};
o.timeout = function () {
$("div.relation").not(".qred").each(function (i, element) {
console.log(element);
var jElement = $(element);
jElement.addClass('qred');
var jQR = $("<div>")
jQR.addClass("qr").hide();
var jButton = $("<button>");
jButton.text("QR");
jButton.on('click', function () {
o.qr(jElement, jQR);
jButton.hide();
});
jQR.on('click', function () {
jQR.hide();
jButton.show();
});
jElement.prepend(jQR).prepend(jButton);
});
o.queueTimeout();
};
o.queueTimeout = function () {
setTimeout(function () { o.timeout() }, 2000);
};
o.queueTimeout();
})();