-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
48 lines (42 loc) · 1.7 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
const evilButton = document.getElementById('evil-button')
const OFFSET = 100
evilButton.addEventListener('click', () => {
alert('Nice Try')
window.close()
})
document.addEventListener('mousemove', (e) => {
const x = e.pageX
const y = e.pageY
const buttonBox = evilButton.getBoundingClientRect()
const horizontalDistanceFrom = distanceFromCenter(buttonBox.x, x, buttonBox.width)
const verticalDistanceFrom = distanceFromCenter(buttonBox.y, y, buttonBox.height)
const horizontalOffset = buttonBox.width / 2 + OFFSET
const verticalOffset = buttonBox.height / 2 + OFFSET
if (Math.abs(horizontalDistanceFrom) <= horizontalOffset && Math.abs(verticalDistanceFrom) <= verticalOffset) {
setButtonPosition(
buttonBox.x + horizontalOffset / horizontalDistanceFrom * 10,
buttonBox.y + verticalOffset / verticalDistanceFrom * 10
)
}
})
function setButtonPosition(left, top) {
const windowBox = document.body.getBoundingClientRect()
const buttonBox = evilButton.getBoundingClientRect()
if(distanceFromCenter(left, windowBox.left, buttonBox.width) < 0) {
left = windowBox.right - buttonBox.width - OFFSET
}
if(distanceFromCenter(left, windowBox.right, buttonBox.width) > 0) {
left = windowBox.left + OFFSET
}
if(distanceFromCenter(top, windowBox.top, buttonBox.height) < 0) {
top = windowBox.bottom - buttonBox.height - OFFSET
}
if(distanceFromCenter(top, windowBox.bottom, buttonBox.height) > 0) {
top = windowBox.top + OFFSET
}
evilButton.style.left = `${left}px`
evilButton.style.top = `${top}px`
}
function distanceFromCenter(boxPosition, mousePosition, boxSize) {
return boxPosition - mousePosition + boxSize / 2
}