-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
174 lines (147 loc) · 4.9 KB
/
content.js
File metadata and controls
174 lines (147 loc) · 4.9 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
let isEnabled = false;
let currentElement = null;
// Toggle extension on/off with ALT+C
document.addEventListener('keydown', (e) => {
if (e.altKey && e.key === 'c') {
isEnabled = !isEnabled;
// Remove any existing borders when disabling
if (!isEnabled && currentElement) {
currentElement.style.outline = '';
currentElement.style.backgroundColor = '';
currentElement = null;
}
// Show status message
showStatus(isEnabled ? '✓ Extension Enabled' : '✗ Extension Disabled');
}
});
// Handle mouse movement to detect text sections
document.addEventListener('mouseover', (e) => {
if (!isEnabled) return;
// Remove highlight from previous element
if (currentElement && currentElement !== e.target) {
currentElement.style.outline = '';
currentElement.style.backgroundColor = '';
}
// Only highlight elements that contain text
const element = e.target;
const hasText = element.innerText && element.innerText.trim().length > 0;
if (hasText) {
currentElement = element;
// Add dotted border and light background highlight
currentElement.style.outline = '2px dotted #007bff';
currentElement.style.backgroundColor = 'rgba(0, 123, 255, 0.1)';
}
});
document.addEventListener('mouseout', (e) => {
if (!isEnabled) return;
// Remove highlight when mouse leaves
if (e.target === currentElement) {
e.target.style.outline = '';
e.target.style.backgroundColor = '';
}
});
// Copy with formatting preserved - LEFT CLICK
document.addEventListener('click', (e) => {
if (!isEnabled) return;
// Only trigger on left mouse button
if (e.button !== 0) return;
const element = e.target;
// Check if element has text
if (!element.innerText || element.innerText.trim().length === 0) return;
// Prevent default action to avoid interfering with page links
e.preventDefault();
try {
// Create a range and selection to copy HTML with formatting
const range = document.createRange();
range.selectNodeContents(element);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
// Copy both plain text and HTML to clipboard
const plainText = element.innerText || element.textContent;
const htmlContent = element.innerHTML;
// Use the Clipboard API to write both formats
const clipboardItem = new ClipboardItem({
'text/plain': new Blob([plainText], { type: 'text/plain' }),
'text/html': new Blob([htmlContent], { type: 'text/html' })
});
navigator.clipboard.write([clipboardItem]).then(() => {
showStatus('✓ Text copied successfully!', 'success');
// Clear selection
selection.removeAllRanges();
}).catch(err => {
// Fallback to plain text if HTML copy fails
navigator.clipboard.writeText(plainText).then(() => {
showStatus('✓ Text copied successfully!', 'success');
selection.removeAllRanges();
}).catch(() => {
showStatus('✗ Failed to copy text', 'error');
});
});
} catch (err) {
// Fallback method
const text = element.innerText || element.textContent;
navigator.clipboard.writeText(text.trim()).then(() => {
showStatus('✓ Text copied successfully!', 'success');
}).catch(() => {
showStatus('✗ Failed to copy text', 'error');
});
}
});
// Show status message with different styles
function showStatus(message, type = 'info') {
// Remove any existing status messages first
const existingStatus = document.querySelector('.section-highlighter-status');
if (existingStatus) {
existingStatus.remove();
}
const statusDiv = document.createElement('div');
statusDiv.className = 'section-highlighter-status';
statusDiv.textContent = message;
// Different colors based on type
let bgColor = '#333';
if (type === 'success') {
bgColor = '#28a745'; // Green
} else if (type === 'error') {
bgColor = '#dc3545'; // Red
}
statusDiv.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: ${bgColor};
color: white;
padding: 15px 25px;
border-radius: 8px;
z-index: 999999;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
font-size: 15px;
font-weight: 500;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
animation: slideIn 0.3s ease-out;
`;
// Add animation
const style = document.createElement('style');
style.textContent = `
@keyframes slideIn {
from {
transform: translateX(400px);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
`;
document.head.appendChild(style);
document.body.appendChild(statusDiv);
setTimeout(() => {
statusDiv.style.transition = 'opacity 0.3s ease-out';
statusDiv.style.opacity = '0';
setTimeout(() => {
statusDiv.remove();
style.remove();
}, 300);
}, 2500);
}