feat: load year and month from page, and provide link to planning
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { IcsCalendar, IcsEvent } from "ts-ics";
|
||||
import { isPlanningUrl } from "./url";
|
||||
|
||||
type WorkEvent = {
|
||||
day: number;
|
||||
@@ -47,6 +48,53 @@ const makeIcsCalendar = (events: IcsEvent[]): IcsCalendar => ({
|
||||
events,
|
||||
});
|
||||
|
||||
const dataExtractor = () => {
|
||||
// extract year and month
|
||||
const monthSelect = document.getElementById(
|
||||
"select-pagemois",
|
||||
) as HTMLSelectElement;
|
||||
const yearSelect = document.getElementById(
|
||||
"select-pagean",
|
||||
) as HTMLSelectElement;
|
||||
|
||||
// extract events
|
||||
const trs = document.querySelectorAll("#clearfix-individuel table tr");
|
||||
|
||||
const events: WorkEvent[] = [];
|
||||
|
||||
// [Sam 01, RH]
|
||||
// [Dim 02, 7:00 - 19:00]
|
||||
for (const tr of trs) {
|
||||
const tds = tr.querySelectorAll("td");
|
||||
|
||||
const dateText = tds[0]?.textContent?.trim() ?? "";
|
||||
const dateMatch = dateText.match(/^[A-Z][a-z]{2}\s+(\d{2})$/);
|
||||
if (!dateMatch) continue;
|
||||
|
||||
const workText = tds[1]?.textContent?.trim() ?? "";
|
||||
const hoursMatch = workText.match(/^(\d+):(\d+) - (\d+):(\d+)$/);
|
||||
if (!hoursMatch) continue;
|
||||
|
||||
events.push({
|
||||
day: parseInt(dateMatch[1]!),
|
||||
start: {
|
||||
hours: parseInt(hoursMatch[1]!),
|
||||
minutes: parseInt(hoursMatch[2]!),
|
||||
},
|
||||
end: {
|
||||
hours: parseInt(hoursMatch[3]!),
|
||||
minutes: parseInt(hoursMatch[4]!),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
month: parseInt(monthSelect.value),
|
||||
year: parseInt(yearSelect.value),
|
||||
events,
|
||||
};
|
||||
};
|
||||
|
||||
export async function extractData(): Promise<{
|
||||
name: string;
|
||||
data: IcsCalendar;
|
||||
@@ -55,62 +103,23 @@ export async function extractData(): Promise<{
|
||||
active: true,
|
||||
currentWindow: true,
|
||||
});
|
||||
if (!tab?.id) {
|
||||
if (!tab?.id || !(await isPlanningUrl())) {
|
||||
throw new Error("URL incorrecte");
|
||||
}
|
||||
|
||||
const urlMatch = tab.url?.match(/\/(\d{2})-(\d{4})$/);
|
||||
if (!urlMatch) {
|
||||
throw new Error("URL incorrecte");
|
||||
}
|
||||
const year = parseInt(urlMatch[2]!);
|
||||
const month = parseInt(urlMatch[1]!);
|
||||
|
||||
try {
|
||||
const [eventsQuery] = await chrome.scripting.executeScript({
|
||||
const [query] = await chrome.scripting.executeScript({
|
||||
target: { tabId: tab.id },
|
||||
func: () => {
|
||||
const trs = document.querySelectorAll("#clearfix-individuel table tr");
|
||||
|
||||
const events: WorkEvent[] = [];
|
||||
|
||||
// [Sam 01, RH]
|
||||
// [Dim 02, 7:00 - 19:00]
|
||||
for (const tr of trs) {
|
||||
const tds = tr.querySelectorAll("td");
|
||||
|
||||
const dateText = tds[0]?.textContent?.trim() ?? "";
|
||||
const dateMatch = dateText.match(/^[A-Z][a-z]{2}\s+(\d{2})$/);
|
||||
if (!dateMatch) continue;
|
||||
|
||||
const workText = tds[1]?.textContent?.trim() ?? "";
|
||||
const hoursMatch = workText.match(/^(\d+):(\d+) - (\d+):(\d+)$/);
|
||||
if (!hoursMatch) continue;
|
||||
|
||||
events.push({
|
||||
day: parseInt(dateMatch[1]!),
|
||||
start: {
|
||||
hours: parseInt(hoursMatch[1]!),
|
||||
minutes: parseInt(hoursMatch[2]!),
|
||||
},
|
||||
end: {
|
||||
hours: parseInt(hoursMatch[3]!),
|
||||
minutes: parseInt(hoursMatch[4]!),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return events;
|
||||
},
|
||||
func: dataExtractor,
|
||||
});
|
||||
if (!eventsQuery?.result) throw undefined;
|
||||
if (!query?.result) throw undefined;
|
||||
|
||||
const { year, month, events } = query.result;
|
||||
|
||||
return {
|
||||
name: `gestime-${urlMatch[1]}-${urlMatch[2]}.ics`,
|
||||
name: `gestime-${year}-${month}.ics`,
|
||||
data: makeIcsCalendar(
|
||||
eventsQuery.result.map((event) =>
|
||||
makeIcsEvent({ year, month, ...event }),
|
||||
),
|
||||
events.map((event) => makeIcsEvent({ year, month, ...event })),
|
||||
),
|
||||
};
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user