Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
186 lines (164 loc) · 4.85 KB
/
Copy pathcontent.js
File metadata and controls
186 lines (164 loc) · 4.85 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Content script for Nummi Chrome Extension
// This runs on every webpage
let currentSelection = null;
let loadingIndicator = null;
// Listen for messages from background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.action) {
case "showLoading":
showLoadingIndicator(request.text);
break;
case "replaceText":
replaceSelectedText(request.originalText, request.fixedText, request.usedVoiceProfile);
break;
case "showError":
showError(request.error);
break;
}
});
function showLoadingIndicator(text) {
hideLoadingIndicator(); // Remove any existing indicator
loadingIndicator = document.createElement('div');
loadingIndicator.id = 'nummi-loading';
loadingIndicator.innerHTML = `
<div style="
position: fixed;
top: 20px;
right: 20px;
background: #1a1a1a;
color: white;
padding: 12px 16px;
border-radius: 8px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 14px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
z-index: 10000;
display: flex;
align-items: center;
gap: 8px;
">
<div style="
width: 16px;
height: 16px;
border: 2px solid #666;
border-top: 2px solid #4ade80;
border-radius: 50%;
animation: spin 1s linear infinite;
"></div>
Nummi is fixing your grammar...
</div>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
`;
document.body.appendChild(loadingIndicator);
}
function hideLoadingIndicator() {
if (loadingIndicator) {
loadingIndicator.remove();
loadingIndicator = null;
}
}
function replaceSelectedText(originalText, fixedText, usedVoiceProfile) {
hideLoadingIndicator();
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
// Check if the selected text still matches what we're trying to replace
if (range.toString().trim() === originalText.trim()) {
// Replace the text
range.deleteContents();
range.insertNode(document.createTextNode(fixedText));
// Show success notification
showSuccessNotification(usedVoiceProfile);
// Clear selection
selection.removeAllRanges();
} else {
showError("Text selection has changed. Please try again.");
}
}
}
function showSuccessNotification(usedVoiceProfile) {
const notification = document.createElement('div');
notification.innerHTML = `
<div style="
position: fixed;
top: 20px;
right: 20px;
background: #059669;
color: white;
padding: 12px 16px;
border-radius: 8px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 14px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
z-index: 10000;
display: flex;
align-items: center;
gap: 8px;
">
✨ Fixed with ${usedVoiceProfile ? 'your personal style!' : 'Nummi!'}
${usedVoiceProfile ? '🧠' : ''}
</div>
`;
document.body.appendChild(notification);
// Auto-remove after 3 seconds
setTimeout(() => {
if (notification.parentNode) {
notification.remove();
}
}, 3000);
}
function showError(errorMessage) {
hideLoadingIndicator();
const errorNotification = document.createElement('div');
errorNotification.innerHTML = `
<div style="
position: fixed;
top: 20px;
right: 20px;
background: #dc2626;
color: white;
padding: 12px 16px;
border-radius: 8px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 14px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
z-index: 10000;
">
❌ Error: ${errorMessage}
</div>
`;
document.body.appendChild(errorNotification);
// Auto-remove after 5 seconds
setTimeout(() => {
if (errorNotification.parentNode) {
errorNotification.remove();
}
}, 5000);
}
// Keyboard shortcut (Ctrl+Shift+F or Cmd+Shift+F)
document.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'F') {
e.preventDefault();
const selection = window.getSelection();
const selectedText = selection.toString().trim();
if (selectedText.length > 0) {
// Trigger grammar fix via background script
chrome.runtime.sendMessage({
action: "fixGrammar",
text: selectedText
}, (response) => {
if (response.success) {
replaceSelectedText(selectedText, response.data.fixedContent, response.data.usedVoiceProfile);
} else {
showError(response.error);
}
});
showLoadingIndicator(selectedText);
}
}
});