-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
509 lines (436 loc) · 14.9 KB
/
content.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Bodies = Matter.Bodies,
Composite = Matter.Composite;
Mouse = Matter.MouseConstraint;
MouseConstraint = Matter.MouseConstraint;
// create an engine
var engine = Engine.create();
// Create a render instance
const render = Render.create({
element: document.body,
engine: engine,
options: {
width: window.innerWidth,
height: window.innerHeight,
wireframes: false,
background: "transparent",
},
});
// Apply custom styles to the canvas
const canvas = render.canvas;
canvas.style.position = "fixed";
canvas.style.top = "0";
canvas.style.left = "0";
canvas.style.zIndex = "999999";
canvas.style.pointerEvents = "none";
// Configuration for radius and image scale
const cookieConfig = {
S: { radius: 50, scale: 0.3 },
M: { radius: 60, scale: 0.5 },
L: { radius: 70, scale: 0.8 },
XL: { radius: 150, scale: 1 },
};
const cookieTextures = {
full: "images/cookie.png",
bite1: "images/cookiebite1.png",
bite1Selected: "images/cookiebite1-selected.png",
bite2: "images/cookiebite2.png",
bite2Selected: "images/cookiebite2-selected.png",
};
function explodeCookie(cookie) {
const forceMagnitude = 1.2; // Adjust force for explosion
const randomAngle = Math.random() * 2 * Math.PI;
// Apply force to the original cookie and remove it from the world
Matter.Body.applyForce(cookie, cookie.position, {
x: forceMagnitude * Math.cos(randomAngle),
y: forceMagnitude * Math.sin(randomAngle),
});
// Remove the label div from the DOM
if (cookie.labelId) {
const labelElement = document.getElementById(cookie.labelId);
if (labelElement) {
document.body.removeChild(labelElement);
}
}
Composite.remove(engine.world, cookie);
setTimeout(() => {
// Create crumbs
for (let i = 0; i < 8; i++) {
createCrumb(cookie.position.x, cookie.position.y, cookie.size);
}
// Transform the cookie to the next state
if (
cookie.render.sprite.texture ===
chrome.runtime.getURL(cookieTextures.full)
) {
createTransformedCookie(
cookie.position.x,
cookie.position.y,
cookie.size,
"bite1",
cookie.cookieName
);
} else if (
cookie.render.sprite.texture ===
chrome.runtime.getURL(cookieTextures.bite1)
) {
createTransformedCookie(
cookie.position.x,
cookie.position.y,
cookie.size,
"bite2",
cookie.cookieName
);
} else if (
cookie.render.sprite.texture ===
chrome.runtime.getURL(cookieTextures.bite2)
) {
for (let i = 0; i < 3; i++) {
createCrumb(cookie.position.x, cookie.position.y, cookie.size);
}
}
}, 200);
}
function createTransformedCookie(x, y, size, state, name) {
const config = cookieConfig[size] || cookieConfig.M; // Default to M size if size is unknown
// Define the maximum number of characters for the label
const maxLabelCharacters = 10; // Adjust this value based on your design preferences
const truncatedName =
name.length > maxLabelCharacters
? name.substring(0, maxLabelCharacters) + "..."
: name;
// Create the transformed cookie
var transformedCookie = Bodies.circle(x, y, config.radius, {
restitution: 1,
render: {
sprite: {
texture: chrome.runtime.getURL(cookieTextures[state]),
xScale: config.scale,
yScale: config.scale,
},
},
});
transformedCookie.size = size; // Store size in the cookie for future transformations
transformedCookie.cookieName = name; // Assign the same name as the original cookie
// Create a new label for the transformed cookie
const label = document.createElement("div");
label.innerText = truncatedName; // Use truncated name
label.style.position = "absolute";
label.style.color = "#fff";
label.style.fontSize = "16px";
label.style.fontWeight = "bold";
label.style.textAlign = "center";
label.style.pointerEvents = "none";
label.style.transformOrigin = "center center";
label.style.zIndex = zIndexCounter.toString(); // Set z-index for the label
label.style.backgroundColor = "black"; // Black background
label.style.borderRadius = "10px"; // Corner radius
label.style.padding = "5px 10px"; // Padding inside the label
label.style.maxWidth = "150px"; // Set a maximum width for the label
label.style.whiteSpace = "nowrap"; // Prevent wrapping
label.style.overflow = "hidden"; // Hide overflow text
Matter.Events.on(engine, "afterUpdate", () => {
const posX = cookie.position.x;
const posY = cookie.position.y;
// Set label's position relative to the canvas, compensating for its size
label.style.left = `${posX}px`;
label.style.top = `${posY}px`;
label.style.transform = `translate(-50%, -50%) rotate(${cookie.angle}rad)`;
});
// Increment the global z-index counter for the next cookie
zIndexCounter++;
Composite.add(engine.world, transformedCookie);
}
function getCookieSizeCategory(totalCharacters) {
console.log(`Debug: Total characters = ${totalCharacters}`);
if (totalCharacters < 100) {
console.log("Debug: Size category = S");
return "S";
}
if (totalCharacters < 300) {
console.log("Debug: Size category = M");
return "M";
}
if (totalCharacters < 500) {
console.log("Debug: Size category = L");
return "L";
}
console.log("Debug: Size category = XL");
return "XL";
}
// Global z-index counter to ensure each new cookie is on a different layer
let zIndexCounter = 1000000; // Starting z-index value
function createCookie(x, y, size, name) {
console.log("Creating Cookie of size:", size);
const config = cookieConfig[size] || cookieConfig.M; // Default to M size if size is unknown
// Define the maximum number of characters for the label
const maxLabelCharacters = 10; // Adjust this value based on your design preferences
const truncatedName =
name.length > maxLabelCharacters
? name.substring(0, maxLabelCharacters) + "..."
: name;
// Create the Matter.js body for the cookie
var cookie = Bodies.circle(x, y, config.radius, {
restitution: 1,
render: {
sprite: {
texture: chrome.runtime.getURL("images/cookie.png"),
xScale: config.scale,
yScale: config.scale,
},
},
});
cookie.size = size; // Store size in the cookie for future transformations
cookie.cookieName = name; // Store the name for display
// Create a label element for the cookie name
const label = document.createElement("div");
const state = "full"; // Initial state is "full"
label.id = `cookie-label-${name}-${state}`; // Assign a unique id based on cookie name and state
label.innerText = truncatedName; // Use truncated name
label.style.position = "absolute";
label.style.color = "#fff";
label.style.fontSize = "16px";
label.style.fontWeight = "bold";
label.style.textAlign = "center";
label.style.pointerEvents = "none";
label.style.transformOrigin = "center center";
label.style.zIndex = zIndexCounter.toString(); // Set z-index for the label
label.style.backgroundColor = "black"; // Black background
label.style.borderRadius = "10px"; // Corner radius
label.style.padding = "5px 10px"; // Padding inside the label
label.style.maxWidth = "150px"; // Set a maximum width for the label
label.style.whiteSpace = "nowrap"; // Prevent wrapping
label.style.overflow = "hidden"; // Hide overflow text
Matter.Events.on(engine, "afterUpdate", () => {
const posX = cookie.position.x;
const posY = cookie.position.y;
// Set label's position relative to the canvas, compensating for its size
label.style.left = `${posX}px`;
label.style.top = `${posY}px`;
label.style.transform = `translate(-50%, -50%) rotate(${cookie.angle}rad)`;
});
// Store the label element ID in the cookie object for future reference
cookie.labelId = label.id;
// Increment the global z-index counter for the next cookie
zIndexCounter++;
Composite.add(engine.world, cookie);
}
function createCrumb(x, y, size) {
console.log(`images/crumbs/crumb${Math.floor(Math.random() * 6) + 1}.png`);
var crumb = Bodies.circle(x, y, 20, {
restitution: 0.2,
render: {
sprite: {
texture: chrome.runtime.getURL(
`images/crumbs/crumb${Math.floor(Math.random() * 6) + 1}.png`
),
xScale: 1.5,
yScale: 1.5,
},
},
// Add a custom property to mark this body as a crumb
isCrumb: true,
});
// // Add click event to trigger explodeCookie when clicked
// cookie.render.sprite.clickHandler = () => explodeCookie(cookie);
Composite.add(engine.world, crumb);
const forceMagnitude = 0.2; // Adjust force for explosion
const randomAngle = Math.random() * 2 * Math.PI;
Matter.Body.applyForce(crumb, crumb.position, {
x: forceMagnitude * Math.cos(randomAngle),
y: forceMagnitude * Math.sin(randomAngle),
});
}
function preventClickThrough() {
document.addEventListener(
"click",
(event) => {
if (selectedCookie) {
// Prevent default browser behaviour
event.preventDefault();
// Stop the event from propagating to other elements
event.stopPropagation();
console.log(
"Click prevented due to selected rigid body:",
selectedCookie
);
}
},
true
); // Use the capture phase to intercept clicks before they reach other elements
}
function createCursorBody() {
const cursorBody = Bodies.circle(0, 0, 50, {
isStatic: true,
density: 100, // Adjust for desired mass
restitution: 1, // Bounciness
frictionAir: 0.05, // Simulates air drag for smoother control
render: {
fillStyle: "rgba(0, 0, 0, 0)", // Visual style (transparent)
},
});
Composite.add(engine.world, cursorBody);
document.addEventListener("mousemove", (event) => {
const mouseX = event.clientX;
const mouseY = event.clientY;
// Set cursor body position to mouse position
Matter.Body.setPosition(cursorBody, { x: mouseX, y: mouseY });
});
let selectedCookie = null;
// Handle hover (collisionStart)
Matter.Events.on(engine, "collisionStart", (event) => {
event.pairs.forEach((pair) => {
if (pair.bodyA === cursorBody || pair.bodyB === cursorBody) {
const cookieBody = pair.bodyA === cursorBody ? pair.bodyB : pair.bodyA;
// If the body is a crumb, do nothing
if (cookieBody.isCrumb) return;
// Change texture to selected on hover
if (cookieBody.render && cookieBody.render.sprite) {
if (
cookieBody.render.sprite.texture ===
chrome.runtime.getURL(cookieTextures.bite1)
) {
cookieBody.render.sprite.texture = chrome.runtime.getURL(
cookieTextures.bite1Selected
);
} else if (
cookieBody.render.sprite.texture ===
chrome.runtime.getURL(cookieTextures.bite2)
) {
cookieBody.render.sprite.texture = chrome.runtime.getURL(
cookieTextures.bite2Selected
);
} else {
cookieBody.render.sprite.texture = chrome.runtime.getURL(
"images/cookie-selected.png"
);
}
}
selectedCookie = cookieBody;
}
});
});
// Handle hover out (collisionEnd)
Matter.Events.on(engine, "collisionEnd", (event) => {
event.pairs.forEach((pair) => {
if (pair.bodyA === cursorBody || pair.bodyB === cursorBody) {
const cookieBody = pair.bodyA === cursorBody ? pair.bodyB : pair.bodyA;
// If the body is a crumb, do nothing
if (cookieBody.isCrumb) return;
// Reset texture to default when hover ends
if (cookieBody.render && cookieBody.render.sprite) {
if (
cookieBody.render.sprite.texture ===
chrome.runtime.getURL(cookieTextures.bite1Selected)
) {
cookieBody.render.sprite.texture = chrome.runtime.getURL(
cookieTextures.bite1
);
} else if (
cookieBody.render.sprite.texture ===
chrome.runtime.getURL(cookieTextures.bite2Selected)
) {
cookieBody.render.sprite.texture = chrome.runtime.getURL(
cookieTextures.bite2
);
} else {
cookieBody.render.sprite.texture =
chrome.runtime.getURL("images/cookie.png");
}
}
selectedCookie = null;
}
});
});
// Handle click to explode
document.addEventListener("click", () => {
if (selectedCookie) {
// Explode the cookie if it's not a crumb
if (!selectedCookie.isCrumb) {
explodeCookie(selectedCookie);
}
selectedCookie = null; // Clear the selection
}
});
}
function createBoundaries() {
const thickness = 50;
// Bottom boundary
var ground = Bodies.rectangle(
window.innerWidth / 2,
window.innerHeight + thickness / 2,
window.innerWidth,
thickness,
{ isStatic: true }
);
// Top boundary
var ceiling = Bodies.rectangle(
window.innerWidth / 2,
-thickness / 2,
window.innerWidth,
thickness,
{ isStatic: true }
);
// Left boundary
var leftWall = Bodies.rectangle(
-thickness / 2,
window.innerHeight / 2,
thickness,
window.innerHeight,
{ isStatic: true }
);
// Right boundary
var rightWall = Bodies.rectangle(
window.innerWidth + thickness / 2,
window.innerHeight / 2,
thickness,
window.innerHeight,
{ isStatic: true }
);
// Add all boundaries to the world
Composite.add(engine.world, [ground, ceiling, leftWall, rightWall]);
}
chrome.runtime.sendMessage(
{ action: "getCookiesForCurrentDomain" },
(response) => {
if (response && response.success) {
console.log(
"Cookies for the current domain:",
response.domain,
response.cookies
);
for (let i = 0; i < response.cookies.length; i++) {
const cookie = response.cookies[i];
// Calculate total characters in all values of the cookie
const totalCharacters = Object.values(cookie)
.filter((value) => typeof value === "string") // Ensure values are strings
.reduce((sum, value) => sum + value.length, 0);
console.log("Debug: Total characters =", totalCharacters);
// Categorise based on total characters
const sizeCategory = getCookieSizeCategory(totalCharacters);
console.log("Debug: Size category =", sizeCategory);
// Create a cookie with the corresponding size
createCookie(
Math.random() * window.innerWidth,
Math.random() * window.innerHeight,
sizeCategory,
cookie.name
);
}
} else {
console.error("Failed to retrieve cookies.");
}
}
);
// Prevent click-through functionality
preventClickThrough();
createBoundaries();
createCursorBody();
// run the renderer
Render.run(render);
// create runner
var runner = Runner.create();
// run the engine
Runner.run(runner, engine);