-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
103 lines (86 loc) · 2.84 KB
/
Copy pathcontent.js
File metadata and controls
103 lines (86 loc) · 2.84 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
// === content.js ===
// BEEF+ の「課題・テスト一覧」ページから課題情報を抽出して保存
class TaskPageParser {
constructor(doc, loc) {
this.doc = doc;
this.loc = loc;
}
parse() {
const tasks = [];
const noPendingEl = this.doc.querySelector(".contents-detail .contents-list .no-data");
const noPendingDetected = Boolean(
noPendingEl && noPendingEl.textContent.includes("未提出の課題・テスト一覧はありません")
);
const rows = this.doc.querySelectorAll(".result_list_line.sortTaskBlock");
rows.forEach((row) => {
const courseEl = row.querySelector(".tasklist-course");
const contentEl = row.querySelector(".tasklist-contents a");
const titleEl = row.querySelector(".tasklist-title a");
const deadlineEl = row.querySelector(".tasklist-deadline .deadline");
if (!courseEl || !contentEl || !titleEl || !deadlineEl) return;
const course = courseEl.textContent.trim();
const contentType = contentEl.textContent.trim();
const title = titleEl.textContent.trim();
const url = new URL(titleEl.getAttribute("href"), this.loc.origin).href;
const dueText = deadlineEl.textContent.trim();
const due = new Date(dueText.replace(/-/g, "/"));
if (isNaN(due)) return;
tasks.push({
course,
contentType,
title,
url,
due: due.toISOString(),
});
});
return { tasks, noPendingDetected };
}
}
class TaskStorageUpdater {
constructor(storage) {
this.storage = storage;
}
update(newTasks) {
this.storage.get("tasks", (data) => {
const oldTasks = data.tasks || [];
const newUrls = newTasks.map((t) => t.url);
const kept = oldTasks.filter((t) => newUrls.includes(t.url));
const added = newTasks.filter((t) => !oldTasks.some((o) => o.url === t.url));
const merged = [...kept, ...added];
this.storage.set({
tasks: merged,
noPending: false,
noPendingMessage: "",
lastUpdated: new Date().toISOString(),
});
});
}
}
class TaskPageController {
constructor(parser, updater, storage) {
this.parser = parser;
this.updater = updater;
this.storage = storage;
}
run() {
const result = this.parser.parse();
if (result.noPendingDetected) {
this.storage.set({
tasks: [],
noPending: true,
noPendingMessage: "✅ 未提出の課題・テストはありません",
lastUpdated: new Date().toISOString(),
});
return;
}
if (result.tasks.length > 0) {
this.updater.update(result.tasks);
}
}
}
(() => {
const parser = new TaskPageParser(document, location);
const updater = new TaskStorageUpdater(chrome.storage.local);
const controller = new TaskPageController(parser, updater, chrome.storage.local);
controller.run();
})();