-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
83 lines (74 loc) · 2.66 KB
/
background.js
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
chrome.runtime.onInstalled.addListener(() => {
console.log("Background script installed");
chrome.contextMenus.create({
id: "startScreenshot",
title: "Start Screenshot",
contexts: ["all"]
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "startScreenshot" && tab.id) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: startScreenshot
});
}
});
});
chrome.action.onClicked.addListener((tab) => {
if (tab.id) {
console.log("Action button clicked");
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: startScreenshot
});
} else {
console.error("No active tab found");
}
});
function startScreenshot() {
console.log("startScreenshot function executed");
document.body.style.userSelect = 'none';
const overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.backgroundColor = 'rgba(0,0,0,0.3)';
overlay.style.zIndex = '9999';
document.body.appendChild(overlay);
const selectionBox = document.createElement('div');
selectionBox.style.border = '2px dashed #FFF';
selectionBox.style.position = 'absolute';
selectionBox.style.display = 'none';
overlay.appendChild(selectionBox);
let startX, startY, isSelecting = false;
overlay.addEventListener('mousedown', (e) => {
console.log("mousedown event");
startX = e.clientX;
startY = e.clientY;
isSelecting = true;
selectionBox.style.left = `${startX}px`;
selectionBox.style.top = `${startY}px`;
selectionBox.style.width = '0';
selectionBox.style.height = '0';
selectionBox.style.display = 'block';
});
overlay.addEventListener('mousemove', (e) => {
if (!isSelecting) return;
console.log("mousemove event");
const currentX = e.clientX;
const currentY = e.clientY;
const width = currentX - startX;
const height = currentY - startY;
selectionBox.style.width = `${Math.abs(width)}px`;
selectionBox.style.height = `${Math.abs(height)}px`;
selectionBox.style.left = `${width < 0 ? currentX : startX}px`;
selectionBox.style.top = `${height < 0 ? currentY : startY}px`;
});
overlay.addEventListener('mouseup', () => {
console.log("mouseup event");
isSelecting = false;
document.body.removeChild(overlay);
});
}