Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (126 loc) · 4.44 KB
/
Copy pathscript.js
File metadata and controls
150 lines (126 loc) · 4.44 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
const DEFAULT_RATING = "5";
const DEFAULT_FEEDBACK = "N/A";
const form = document.getElementById("ratingForm");
const feedbackInput = document.getElementById("feedback");
const fillCurrentBtn = document.getElementById("fillCurrentBtn");
const submitAllBtn = document.getElementById("submitAllBtn");
const statusText = document.getElementById("status");
const pageStateText = document.getElementById("pageState");
function getStoredSettings() {
return new Promise((resolve) => {
chrome.storage.local.get(["Rating", "Feedback"], function (result) {
resolve({
rating: result.Rating || DEFAULT_RATING,
feedback: result.Feedback || DEFAULT_FEEDBACK
});
});
});
}
function saveSettings(settings) {
return new Promise((resolve) => {
chrome.storage.local.set({
Rating: settings.rating,
Feedback: settings.feedback
}, resolve);
});
}
function sendCommand(command, settings) {
return new Promise((resolve) => {
chrome.runtime.sendMessage({
type: "IUT_EVALUATION_COMMAND",
command,
settings
}, function (response) {
if (chrome.runtime.lastError) {
resolve({ ok: false, error: chrome.runtime.lastError.message });
return;
}
resolve(response || { ok: false, error: "No response received." });
});
});
}
function getSelectedSettings() {
const checkedRating = document.querySelector('input[name="rating"]:checked');
return {
rating: checkedRating ? checkedRating.value : DEFAULT_RATING,
feedback: feedbackInput.value.trim() || DEFAULT_FEEDBACK
};
}
function setStatus(message, type = "info") {
statusText.textContent = message;
statusText.dataset.type = type;
}
function setBusy(isBusy) {
fillCurrentBtn.disabled = isBusy || fillCurrentBtn.dataset.available !== "true";
submitAllBtn.disabled = isBusy || submitAllBtn.dataset.available !== "true";
form.setAttribute("aria-busy", String(isBusy));
}
function setAvailability(state) {
const canFill = state && state.page === "course";
const canSubmitAll = state && state.page === "list" && state.courseCount > 0;
fillCurrentBtn.dataset.available = String(canFill);
submitAllBtn.dataset.available = String(canSubmitAll);
fillCurrentBtn.disabled = !canFill;
submitAllBtn.disabled = !canSubmitAll;
if (!state || state.page === "unsupported") {
pageStateText.textContent = "Open an SIS evaluation page.";
return;
}
if (state.page === "course") {
pageStateText.textContent = "Ready to submit this course.";
return;
}
pageStateText.textContent = state.courseCount === 1
? "1 course link found."
: `${state.courseCount} course links found.`;
}
async function initialize() {
const settings = await getStoredSettings();
const ratingInput = document.querySelector(`input[name="rating"][value="${settings.rating}"]`);
(ratingInput || document.querySelector(`input[name="rating"][value="${DEFAULT_RATING}"]`)).checked = true;
feedbackInput.value = settings.feedback;
const pageState = await sendCommand("getPageState");
if (pageState.ok) {
setAvailability(pageState);
if (pageState.page === "unsupported") {
setStatus("Open the SIS evaluation list or a course evaluation page.", "error");
} else {
setStatus("Ready.");
}
} else {
setAvailability({ page: "unsupported" });
setStatus(pageState.error, "error");
}
}
form.addEventListener("submit", async function (event) {
event.preventDefault();
const settings = getSelectedSettings();
await saveSettings(settings);
setBusy(true);
setStatus("Filling and submitting current course...");
const response = await sendCommand("submitCurrent", settings);
setBusy(false);
if (!response.ok) {
setStatus(response.error || "Could not submit the current course.", "error");
return;
}
setStatus(
`Submitted current course. Returning to evaluation list...`,
"success"
);
});
submitAllBtn.addEventListener("click", async function () {
const settings = getSelectedSettings();
await saveSettings(settings);
setBusy(true);
setStatus("Submitting listed courses...");
const response = await sendCommand("submitAll", settings);
setBusy(false);
if (!response.ok) {
setStatus(response.error || "Could not submit listed courses.", "error");
return;
}
const failedText = response.failedCount > 0 ? ` ${response.failedCount} failed.` : "";
setStatus(`Submitted ${response.successCount}/${response.totalCount} courses.${failedText}`, "success");
});
initialize();