Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
95 lines (80 loc) · 2.44 KB
/
Copy pathbackground.js
File metadata and controls
95 lines (80 loc) · 2.44 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
// Background script for Nummi Chrome Extension
// Create context menu when extension installs
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "nummi-fix-grammar",
title: "Fix with Nummi",
contexts: ["selection"]
});
});
// Handle context menu clicks
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "nummi-fix-grammar") {
const selectedText = info.selectionText;
if (!selectedText || selectedText.trim().length === 0) {
return;
}
try {
// Show loading state
chrome.tabs.sendMessage(tab.id, {
action: "showLoading",
text: selectedText
});
// Call Nummi API
const response = await fetch('https://ship.nummi.ai/api/grammar-fix', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: selectedText,
useVoiceProfile: true
})
});
if (!response.ok) {
throw new Error(`API Error: ${response.status}`);
}
const data = await response.json();
// Send fixed text back to content script
chrome.tabs.sendMessage(tab.id, {
action: "replaceText",
originalText: selectedText,
fixedText: data.fixedContent,
usedVoiceProfile: data.usedVoiceProfile
});
} catch (error) {
console.error('Nummi API Error:', error);
// Show error to user
chrome.tabs.sendMessage(tab.id, {
action: "showError",
error: error.message
});
}
}
});
// Handle messages from popup or content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "fixGrammar") {
// Handle direct grammar fix requests
fixGrammarAPI(request.text)
.then(result => sendResponse({ success: true, data: result }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Keep message channel open for async response
}
});
async function fixGrammarAPI(text) {
const response = await fetch('https://ship.nummi.ai/api/grammar-fix', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: text,
useVoiceProfile: true
})
});
if (!response.ok) {
throw new Error(`API Error: ${response.status}`);
}
return await response.json();
}