-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.ts
98 lines (74 loc) · 2.64 KB
/
content.ts
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
import "./style.scss"
import dayjs from "dayjs"
import relativeTime from "dayjs/plugin/relativeTime"
import timezone from "dayjs/plugin/timezone"
import utc from "dayjs/plugin/utc"
import "dayjs/locale/ja"
dayjs.extend(relativeTime)
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.tz.guess()
const STATUS_TEXTS = {
notSubmitted: "未提出",
closed: "受付終了",
}
const COLOR_THRESHOLDS = [
{ days: 1, className: "one-day-left" },
{ days: 3, className: "three-days-left" },
{ days: 7, className: "seven-days-left" },
]
const COLUMN_TITLES = {
TYPE: "タイプ",
TITLE: "タイトル",
STATUS: "状態",
COURSE: "コース",
STARTS_AT: "受付開始日時",
ENDS_AT: "受付終了日時",
};
let column_indexes = {
TYPE: undefined,
TITLE: undefined,
STATUS: undefined,
COURSE: undefined,
STARTS_AT: undefined,
ENDS_AT: undefined,
RANGE: undefined,
}
const classNameFromDiffDays = (diffDays: number): string | undefined =>
COLOR_THRESHOLDS.find(({ days }) => diffDays <= days)?.className
const colorizeManaba = () => {
const titleRow = document.querySelector("table.stdlist > tbody > tr.title");
for (const [idx, element] of Array.from(titleRow.children).entries()) {
element.removeAttribute("width");
const matchedKey = Object.keys(COLUMN_TITLES).find(key => COLUMN_TITLES[key] === element.textContent);
if (matchedKey) {
column_indexes[matchedKey] = idx;
}
}
if (!column_indexes.ENDS_AT) {
return
}
const daysLeftColumnTitle = document.createElement("th")
daysLeftColumnTitle.textContent = "残り日数"
titleRow.append(daysLeftColumnTitle)
const rows = Array.from(document.querySelectorAll("table.stdlist > tbody > tr:is(.row0, .row1)"));
for (const [idx, row] of rows.entries()) {
const columns = Array.from(row.children)
// 状態カラムが存在するページでは状態に応じて色をつける処理をスキップ
if (column_indexes.STATUS) {
const status = Array.from(columns[column_indexes.STATUS].childNodes).map((child) => child.textContent.trim()).filter(child => child != "")
if (!status.includes(STATUS_TEXTS.notSubmitted) || status.includes(STATUS_TEXTS.closed)) {
continue
}
}
const deadlineText = columns[column_indexes.ENDS_AT].textContent
const deadline = dayjs(deadlineText, "YYYY-MM-DD HH:mm")
const now = dayjs()
const diffDays = deadline.diff(now, "days", true)
row.classList.add(classNameFromDiffDays(diffDays))
const daysLeftColumn = document.createElement("td")
daysLeftColumn.textContent = `約${deadline.locale("ja").fromNow(true)}`
row.append(daysLeftColumn)
}
}
colorizeManaba()