-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
270 lines (231 loc) · 9.51 KB
/
content-script.js
File metadata and controls
270 lines (231 loc) · 9.51 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// ContextClipCalendar Content Script
// This script runs on all web pages to enable context menu functionality
console.log('ContextClipCalendar content script loaded');
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('Content script received message:', request);
switch (request.action) {
case 'getSelectedText':
const selectedText = window.getSelection().toString().trim();
sendResponse({ selectedText });
break;
case 'showNotification':
showNotification(request.message, request.type || 'info');
sendResponse({ success: true });
break;
case 'highlightText':
highlightSelectedText();
sendResponse({ success: true });
break;
default:
sendResponse({ error: 'Unknown action' });
}
return true; // Keep the message channel open for async responses
});
// Show notification on the page
function showNotification(message, type = 'info') {
// Remove any existing notifications
const existingNotifications = document.querySelectorAll('.contextclip-notification');
existingNotifications.forEach(notification => notification.remove());
// Create notification element
const notification = document.createElement('div');
notification.className = 'contextclip-notification';
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
padding: 16px 20px;
border-radius: 8px;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
z-index: 10000;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Roboto, sans-serif;
font-size: 14px;
font-weight: 500;
max-width: 320px;
word-wrap: break-word;
transform: translateX(100%);
transition: transform 0.3s ease-in-out;
display: flex;
align-items: center;
gap: 8px;
`;
// Set colors based on type
let backgroundColor, textColor, iconSvg;
switch (type) {
case 'success':
backgroundColor = '#10b981';
textColor = 'white';
iconSvg = `
<svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
`;
break;
case 'error':
backgroundColor = '#ef4444';
textColor = 'white';
iconSvg = `
<svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
`;
break;
case 'warning':
backgroundColor = '#f59e0b';
textColor = 'white';
iconSvg = `
<svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.732-.833-2.464 0L3.34 16.5c-.77.833.192 2.5 1.732 2.5z"></path>
</svg>
`;
break;
default:
backgroundColor = '#3b82f6';
textColor = 'white';
iconSvg = `
<svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
`;
}
notification.style.backgroundColor = backgroundColor;
notification.style.color = textColor;
// Add icon and message
notification.innerHTML = `
${iconSvg}
<span>${message}</span>
`;
// Add to page
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.style.transform = 'translateX(0)';
}, 100);
// Remove after 4 seconds
setTimeout(() => {
notification.style.transform = 'translateX(100%)';
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
}, 300);
}, 4000);
}
// Highlight selected text temporarily
function highlightSelectedText() {
const selection = window.getSelection();
if (selection.rangeCount === 0) return;
const range = selection.getRangeAt(0);
const selectedText = range.toString().trim();
if (!selectedText) return;
// Create highlight element
const highlight = document.createElement('span');
highlight.style.cssText = `
background: rgba(16, 185, 129, 0.2);
border-radius: 4px;
padding: 2px 4px;
transition: all 0.3s ease;
box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.3);
`;
try {
// Wrap the selected text with highlight
range.surroundContents(highlight);
// Remove highlight after 2 seconds
setTimeout(() => {
if (highlight.parentNode) {
const parent = highlight.parentNode;
parent.replaceChild(document.createTextNode(selectedText), highlight);
parent.normalize(); // Merge adjacent text nodes
}
}, 2000);
// Clear selection
selection.removeAllRanges();
} catch (error) {
console.log('Could not highlight selection:', error);
// Fallback: just clear selection
selection.removeAllRanges();
}
}
// Enhanced text selection detection for better context menu positioning
let lastSelectedText = '';
let selectionTimeout = null;
document.addEventListener('selectionchange', () => {
// Clear previous timeout
if (selectionTimeout) {
clearTimeout(selectionTimeout);
}
// Debounce selection changes
selectionTimeout = setTimeout(() => {
const selectedText = window.getSelection().toString().trim();
if (selectedText && selectedText !== lastSelectedText) {
lastSelectedText = selectedText;
console.log('New text selected:', selectedText.substring(0, 50) + '...');
// Send selection info to background script (for potential future features)
chrome.runtime.sendMessage({
action: 'textSelected',
text: selectedText,
url: window.location.href,
timestamp: Date.now()
}).catch(error => {
// Ignore errors if background script is not ready
console.log('Could not send selection to background script:', error);
});
} else if (!selectedText) {
lastSelectedText = '';
}
}, 100);
});
// Keyboard shortcut support (optional enhancement)
document.addEventListener('keydown', (event) => {
// Ctrl+Shift+C (or Cmd+Shift+C on Mac) to quickly register selected text
if ((event.ctrlKey || event.metaKey) && event.shiftKey && event.key === 'C') {
const selectedText = window.getSelection().toString().trim();
if (selectedText) {
event.preventDefault();
// Send to background script for processing
chrome.runtime.sendMessage({
action: 'createCalendarEvent',
text: selectedText,
source: 'keyboard_shortcut'
}).then(response => {
if (response && response.success) {
showNotification(chrome.i18n.getMessage('scheduleSuccessfullyRegistered'), 'success');
highlightSelectedText();
} else {
showNotification(response?.error || chrome.i18n.getMessage('errorRegisteringSchedule'), 'error');
}
}).catch(error => {
console.error('Keyboard shortcut calendar action error:', error);
showNotification(chrome.i18n.getMessage('errorRegisteringSchedule'), 'error');
});
} else {
showNotification(chrome.i18n.getMessage('pleaseSelectTextFirst'), 'warning');
}
}
});
// Page load optimization: inject CSS only when needed
function injectNotificationStyles() {
if (document.getElementById('contextclip-styles')) return;
const styles = document.createElement('style');
styles.id = 'contextclip-styles';
styles.textContent = `
.contextclip-notification {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Roboto, sans-serif !important;
}
.contextclip-notification svg {
flex-shrink: 0;
}
/* Ensure notifications appear above all content */
.contextclip-notification {
z-index: 2147483647 !important;
}
`;
document.head.appendChild(styles);
}
// Initialize styles when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', injectNotificationStyles);
} else {
injectNotificationStyles();
}
console.log('ContextClipCalendar content script initialized successfully');