-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetr.js
97 lines (73 loc) · 2.41 KB
/
petr.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
function random(min, max) {
return Math.floor(min + Math.random() * (max - min + 1));
}
function setStyle(element, style) {
for (var s in style) {
element.style[s] = style[s];
}
}
function removeElement(el) {
el.parentNode.removeChild(el);
}
const shakeSpeed = 40;
const shakePositionInterval = 250;
const appVersion = navigator.appVersion;
var cursor = null,
posInterval = null,
cursorPosX = 0,
cursorPosY = 0,
viewportPosX = 0,
viewportPosY = 0,
offsetX = 0,
offsetY = 0,
css = null,
clickedElement = null;
function mousemoveHandler(e) {
// Save the position of the fake cursor
cursorPosX = e.pageX + offsetX;
cursorPosY = e.pageY + offsetY;
// Save the viewport position of the fake cursor (position without scroll)
// We use this later to get the clicked element
viewportPosX = e.clientX + offsetX;
viewportPosY = e.clientY + offsetY;
setStyle(cursor, { left: cursorPosX + "px", top: cursorPosY + "px" });
}
function elementClickHandler(e) {
if (e.target === clickedElement) {
// Actual mouse clicked element is the same as the element that the fake cursor would click.
// This is because we triggered the click or that the positions of the mouse and the fake cursor are both over the same element.
// Do nothing and pass on the click. Reset the clicked element.
clickedElement = null;
} else {
// Actual mouse clicked element is NOT the same as the element that the fake cursor would click.
// Get the element that the fake cursor would click and trigger click on that element.
e.preventDefault();
clickedElement = document.elementFromPoint(viewportPosX, viewportPosY);
if (clickedElement) {
clickedElement.click();
}
}
}
function setOffset() {
offsetX = random(-shakeSpeed, shakeSpeed);
offsetY = random(-shakeSpeed, shakeSpeed);
}
function start() {
const cursorType = appVersion.includes("Windows") ? "windows" : "mac";
cursor = document.createElement("div");
cursor.setAttribute("id", "wds-parkinsonsCursor");
cursor.setAttribute("class", cursorType);
document.body.appendChild(cursor);
document.addEventListener("mousemove", mousemoveHandler);
document.addEventListener("click", elementClickHandler);
posInterval = setInterval(setOffset, shakePositionInterval);
}
function stop() {
if(cursor) {
removeElement(cursor);
}
if(posInterval) {
clearInterval(posInterval);
}
// TODO: remove listeners
}