-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_course_sections.js
More file actions
139 lines (123 loc) · 5.9 KB
/
group_course_sections.js
File metadata and controls
139 lines (123 loc) · 5.9 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
(() => {
const mainContainer = document.querySelector(".v-main__wrap .container");
if (!mainContainer) return;
function extractAllSubjects() {
return Array.from(mainContainer.querySelectorAll("table tbody tr"))
.map((tr) => {
const tds = tr.querySelectorAll("td");
if (tds.length < 9) return null;
return {
code: tds[0]?.innerText.trim(),
name: tds[1]?.innerText.trim(),
credit: tds[2]?.innerText.trim(),
section: tds[3]?.innerText.trim().split("\n")[0].trim(),
type: tds[3]?.innerText.trim().split("\n")[1].trim(),
time: tds[4]?.innerText.trim(),
room: tds[5]?.innerText.trim(),
building: tds[6]?.innerText.trim(),
teacher: tds[7]?.innerText.trim().split("\n").map(t => t.trim()),
exam: {
midterm: {
raw: tds[8]?.innerText.trim().split("\nFinal")[0].trim(),
date: tds[8]?.innerText.trim().split("\nFinal")[0].split("\n")[1]?.trim(),
time: tds[8]?.innerText.trim().split("\nFinal")[0].split("\n")[2]?.trim(),
},
final: {
raw: "Final" + tds[8]?.innerText.trim().split("\nFinal")[1].trim(),
date: tds[8]?.innerText.trim().split("\nFinal")[1].split("\n")[1]?.trim(),
time: tds[8]?.innerText.trim().split("\nFinal")[1].split("\n")[2]?.trim(),
},
},
conditionButton: tds[9]?.querySelector("button"),
note: tds[10]?.innerText.trim(),
};
})
.filter((data) => data?.code);
}
function groupSubjects(rows) {
const map = rows.reduce((acc, r) => {
if (!acc[r.code]) {
acc[r.code] = { name: r.name, credit: r.credit, sections: [] };
}
const key = `${r.section}-${r.time}-${r.room}`;
if (!acc[r.code].sections.some((s) => s.key === key)) {
acc[r.code].sections.push({ ...r, key });
}
return acc;
}, {});
Object.values(map).forEach((subj) => {
subj.sections.sort((a, b) => {
const numA = parseInt(a.section.match(/\d+/)?.[0] || 0);
const numB = parseInt(b.section.match(/\d+/)?.[0] || 0);
return numA - numB;
});
});
return map;
}
function renderList(grouped) {
mainContainer.innerHTML = "";
const container = mainContainer.appendChild(document.createElement("div"));
container.style.padding = "20px 30px";
const title = container.appendChild(document.createElement("h2"));
title.textContent = "รายวิชาทั้งหมด";
title.style.cssText = "color:#e65100; margin-bottom:25px; font-weight:600;";
Object.entries(grouped).forEach(([code, subject]) => {
const card = container.appendChild(document.createElement("div"));
card.style.cssText =
"background:white; border-radius:10px; box-shadow:0 1px 4px rgba(0,0,0,0.1); margin-bottom:20px; padding:20px;";
card.innerHTML = `
<div style="font-size:17px; font-weight:600; color:#ef6c00;">
${code} - ${subject.name}
</div>
<div style="color:#555; font-size:13px; margin-top:2px;">หน่วยกิต ${subject.credit}</div>
`;
const sectionList = card.appendChild(document.createElement("div"));
sectionList.style.marginTop = "12px";
subject.sections.forEach((sec) => {
const wrapper = sectionList.appendChild(document.createElement("div"));
wrapper.style.margin = "8px 0";
const headerRow = wrapper.appendChild(document.createElement("div"));
headerRow.style.cssText =
"background:#fff8f3; border-left:4px solid #ef6c00; border-radius:6px; padding:10px 12px; font-size:14px; cursor:pointer; display:flex; justify-content:space-between; align-items:center;";
headerRow.innerHTML = `
<div><b>Section ${sec.section}</b> | ${sec.time} | ${sec.room} (${sec.building ? sec.building : "ไม่ระบุ"})</div>
<span style="color:#ef6c00; font-weight:bold;">+</span>
`;
const detail = wrapper.appendChild(document.createElement("div"));
detail.style.cssText =
"display:none; margin-left:10px; margin-top:8px; padding:8px 10px; background:#fffaf7; border-left:2px dashed #ef6c00; border-radius:4px; font-size:13.5px; color:#444; line-height:1.7;";
detail.appendChild(
document.createElement("div")
).innerHTML = `<b>${sec.teacher.join(", ")}</b>`;
detail.appendChild(
document.createElement("div")
).innerHTML = `<b>สอบกลางภาค</b> <span style="color:#555;">${sec.exam.midterm.date} ${sec.exam.midterm.time ? sec.exam.midterm.time : ""}</span>`;
detail.appendChild(
document.createElement("div")
).innerHTML = `<b>สอบปลายภาค</b> <span style="color:#555;">${sec.exam.final.date} ${sec.exam.final.time ? sec.exam.final.time : ""}</span>`;
if (sec.conditionButton) {
const line = detail.appendChild(document.createElement("div"));
line.style.cssText =
"display: flex; align-items: center; margin-top: 4px;";
const label = line.appendChild(document.createElement("b"));
label.textContent = "เงื่อนไข:";
label.style.marginRight = "5px";
line.appendChild(sec.conditionButton);
}
if (sec.note) {
const noteLine = detail.appendChild(document.createElement("div"));
noteLine.style.marginTop = "4px";
noteLine.innerHTML = `<b>หมายเหตุ:</b> ${sec.note}`;
}
headerRow.addEventListener("click", () => {
const isHidden = detail.style.display === "none";
detail.style.display = isHidden ? "block" : "none";
headerRow.querySelector("span").textContent = isHidden ? "-" : "+";
});
});
});
}
const data = extractAllSubjects();
const grouped = groupSubjects(data);
renderList(grouped);
})();