-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
143 lines (116 loc) · 3.35 KB
/
Copy pathbackground.js
File metadata and controls
143 lines (116 loc) · 3.35 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
131
132
133
134
135
136
137
138
139
140
141
142
143
const XENO_CANTO_API_BASE = "https://www.xeno-canto.org/api/3/recordings";
const API_KEY = "";
const CACHE_KEY = 'species-data-cache';
const ONE_DAY = 24 * 60 * 60 * 1000;
self.importScripts('sw-indexedDB.js');
const idb = new ServiceWorkerIDB();
chrome.runtime.onInstalled.addListener(() => {
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => {});
checkAndUpdateData();
});
chrome.runtime.onStartup.addListener(() => {
checkAndUpdateData();
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'searchRecordings') {
handleSearchRequest(request.query)
.then(response => {
sendResponse({ success: true, data: response });
})
.catch(error => {
sendResponse({
success: false,
error: error.message || 'Unknown error occurred'
});
});
return true;
}
if (request.action === 'getCachedData') {
getCacheData().then(data => {
sendResponse({ success: true, data });
}).catch(error => {
sendResponse({ success: false, error: error.message });
});
return true;
}
});
async function handleSearchRequest(query) {
try {
const apiUrl = `${XENO_CANTO_API_BASE}?key=${API_KEY}&query=sp:"${encodeURIComponent(query)}"`;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 50000);
const response = await fetch(apiUrl, {
signal: controller.signal,
headers: {
'User-Agent': 'Chrome Extension - Xeno-Canto Player'
}
});
clearTimeout(timeoutId);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (!data || typeof data !== 'object') {
throw new Error('Invalid response format');
}
return data;
} catch (error) {
if (error.name === 'AbortError') {
throw new Error('Request timeout');
}
throw error;
}
}
chrome.runtime.onUpdateAvailable.addListener(() => {});
async function checkAndUpdateData() {
try {
const cacheData = await getCacheData();
const now = Date.now();
const needsUpdate = !cacheData ||
!cacheData.lastUpdate ||
(now - cacheData.lastUpdate) > ONE_DAY ||
!cacheData.speciesData;
if (needsUpdate) {
await downloadAndCacheData();
}
} catch (error) {
}
}
async function getCacheData() {
try {
const data = await idb.get(CACHE_KEY);
return data;
} catch (error) {
return null;
}
}
async function downloadAndCacheData() {
try {
const response = await fetchWithTimeout("https://ebird.siansiansu.com/species.json", 30000);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const speciesData = await response.json();
if (!Array.isArray(speciesData)) {
throw new Error('Invalid data format received');
}
const cacheData = {
speciesData,
lastUpdate: Date.now(),
dataSource: 'remote',
version: '1.0'
};
await idb.put(CACHE_KEY, cacheData);
} catch (error) {
}
}
function fetchWithTimeout(url, timeout = 15000) {
return Promise.race([
fetch(url),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Request timed out")), timeout)
)
]);
}