-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
130 lines (110 loc) · 3.57 KB
/
background.js
File metadata and controls
130 lines (110 loc) · 3.57 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
let kronosCredentials = null;
let googleAccessToken = null;
// Load saved credentials
chrome.storage.local.get('kronosCredentials', (data) => {
if (data.kronosCredentials) {
kronosCredentials = data.kronosCredentials;
}
});
// Handle Kronos login
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'submitMFA') {
handleMFA(message.mfaCode);
}
else if (message.type === 'scheduleData') {
processSchedule(message.schedule);
}
});
function handleMFA(mfaCode) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: (code) => {
document.getElementById('mfaCode').value = code;
document.getElementById('verify-mfa-button').click();
},
args: [mfaCode]
});
});
}
async function processSchedule(schedule) {
if (!schedule.length) {
updateStatus('⚠️ No shifts found in schedule');
return;
}
updateStatus(`📅 Processing ${schedule.length} shifts...`);
try {
// Authenticate with Google
googleAccessToken = await getGoogleAuthToken();
// Create calendar events
for (const shift of schedule) {
await createCalendarEvent(shift);
updateStatus(`✅ Added: ${shift.date} ${shift.start}-${shift.end}`);
}
updateStatus('🎉 Schedule synced to Google Calendar!');
} catch (error) {
updateStatus(`❌ Error: ${error.message}`);
}
}
function getGoogleAuthToken() {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({ interactive: true }, (token) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
} else {
resolve(token);
}
});
});
}
async function createCalendarEvent(shift) {
// Parse shift date/time
const shiftDate = parseDate(shift.date);
const startDateTime = combineDateTime(shiftDate, shift.start);
const endDateTime = combineDateTime(shiftDate, shift.end);
// Create event payload
const event = {
summary: 'Work Shift',
location: 'Cumberland Farms',
start: { dateTime: startDateTime.toISOString(), timeZone: 'America/New_York' },
end: { dateTime: endDateTime.toISOString(), timeZone: 'America/New_York' },
reminders: { useDefault: true }
};
// Send to Google Calendar API
const response = await fetch('https://www.googleapis.com/calendar/v3/calendars/primary/events', {
method: 'POST',
headers: {
'Authorization': `Bearer ${googleAccessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(event)
});
if (!response.ok) {
throw new Error('Failed to create calendar event');
}
}
// Helper functions
function parseDate(dateStr) {
// Format: "Wed 06/05/24"
const [, month, day, year] = dateStr.match(/\w{3} (\d{2})\/(\d{2})\/(\d{2})/);
return new Date(`20${year}-${month}-${day}`);
}
function combineDateTime(date, timeStr) {
const [hours, minutes] = timeStr.includes('AM') || timeStr.includes('PM')
? parse12HourTime(timeStr)
: timeStr.split(':').map(Number);
const dateTime = new Date(date);
dateTime.setHours(hours, minutes);
return dateTime;
}
function parse12HourTime(timeStr) {
const [, time, period] = timeStr.match(/(\d+):(\d+) (AM|PM)/);
let hours = parseInt(time);
const minutes = parseInt(minutes);
if (period === 'PM' && hours < 12) hours += 12;
if (period === 'AM' && hours === 12) hours = 0;
return [hours, minutes];
}
function updateStatus(text) {
chrome.runtime.sendMessage({ type: 'syncStatus', text });
}