|
1 | | -function downloadICS(events) { |
2 | | - let icsContent = `BEGIN:VCALENDAR |
3 | | -VERSION:2.0 |
4 | | -CALSCALE:GREGORIAN |
5 | | -PRODID:-//Moodle to Calendar//EN |
6 | | -`; |
| 1 | +import { getCurrentYearMonth, downloadICS } from "./utils.js"; |
7 | 2 |
|
8 | | - for (const event of events) { |
9 | | - const { title, description, startDate, endDate } = event; |
10 | | - const format = (d) => d.toISOString().replace(/[-:]/g, "").split(".")[0] + "Z"; |
| 3 | +function initializeInputs() { |
| 4 | + const { year, month } = getCurrentYearMonth(); |
| 5 | + document.getElementById("yearInput").value = year; |
| 6 | + document.getElementById("monthInput").value = month; |
| 7 | +} |
11 | 8 |
|
12 | | - icsContent += `BEGIN:VEVENT |
13 | | -SUMMARY:${title} |
14 | | -DESCRIPTION:${description || ""} |
15 | | -DTSTART:${format(startDate)} |
16 | | -DTEND:${format(endDate)} |
17 | | -END:VEVENT |
18 | | -`; |
19 | | - } |
| 9 | +async function fetchCalendarData(sesskey) { |
| 10 | + const year = parseInt(document.getElementById("yearInput").value); |
| 11 | + const month = parseInt(document.getElementById("monthInput").value); |
| 12 | + // day is dont care |
| 13 | + const day = 1; |
20 | 14 |
|
21 | | - icsContent += "END:VCALENDAR"; |
| 15 | + const response = await fetch( |
| 16 | + `https://ecourse2.ccu.edu.tw/lib/ajax/service.php?sesskey=${sesskey}&info=core_calendar_get_calendar_monthly_view`, |
| 17 | + { |
| 18 | + method: "POST", |
| 19 | + headers: { "Content-Type": "application/json" }, |
| 20 | + body: JSON.stringify([ |
| 21 | + { |
| 22 | + index: 0, |
| 23 | + methodname: "core_calendar_get_calendar_monthly_view", |
| 24 | + args: { year, month, day }, |
| 25 | + }, |
| 26 | + ]), |
| 27 | + credentials: "include", |
| 28 | + } |
| 29 | + ); |
22 | 30 |
|
23 | | - const blob = new Blob([icsContent], { type: "text/calendar" }); |
24 | | - const url = URL.createObjectURL(blob); |
25 | | - const a = document.createElement("a"); |
26 | | - a.href = url; |
27 | | - a.download = "moodle-homework.ics"; |
28 | | - a.click(); |
29 | | - URL.revokeObjectURL(url); |
| 31 | + const raw_data = await response.json(); |
| 32 | + console.log(raw_data); |
| 33 | + return raw_data[0]["data"]["weeks"] |
| 34 | + .flatMap((week) => week.days) |
| 35 | + .flatMap((day) => day.events) |
| 36 | + .map((event) => ({ |
| 37 | + description: event.course.fullname, |
| 38 | + title: event.activityname, |
| 39 | + startDate: new Date(event.timestart * 1000 - 3600 * 1000), |
| 40 | + endDate: new Date(event.timestart * 1000), |
| 41 | + })); |
30 | 42 | } |
31 | 43 |
|
32 | | -document.getElementById("exportBtn").addEventListener("click", async () => { |
33 | | - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { |
34 | | - chrome.tabs.sendMessage(tabs[0].id, { action: "get_sesskey" }, async (res) => { |
35 | | - const sesskey = res?.sesskey; |
36 | | - if (!sesskey) return console.log("no touch sesskey"); |
37 | | - |
38 | | - const today = new Date(); |
39 | | - const year = today.getFullYear(); |
40 | | - const month = today.getMonth() + 1; |
41 | | - const day = today.getDate(); |
42 | | - |
43 | | - const response = await fetch( |
44 | | - `https://ecourse2.ccu.edu.tw/lib/ajax/service.php?sesskey=${sesskey}&info=core_calendar_get_calendar_monthly_view`, |
45 | | - { |
46 | | - method: "POST", |
47 | | - headers: { "Content-Type": "application/json" }, |
48 | | - body: JSON.stringify([ |
49 | | - { |
50 | | - index: 0, |
51 | | - methodname: "core_calendar_get_calendar_monthly_view", |
52 | | - args: { year, month, day }, |
53 | | - }, |
54 | | - ]), |
55 | | - credentials: "include", |
| 44 | +function setupEventListeners() { |
| 45 | + document.getElementById("exportBtn").addEventListener("click", async () => { |
| 46 | + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { |
| 47 | + chrome.tabs.sendMessage(tabs[0].id, { action: "get_sesskey" }, async (res) => { |
| 48 | + const sesskey = res?.sesskey; |
| 49 | + if (!sesskey) { |
| 50 | + console.log("no touch sesskey"); |
| 51 | + document.getElementById("result").textContent = "無法獲取 sesskey,請先登入 Moodle"; |
| 52 | + return; |
56 | 53 | } |
57 | | - ); |
58 | 54 |
|
59 | | - const raw_data = await response.json(); |
60 | | - console.log(raw_data); |
61 | | - const data = raw_data[0]["data"]["weeks"] |
62 | | - .flatMap((week) => week.days) |
63 | | - .flatMap((day) => day.events) |
64 | | - .map((event) => ({ |
65 | | - description: event.course.fullname, |
66 | | - title: event.activityname, |
67 | | - startDate: new Date(event.timestart * 1000 - 3600 * 1000), |
68 | | - endDate: new Date(event.timestart * 1000), |
69 | | - })); |
70 | | - |
71 | | - downloadICS(data); |
| 55 | + try { |
| 56 | + const events = await fetchCalendarData(sesskey); |
| 57 | + downloadICS(events); |
| 58 | + document.getElementById("result").textContent = `成功匯出 ${events.length} 個行事曆事件`; |
| 59 | + } catch (error) { |
| 60 | + console.error(error); |
| 61 | + document.getElementById("result").textContent = "發生錯誤,請稍後再試"; |
| 62 | + } |
| 63 | + }); |
72 | 64 | }); |
73 | 65 | }); |
| 66 | +} |
| 67 | + |
| 68 | +// Initialize when DOM is fully loaded |
| 69 | +document.addEventListener("DOMContentLoaded", () => { |
| 70 | + initializeInputs(); |
| 71 | + setupEventListeners(); |
74 | 72 | }); |
0 commit comments