-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
110 lines (95 loc) · 4.59 KB
/
Copy pathpopup.js
File metadata and controls
110 lines (95 loc) · 4.59 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
document.addEventListener('DOMContentLoaded', function() {
const updateButton = document.getElementById('updateButton');
updateButton.addEventListener('click', updateTimes);
// Load saved city and update times when the popup opens
chrome.storage.local.get('city', function(data) {
if (chrome.runtime.lastError) {
console.error("Error retrieving city:", chrome.runtime.lastError);
} else if (data.city) {
document.getElementById('city').value = data.city;
}
updateTimes();
});
// Update all static text
updateStaticText();
});
function updateStaticText() {
document.getElementById('headerText').textContent = chrome.i18n.getMessage('shabbatTimes');
document.getElementById('city').placeholder = chrome.i18n.getMessage('cityPlaceholder');
document.getElementById('updateButton').textContent = chrome.i18n.getMessage('updateButton');
document.getElementById('entrance-label').textContent = chrome.i18n.getMessage('shabbatEntrance') + ':';
document.getElementById('exit-label').textContent = chrome.i18n.getMessage('shabbatExit') + ':';
}
async function getShabbatTimes(city) {
const lang = chrome.i18n.getUILanguage().split('-')[0];
const apiUrl = `https://www.hebcal.com/shabbat?cfg=json&city=${encodeURIComponent(city)}&lg=${lang}`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (!data.items || !Array.isArray(data.items)) {
throw new Error('Invalid response format');
}
const items = data.items;
const entranceItem = items.find(item => item.category === "candles");
const exitItem = items.find(item => item.category === "havdalah");
// Helper function to extract time from title
const extractTime = (title) => {
const parts = title.split(': ');
return parts.length > 1 ? parts[parts.length - 1] : title;
};
return {
entrance: entranceItem ? extractTime(entranceItem.title) : chrome.i18n.getMessage('notFound'),
exit: exitItem ? extractTime(exitItem.title) : chrome.i18n.getMessage('notFound')
};
} catch (error) {
console.error("Error fetching Shabbat times:", error);
throw error;
}
}
async function updateTimes() {
const city = document.getElementById('city').value || "Tel Aviv";
const entranceTimeElement = document.getElementById('entrance-time');
const exitTimeElement = document.getElementById('exit-time');
const errorMessageElement = document.getElementById('error-message');
try {
errorMessageElement.textContent = '';
entranceTimeElement.textContent = chrome.i18n.getMessage('loading');
exitTimeElement.textContent = chrome.i18n.getMessage('loading');
const times = await getShabbatTimes(city);
// Update the display with localized labels
document.getElementById('entrance-label').textContent = chrome.i18n.getMessage('shabbatEntrance') + ':';
document.getElementById('exit-label').textContent = chrome.i18n.getMessage('shabbatExit') + ':';
entranceTimeElement.textContent = times.entrance;
exitTimeElement.textContent = times.exit;
// Save the city for next time
chrome.storage.local.set({ city: city }, () => {
if (chrome.runtime.lastError) {
console.error("Error saving city:", chrome.runtime.lastError);
}
});
// Update the header with the next Friday's date
updateHeader();
} catch (error) {
entranceTimeElement.textContent = chrome.i18n.getMessage('error');
exitTimeElement.textContent = chrome.i18n.getMessage('error');
errorMessageElement.textContent = `${chrome.i18n.getMessage('errorLoading')}: ${error.message}`;
console.error("Error updating times:", error);
}
}
function getNextFriday(date) {
const dayOfWeek = date.getDay();
const daysUntilFriday = (5 - dayOfWeek + 7) % 7;
const nextFriday = new Date(date);
nextFriday.setDate(date.getDate() + daysUntilFriday);
return nextFriday;
}
function updateHeader() {
const d = new Date();
const nextFriday = getNextFriday(d);
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
const formatter = new Intl.DateTimeFormat(chrome.i18n.getUILanguage(), options);
document.getElementById('headerText').textContent = `${chrome.i18n.getMessage('shabbatTimesFor')} ${formatter.format(nextFriday)}`;
}