-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
209 lines (180 loc) · 5.4 KB
/
content.js
File metadata and controls
209 lines (180 loc) · 5.4 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
let canvas, ctx;
let mouse = { x: 0, y: 0 };
let last = null;
let trail = [];
let animationFrame;
let isActive = false;
function createCanvas() {
canvas = document.createElement('canvas');
canvas.style.position = 'fixed';
canvas.style.top = 0;
canvas.style.left = 0;
canvas.style.pointerEvents = 'none';
canvas.style.zIndex = 999999;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
ctx = canvas.getContext('2d');
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
}
function removeCanvas() {
if (canvas) {
cancelAnimationFrame(animationFrame);
document.body.removeChild(canvas);
canvas = null;
ctx = null;
}
}
function draw(color, fadeRate) {
if (!ctx) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < trail.length; i++) {
const p = trail[i];
ctx.beginPath();
ctx.arc(p.x, p.y, 8, 0, 2 * Math.PI, false);
ctx.fillStyle = `rgba(${color.r}, ${color.g}, ${color.b}, ${p.alpha})`;
ctx.shadowColor = `rgba(${color.r}, ${color.g}, ${color.b}, 0.8)`;
ctx.shadowBlur = 15;
ctx.fill();
p.alpha -= fadeRate;
}
trail = trail.filter(p => p.alpha > 0);
// Only continue animation if laser is still active
if (isActive) {
animationFrame = requestAnimationFrame(() => draw(color, fadeRate));
}
}
function startLaser(config) {
if (isActive) return;
isActive = true;
createCanvas();
last = null;
trail = [];
document.addEventListener('mousemove', handleMouse);
draw(config.color, config.fadeRate);
}
function stopLaser() {
if (!isActive) return;
isActive = false;
document.removeEventListener('mousemove', handleMouse);
// Cancel animation frame to stop drawing
if (animationFrame) {
cancelAnimationFrame(animationFrame);
}
removeCanvas();
trail = [];
}
function handleMouse(e) {
const newPoint = { x: e.clientX, y: e.clientY, alpha: 1 };
if (last) {
const dx = newPoint.x - last.x;
const dy = newPoint.y - last.y;
const dist = Math.hypot(dx, dy);
const steps = Math.floor(dist / 4);
for (let i = 1; i <= steps; i++) {
const t = i / steps;
trail.push({
x: last.x + dx * t,
y: last.y + dy * t,
alpha: 1
});
}
}
trail.push(newPoint);
last = newPoint;
}
// Load initial state
chrome.storage.local.get(['laserEnabled', 'laserColor', 'trailLength'], (result) => {
if (result.laserEnabled) {
const color = parseColor(result.laserColor || '#ff0000');
const fadeRate = calculateFadeRate(result.trailLength || 40);
startLaser({ color, fadeRate });
}
});
// Listen for popup messages (FIX 1: Handle direct messages from popup)
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'PING') {
// Respond to ping to confirm content script is ready
sendResponse({ ready: true });
return true;
} else if (message.type === 'TOGGLE_LASER') {
if (message.enabled) {
chrome.storage.local.get(['laserColor', 'trailLength'], (res) => {
const color = parseColor(res.laserColor || '#ff0000');
const fadeRate = calculateFadeRate(res.trailLength || 40);
startLaser({ color, fadeRate });
});
} else {
stopLaser();
}
sendResponse({ success: true });
} else if (message.type === 'UPDATE_SETTINGS') {
if (isActive) {
const color = parseColor(message.color);
const fadeRate = calculateFadeRate(message.trailLength);
stopLaser();
startLaser({ color, fadeRate });
}
sendResponse({ success: true });
}
return true;
});
// Listen for storage changes (backup method)
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== 'local') return;
if (changes.laserEnabled) {
if (changes.laserEnabled.newValue) {
chrome.storage.local.get(['laserColor', 'trailLength'], (res) => {
const color = parseColor(res.laserColor || '#ff0000');
const fadeRate = calculateFadeRate(res.trailLength || 40);
startLaser({ color, fadeRate });
});
} else {
stopLaser();
}
}
if (changes.laserColor || changes.trailLength) {
if (isActive) {
chrome.storage.local.get(['laserColor', 'trailLength'], (res) => {
const color = parseColor(res.laserColor || '#ff0000');
const fadeRate = calculateFadeRate(res.trailLength || 40);
stopLaser();
startLaser({ color, fadeRate });
});
}
}
});
function parseColor(hex) {
const bigint = parseInt(hex.replace('#', ''), 16);
return {
r: (bigint >> 16) & 255,
g: (bigint >> 8) & 255,
b: bigint & 255,
};
}
function calculateFadeRate(trailLength) {
// Convert trail length to fade rate (longer trail = slower fade)
return 0.8 / trailLength;
}
// FIX 2: Properly clean up when page unloads or extension is removed
window.addEventListener('beforeunload', () => {
stopLaser();
});
// FIX 2: Listen for extension being disabled/removed
chrome.runtime.onSuspend?.addListener(() => {
stopLaser();
});
// FIX 2: Additional cleanup for when extension context is invalidated
const checkExtensionContext = () => {
if (chrome.runtime?.id) {
// Extension is still active
return;
}
// Extension has been removed/disabled, clean up
stopLaser();
};
// Check every few seconds if extension is still active
setInterval(checkExtensionContext, 3000);