-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
150 lines (126 loc) · 4.86 KB
/
content-script.js
File metadata and controls
150 lines (126 loc) · 4.86 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
// Content script for screen capture handling
(() => {
let isSelecting = false;
let startX = 0;
let startY = 0;
let overlay = null;
let selectionBox = null;
let dimensionsLabel = null;
// Listen for messages from the popup
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'startCapture') {
startRegionCapture();
}
});
function startRegionCapture() {
// Create overlay
overlay = document.createElement('div');
overlay.className = 'screen-capture-overlay';
document.body.appendChild(overlay);
// Create selection box
selectionBox = document.createElement('div');
selectionBox.className = 'selection-box';
selectionBox.style.display = 'none';
document.body.appendChild(selectionBox);
// Create dimensions label
dimensionsLabel = document.createElement('div');
dimensionsLabel.className = 'selection-dimensions';
dimensionsLabel.style.display = 'none';
document.body.appendChild(dimensionsLabel);
// Add mouse event listeners
overlay.addEventListener('mousedown', handleMouseDown);
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
document.addEventListener('keydown', handleKeyDown);
}
function handleMouseDown(e) {
isSelecting = true;
startX = e.clientX;
startY = e.clientY;
selectionBox.style.display = 'block';
dimensionsLabel.style.display = 'block';
updateSelection(e);
}
function handleMouseMove(e) {
if (!isSelecting) return;
updateSelection(e);
}
function handleMouseUp(e) {
if (!isSelecting) return;
isSelecting = false;
// Get final selection
const rect = selectionBox.getBoundingClientRect();
if (rect.width > 5 && rect.height > 5) {
captureRegion(rect);
}
cleanup();
}
function handleKeyDown(e) {
if (e.key === 'Escape') {
cleanup();
}
}
function updateSelection(e) {
const currentX = e.clientX;
const currentY = e.clientY;
const left = Math.min(startX, currentX);
const top = Math.min(startY, currentY);
const width = Math.abs(currentX - startX);
const height = Math.abs(currentY - startY);
selectionBox.style.left = left + 'px';
selectionBox.style.top = top + 'px';
selectionBox.style.width = width + 'px';
selectionBox.style.height = height + 'px';
// Update dimensions label
dimensionsLabel.textContent = `${width} × ${height}`;
dimensionsLabel.style.left = (left + width / 2) + 'px';
dimensionsLabel.style.top = (top - 30) + 'px';
}
async function captureRegion(rect) {
try {
const tab = await browser.tabs.captureTab(null, {
format: 'png'
});
// Create canvas to crop image
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const dpr = window.devicePixelRatio;
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
ctx.drawImage(img,
rect.left * dpr, rect.top * dpr,
rect.width * dpr, rect.height * dpr,
0, 0,
rect.width * dpr, rect.height * dpr
);
const croppedImage = canvas.toDataURL('image/png');
// Send back to popup
browser.runtime.sendMessage({
action: 'regionCaptured',
imageData: croppedImage,
dimensions: {
width: rect.width,
height: rect.height
}
});
};
img.src = tab;
} catch (error) {
console.error('Capture error:', error);
}
}
function cleanup() {
if (overlay) overlay.remove();
if (selectionBox) selectionBox.remove();
if (dimensionsLabel) dimensionsLabel.remove();
overlay = null;
selectionBox = null;
dimensionsLabel = null;
isSelecting = false;
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
document.removeEventListener('keydown', handleKeyDown);
}
})();