Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ecc521
GitHub Repository: ecc521/gimkit-bot
Path: blob/master/ClickIsTrusted-master/contentScript.js
1634 views
1
console.log("ClickIsTrusted: loading contentscript (and here is the debug link).");
2
3
function makeModifiersInteger(e) {
4
let res = 0;
5
if (e.getModifierState("Alt"))
6
res += 1;
7
if (e.getModifierState("Control"))
8
res += 2;
9
if (e.getModifierState("Meta"))
10
res += 4;
11
if (e.getModifierState("Shift"))
12
res += 8;
13
return res;
14
}
15
16
function convertMouseType(type) {
17
if (type.startsWith("mousedown"))
18
return "mousePressed";
19
if (type.startsWith("mouseup"))
20
return "mouseReleased";
21
if (type.startsWith("mousemove"))
22
return "mouseMoved";
23
if (type.startsWith("wheel"))
24
return "mouseWheel";
25
throw new Error(`The ${type} event cannot be replicated by the ClickIsTrusted extension.`);
26
}
27
28
function convertButton(button) {
29
if (button === 0)
30
return "left";
31
if (button === 1)
32
return "middle";
33
if (button === 2)
34
return "right";
35
if (button === 3)
36
return "back";
37
if (button === 4)
38
return "forward";
39
}
40
41
function convertMouseEvent(e) {
42
return {
43
type: convertMouseType(e.type),
44
modifiers: makeModifiersInteger(e),
45
buttons: e.buttons,
46
button: convertButton(e.button),
47
x: e.clientX,
48
y: e.clientY,
49
deltaX: e.deltaX,
50
deltaY: e.deltaY,
51
clickCount: 1, // needed to make composed `click` events
52
// deltaMode: e.deltaMode, //isn't active in the interface.
53
// timeStamp: e.timeStamp //todo include this one??
54
// pointerType: "mouse" || "pen" //todo enable this one??
55
};
56
}
57
58
function convertTouchType(type) {
59
if (type.startsWith("touchstart"))
60
return "touchStart";
61
if (type.startsWith("touchmove"))
62
return "touchMove";
63
if (type.startsWith("touchend"))
64
return "touchEnd";
65
if (type.startsWith("touchcancel"))
66
return "touchcancel";
67
throw new Error(`The ${type} event cannot be replicated by the ClickIsTrusted extension.`);
68
}
69
70
//todo this is untested and probably doesn't work. see eventToObject.js
71
function convertTouchPoint(t) {
72
return {
73
x: t.clientX,
74
y: t.clientY,
75
radiusX: t.radiusX,
76
radiusY: t.radiusY,
77
rotationAngle: t.rotationAngle,
78
force: t.force,
79
id: t.identifier
80
};
81
}
82
83
function convertTouchEvent(e) {
84
return {
85
type: convertTouchType(e.type),
86
modifiers: makeModifiersInteger(e),
87
touchPoints: e.touches.map(convertTouchPoint),
88
// timeStamp: e.timeStamp //todo include this one??
89
}
90
}
91
92
function convertKeyType(type) {
93
if (type === "keydown-is-trusted")
94
return "keyDown";
95
if (type === "keyup-is-trusted")
96
return "keyUp";
97
if (type === "rawkeydown-is-trusted")
98
return "rawKeyDown";
99
if (type === "char-is-trusted")
100
return "char";
101
throw new Error(`The ${e.type} event cannot be replicated by the ClickIsTrusted extension.`);
102
}
103
104
function convertKeyEvent(e) {
105
return {
106
type: convertKeyType(e.type),
107
modifiers: makeModifiersInteger(e),
108
key: e.key,
109
code: e.code,
110
location: e.location,
111
autoRepeat: e.repeat,
112
113
text: e.text,
114
keyIdentifier: e.keyIdentifier,
115
unmodifiedText: e.unmodifiedText,
116
isKeyPad: e.isKeyPad,
117
isSystemKey: e.isSystemKey,
118
nativeVirtualKeyCode: e.nativeVirtualKeyCode,
119
windowsVirtualKeyCode: e.windowsVirtualKeyCode,
120
121
// timeStamp: e.timeStamp //todo include this one??
122
};
123
}
124
125
function convertInputEvent(e) {
126
if (!e.type.startsWith("beforeinput"))
127
throw new Error(`The ${e.type} event cannot be replicated by the ClickIsTrusted extension.`);
128
return {
129
type: e.type,
130
text: e.data,
131
// timeStamp: e.timeStamp //todo include this one??
132
};
133
}
134
135
//att 1. calling chrome.runtime cannot be done from inside the event listener. (a different 'this' context?? don't know).
136
function sendMessage(e) {
137
let message;
138
if (e instanceof MouseEvent)
139
message = convertMouseEvent(e);
140
else if (e instanceof TouchEvent)
141
message = convertTouchEvent(e);
142
else if (e instanceof KeyboardEvent)
143
message = convertKeyEvent(e);
144
else if (e instanceof InputEvent)
145
message = convertInputEvent(e);
146
else
147
throw new Error("a script has tried to send a bad message: ", e);
148
console.log("Passing native event request to background.js: ", message);
149
chrome.runtime.sendMessage(message);
150
}
151
152
function manInTheMiddle(event) {
153
console.log("received native event request from web page:", event);
154
event.stopImmediatePropagation();
155
event.preventDefault();
156
sendMessage(event);
157
}
158
159
window.addEventListener("mousedown-is-trusted", manInTheMiddle);
160
window.addEventListener("mousemove-is-trusted", manInTheMiddle);
161
window.addEventListener("mouseup-is-trusted", manInTheMiddle);
162
window.addEventListener("wheel-is-trusted", manInTheMiddle);
163
window.addEventListener("keydown-is-trusted", manInTheMiddle);
164
window.addEventListener("keyup-is-trusted", manInTheMiddle);
165
window.addEventListener("rawkeydown-is-trusted", manInTheMiddle);
166
window.addEventListener("char-is-trusted", manInTheMiddle);
167
window.addEventListener("beforeinput-is-trusted", manInTheMiddle);
168