-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (60 loc) · 1.91 KB
/
script.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
const evilButton = document.querySelector("#evil-button");
const OFFSET = 100;
evilButton.addEventListener("click", (e) => {
alert("Nice try!");
window.close();
});
document.addEventListener("mousemove", (e) => {
const { pageX: x, pageY: y } = e;
const {
x: buttonX,
y: buttonY,
width: buttonWidth,
height: buttonHeight,
} = evilButton.getBoundingClientRect();
const horizontalDistanceFrom = distanceFromCenter(buttonX, x, buttonWidth);
const verticalDistanceFrom = distanceFromCenter(buttonY, y, buttonHeight);
const horizontalOffset = buttonWidth / 2 + OFFSET;
const verticalOffset = buttonHeight / 2 + OFFSET;
if (
Math.abs(horizontalDistanceFrom) <= horizontalOffset &&
Math.abs(verticalDistanceFrom) <= verticalOffset
) {
setButtonPosition(
buttonX + (horizontalOffset / horizontalDistanceFrom) * 3,
buttonY + (verticalOffset / verticalDistanceFrom) * 3
);
}
});
function setButtonPosition(left, top) {
const {
left: windowLeft,
right: windowRight,
top: windowTop,
bottom: windowBottom,
} = document.body.getBoundingClientRect();
const {
x: buttonX,
y: buttonY,
width: buttonWidth,
height: buttonHeight,
} = evilButton.getBoundingClientRect();
if (distanceFromCenter(left, windowLeft, buttonWidth) < 0) {
left = windowRight - buttonWidth - OFFSET;
}
console.log(distanceFromCenter(windowRight, left, buttonWidth));
if (distanceFromCenter(top, windowTop, buttonHeight) < 0) {
top = windowBottom - buttonHeight - OFFSET;
}
if (distanceFromCenter(left, windowRight, buttonWidth) > 0) {
left = windowLeft + OFFSET;
}
if (distanceFromCenter(top, windowBottom, buttonHeight) > 0) {
top = windowTop + OFFSET;
}
evilButton.style.top = `${top}px`;
evilButton.style.left = `${left}px`;
}
function distanceFromCenter(boxPosition, mousePosition, boxSize) {
return boxPosition - mousePosition + boxSize / 2;
}