-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
127 lines (109 loc) · 4.01 KB
/
Copy pathbackground.js
File metadata and controls
127 lines (109 loc) · 4.01 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
// Background Service Worker - Handles API calls and message routing
// Import Gemini service (note: in Manifest V3, we use importScripts)
importScripts('gemini-service.js');
// Listen for messages from popup and content scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// Handle async operations
handleMessage(request, sender).then(sendResponse);
return true; // Keep channel open for async response
});
async function handleMessage(request, sender) {
switch (request.action) {
case 'testApiKey':
return await testApiKeyHandler(request.apiKey, request.provider);
case 'detectJargon':
return await detectJargonHandler(request.pageText, request.apiKey, request.abortSignal);
case 'simplifyText':
return await simplifyTextHandler(request.text, request.apiKey, request.abortSignal);
default:
return { success: false, error: 'Unknown action' };
}
}
/**
* Test API key validity
*/
async function testApiKeyHandler(apiKey, provider) {
try {
const result = await testApiKey(apiKey, provider);
return result;
} catch (error) {
return { success: false, error: error.message };
}
}
/**
* Detect jargon
*/
async function detectJargonHandler(pageText, apiKey, abortSignal) {
try {
// Check if already aborted
if (abortSignal) {
return { success: false, error: 'Request aborted', aborted: true };
}
const result = await detectJargon(pageText, apiKey);
return { success: true, data: result };
} catch (error) {
if (error.name === 'AbortError') {
return { success: false, error: 'Request aborted', aborted: true };
}
return { success: false, error: error.message };
}
}
/**
* Simplify text into plain English
*/
async function simplifyTextHandler(text, apiKey, abortSignal) {
try {
// Check if already aborted
if (abortSignal) {
return { success: false, error: 'Request aborted', aborted: true };
}
const result = await simplifyText(text, apiKey);
return { success: true, data: result };
} catch (error) {
if (error.name === 'AbortError') {
return { success: false, error: 'Request aborted', aborted: true };
}
return { success: false, error: error.message };
}
}
// Listen for extension installation
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
console.log('InclusiveRead installed successfully!');
// Set default settings
chrome.storage.sync.set({
jargonEnabled: false,
sensoryEnabled: false,
dyslexiaEnabled: false,
dyslexiaFont: 'opendyslexic',
letterSpacing: 1,
lineHeight: 1.6,
wordSpacing: 3,
overlayColor: 'none',
bionicReading: false,
ttsEnabled: false,
ttsSpeed: 1,
ttsPauseOnPunctuation: true,
ttsWordHighlight: true,
apiProvider: 'gemini' // Default to Gemini for new installs
});
// Open welcome page - redirect to installation guide
chrome.tabs.create({
url: 'https://inclusive-read.vercel.app/#installation'
});
}
// Migration for existing users on update
if (details.reason === 'update') {
chrome.storage.local.get(['apiKey', 'geminiKey'], (localResult) => {
chrome.storage.sync.get(['apiProvider'], (syncResult) => {
const hasOpenRouterKey = !!localResult.apiKey;
const hasGeminiKey = !!localResult.geminiKey;
// If user has Gemini key but not OpenRouter, or no keys at all, default to Gemini
if (!hasOpenRouterKey) {
chrome.storage.sync.set({ apiProvider: 'gemini' });
console.log('InclusiveRead: Migrated to Gemini provider');
}
});
});
}
});