-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
52 lines (42 loc) · 1.54 KB
/
background.js
File metadata and controls
52 lines (42 loc) · 1.54 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
const CONFIG = {
API_TIMEOUT: 10000,
APP_ID: 'EVEKW57V7X'
};
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'wolframQuery') {
handleWolframQuery(request.input)
.then(result => sendResponse({ result }))
.catch(error => sendResponse({ result: `Error: ${error.message}` }));
return true;
}
});
async function handleWolframQuery(input) {
const encodedInput = encodeURIComponent(input);
const url = `https://api.wolframalpha.com/v1/result?appid=${CONFIG.APP_ID}&i=${encodedInput}`;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), CONFIG.API_TIMEOUT);
try {
const response = await fetch(url, { signal: controller.signal });
clearTimeout(timeoutId);
if (!response.ok) {
if (response.status === 501) {
throw new Error('Expression format not recognized. Try rephrasing in plain English.');
} else if (response.status === 400) {
throw new Error('Invalid input format');
} else {
throw new Error(`API returned status ${response.status}`);
}
}
const data = await response.text();
if (!data || data.trim() === '') {
throw new Error('No result returned. Try rephrasing your query.');
}
return data;
} catch (error) {
clearTimeout(timeoutId);
if (error.name === 'AbortError') {
throw new Error('Request timed out. Please try again.');
}
throw error;
}
}