Skip to content

Commit 317ae51

Browse files
committed
add calendar id and color
1 parent 8d2a35f commit 317ae51

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

example.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
VITE_NOT_CHROME_CLIENT_ID=
1+
VITE_NOT_CHROME_CLIENT_ID=
2+
VITE_CALENDAR_NAME='CCU Homework Calendar'
3+
VITE_CALENDAR_DEFAULT_COLOR='6'

src/js/calendar.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export async function getOrCreateCalendar(token, name = import.meta.env.VITE_CALENDAR_NAME) {
2+
// 1. 先列出所有日曆
3+
const listRes = await fetch("https://www.googleapis.com/calendar/v3/users/me/calendarList", {
4+
headers: {
5+
Authorization: `Bearer ${token}`,
6+
},
7+
});
8+
9+
const listData = await listRes.json();
10+
11+
if (!listRes.ok) {
12+
throw new Error(`List calendars failed: ${listRes.status} ${listData.error?.message}`);
13+
}
14+
15+
// 2. 查找是否已有相同名稱的日曆
16+
const existing = listData.items.find((cal) => cal.summary === name);
17+
if (existing) {
18+
return existing.id;
19+
}
20+
21+
// 3. 若無則創建新日曆
22+
const createRes = await fetch("https://www.googleapis.com/calendar/v3/calendars", {
23+
method: "POST",
24+
headers: {
25+
Authorization: `Bearer ${token}`,
26+
"Content-Type": "application/json",
27+
},
28+
body: JSON.stringify({
29+
summary: name,
30+
description: "由 CCU Class 自動建立的作業日曆",
31+
timeZone: "Asia/Taipei",
32+
}),
33+
});
34+
35+
const createData = await createRes.json();
36+
37+
if (!createRes.ok) {
38+
throw new Error(`Create calendar failed: ${createRes.status} ${createData.error?.message}`);
39+
}
40+
41+
return createData.id;
42+
}

src/js/popup.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ async function launchWebAuthFlowForGoogle() {
145145
return new Promise((resolve, reject) => {
146146
// Except Chrome Client ID
147147
const clientId = import.meta.env.VITE_NOT_CHROME_CLIENT_ID;
148-
const scope = "https://www.googleapis.com/auth/calendar.events";
148+
const scopes = [
149+
"https://www.googleapis.com/auth/calendar.events",
150+
"https://www.googleapis.com/auth/calendar",
151+
];
149152

150153
const redirectUri = `https://${chrome.runtime.id}.chromiumapp.org/`;
151154

@@ -154,7 +157,7 @@ async function launchWebAuthFlowForGoogle() {
154157
`?response_type=token` +
155158
`&client_id=${encodeURIComponent(clientId)}` +
156159
`&redirect_uri=${encodeURIComponent(redirectUri)}` +
157-
`&scope=${encodeURIComponent(scope)}` +
160+
`&scope=${encodeURIComponent(scopes.join(" "))}` +
158161
`&prompt=consent`;
159162

160163
chrome.identity.launchWebAuthFlow(

src/js/utils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getOrCreateCalendar } from "./calendar.js";
2+
13
export function getCurrentYearMonth() {
24
const today = new Date();
35
return {
@@ -78,16 +80,18 @@ function normalizeBatchText(raw) {
7880
return raw.replace(/\r?\n/g, "\r\n");
7981
}
8082

81-
function generateBatchRequestBody(events, boundary = "batch_boundary") {
83+
function generateBatchRequestBody(events, boundary = "batch_boundary", calendarId) {
8284
const CRLF = "\r\n";
8385
let body = "";
86+
console.log(calendarId);
8487

8588
events.forEach((event, i) => {
8689
body += `--${boundary}${CRLF}`;
8790
body += `Content-Type: application/http${CRLF}`;
8891
body += `Content-ID: <item-${i + 1}>${CRLF}${CRLF}`;
8992

90-
body += `POST /calendar/v3/calendars/primary/events HTTP/1.1${CRLF}`;
93+
body += `POST /calendar/v3/calendars/${calendarId}/events HTTP/1.1${CRLF}`;
94+
body += `Host: www.googleapis.com${CRLF}`;
9195
body += `Content-Type: application/json${CRLF}${CRLF}`;
9296

9397
const eventPayload = {
@@ -104,6 +108,7 @@ function generateBatchRequestBody(events, boundary = "batch_boundary") {
104108
};
105109

106110
body += JSON.stringify(eventPayload) + CRLF + CRLF;
111+
console.log(body);
107112
});
108113

109114
body += `--${boundary}--${CRLF}`;
@@ -148,7 +153,8 @@ function parseGoogleBatchResponse(responseText) {
148153
// Google Calendar API batch add Event
149154
export async function addBatchEventsToCalendar(token, events) {
150155
const boundary = "batch_boundary_" + Date.now();
151-
const body = generateBatchRequestBody(events, boundary);
156+
const calendarId = await getOrCreateCalendar(token);
157+
const body = generateBatchRequestBody(events, boundary, calendarId);
152158

153159
const res = await fetch("https://www.googleapis.com/batch/calendar/v3", {
154160
method: "POST",

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"oauth2": {
2828
"client_id": "529259850785-eg40qfiu14k4806pocqtgoqprs4v5njn.apps.googleusercontent.com",
2929
"scopes": [
30-
"https://www.googleapis.com/auth/calendar.events"
30+
"https://www.googleapis.com/auth/calendar.events",
31+
"https://www.googleapis.com/auth/calendar"
3132
]
3233
}
3334
}

0 commit comments

Comments
 (0)