Skip to content

Commit 8cb2d11

Browse files
committed
format
1 parent c4d9dd1 commit 8cb2d11

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

src/js/popup.js

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,51 +109,53 @@ async function launchWebAuthFlowForGoogle() {
109109
// Except Chrome Client ID
110110
const clientId = import.meta.env.VITE_NOT_CHROME_CLIENT_ID;
111111
const scope = "https://www.googleapis.com/auth/calendar.events";
112-
112+
113113
const redirectUri = `https://${chrome.runtime.id}.chromiumapp.org/`;
114114

115-
const authUrl =
115+
const authUrl =
116116
"https://accounts.google.com/o/oauth2/v2/auth" +
117117
`?response_type=token` +
118118
`&client_id=${encodeURIComponent(clientId)}` +
119119
`&redirect_uri=${encodeURIComponent(redirectUri)}` +
120120
`&scope=${encodeURIComponent(scope)}` +
121121
`&prompt=consent`;
122122

123-
chrome.identity.launchWebAuthFlow({
124-
url: authUrl,
125-
interactive: true
126-
}, (responseUrl) => {
127-
if (chrome.runtime.lastError) {
128-
console.error("launchWebAuthFlow error:", chrome.runtime.lastError);
129-
return reject(chrome.runtime.lastError);
130-
}
131-
if (!responseUrl) {
132-
return reject("Unable to obtain authorization result");
133-
}
123+
chrome.identity.launchWebAuthFlow(
124+
{
125+
url: authUrl,
126+
interactive: true,
127+
},
128+
(responseUrl) => {
129+
if (chrome.runtime.lastError) {
130+
console.error("launchWebAuthFlow error:", chrome.runtime.lastError);
131+
return reject(chrome.runtime.lastError);
132+
}
133+
if (!responseUrl) {
134+
return reject("Unable to obtain authorization result");
135+
}
134136

135-
const urlFragment = responseUrl.split("#")[1];
136-
if (!urlFragment) {
137-
return reject("Unable to retrieve token from callback URL");
138-
}
139-
const params = new URLSearchParams(urlFragment);
140-
const token = params.get("access_token");
141-
if (!token) {
142-
return reject("Postback URL has no access_token");
143-
}
137+
const urlFragment = responseUrl.split("#")[1];
138+
if (!urlFragment) {
139+
return reject("Unable to retrieve token from callback URL");
140+
}
141+
const params = new URLSearchParams(urlFragment);
142+
const token = params.get("access_token");
143+
if (!token) {
144+
return reject("Postback URL has no access_token");
145+
}
144146

145-
resolve(token);
146-
});
147+
resolve(token);
148+
}
149+
);
147150
});
148151
}
149152

150153
// get token
151154
function getGoogleAuthToken() {
152-
return new Promise(async (resolve, reject) => {
153-
155+
return new Promise((resolve, reject) => {
154156
const ua = navigator.userAgent;
155157
const isChrome = ua.includes("Chrome") && !ua.includes("Edg");
156-
console.log("ischrome", isChrome)
158+
console.log("ischrome", isChrome);
157159

158160
// 1. First check if getAuthToken is available (most common in Chrome)
159161
if (isChrome && chrome.identity && chrome.identity.getAuthToken) {
@@ -172,45 +174,40 @@ function getGoogleAuthToken() {
172174
}
173175
});
174176

175-
// 2. Otherwise try launchWebAuthFlow (Edge may support it)
177+
// 2. Otherwise try launchWebAuthFlow (Edge may support it)
176178
} else if (chrome.identity && chrome.identity.launchWebAuthFlow) {
177179
console.log("Trying chrome.identity.launchWebAuthFlow...");
178-
try {
179-
const token = await launchWebAuthFlowForGoogle();
180-
resolve(token);
181-
} catch (err) {
182-
reject(err);
183-
}
184-
180+
launchWebAuthFlowForGoogle()
181+
.then((token) => resolve(token))
182+
.catch((err) => reject(err));
185183
} else {
186184
reject("This browser does not support the chrome.identity API");
187185
}
188186
});
189187
}
190188

191-
192189
// Google Calendar API add Event
193190
async function addEventToCalendar(token, event) {
194191
// Google Calendar API call
195192
// Docs: https://developers.google.com/calendar/api/v3/reference/events/insert
196193
const res = await fetch("https://www.googleapis.com/calendar/v3/calendars/primary/events", {
197194
method: "POST",
198195
headers: {
199-
"Authorization": `Bearer ${token}`,
200-
"Content-Type": "application/json"
196+
Authorization: `Bearer ${token}`,
197+
"Content-Type": "application/json",
201198
},
202199
body: JSON.stringify({
203200
summary: event.title,
204201
description: event.description,
205202
start: {
206203
dateTime: event.startDate.toISOString(),
207-
timeZone: "Asia/Taipei"
204+
timeZone: "Asia/Taipei",
208205
},
209206
end: {
210207
dateTime: event.endDate.toISOString(),
211-
timeZone: "Asia/Taipei"
212-
}
213-
})
208+
timeZone: "Asia/Taipei",
209+
},
210+
}),
214211
});
215212

216213
if (!res.ok) {

0 commit comments

Comments
 (0)