forked from AENeuro/HKU-Moodle-Helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback.js
84 lines (79 loc) · 2.93 KB
/
feedback.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
globalThis.addFeedbackBox = function() {
function showTextArea() {
document.getElementById("helperFeedbackForm").classList.add("helper-shown")
document.getElementById("helperFeedbackButton").insertAdjacentHTML("beforebegin", `
<div>
<p id="helperFeedbackButton2" style="color: #AAAAAA;">Check the
<a href="https://github.com/AENeuro/HKU-Moodle-Helper" target="_blank">
<span style="color: #AAAAAA;"><u>FAQ</u></span>
</a>
or submit an issue or PR on
<a href="https://github.com/AENeuro/HKU-Moodle-Helper" target="_blank">
<span style="color: #AAAAAA;"><u>Github</u></span>
</a>
</p>
</div>
`)
document.getElementById("helperFeedbackButton").remove()
}
async function sendFeedback() {
if (!document.getElementById("helperFeedbackInput").value) {
return 0
}
document.getElementById("helperFeedbackSend").disabled = true
const content = document.getElementById("helperFeedbackInput").value || "";
const data = {
source: "HKU Moodle Helper",
time: new Date().toISOString(),
content: content
};
try{
await request({
url: " https://feedback.richku.com/feedback.php",
method: "POST",
body: data,
})
} catch(e) {
alert("Network error")
}
document.getElementById("helperFeedbackForm").classList.remove("helper-shown")
document.getElementById("helperFeedbackForm").insertAdjacentHTML("beforebegin", `
<p style="color: #AAAAAA">Thank you for your feedback!</p>
`)
document.getElementById("helperFeedbackButton2").remove()
}
// initialization
var version = chrome.runtime.getManifest().version
document.getElementsByClassName("course-of-sem-wrapper")[0].insertAdjacentHTML("beforeend",`
<div class="helper-feedback">
<p>Powered by HKU Moodle Helper ver. ${version}</p>
<p id="helperFeedbackButton">Feedback</p>
<div id="helperFeedbackForm" class="helper-hidden">
<input id="helperFeedbackInput" type="text" placeholder="Email [Optional] + issue"/><br/>
<button id="helperFeedbackSend">Send</button>
</div>
</div>
`)
document.getElementById("helperFeedbackButton").addEventListener("click", showTextArea)
document.getElementById("helperFeedbackSend").addEventListener("click", sendFeedback)
}
const request = obj => {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open(obj.method || "GET", obj.url);
if (obj.headers) {
Object.keys(obj.headers).forEach(key => {
xhr.setRequestHeader(key, obj.headers[key]);
});
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
} else {
reject(xhr.statusText);
}
};
xhr.onerror = () => reject(xhr.statusText);
xhr.send(JSON.stringify(obj.body));
});
};