-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
68 lines (62 loc) · 1.74 KB
/
Copy pathcontentScript.js
File metadata and controls
68 lines (62 loc) · 1.74 KB
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
let clickedElement;
let interval;
document.addEventListener(
"mousedown",
event => {
// only handle right click
if (event.button !== 2) {
return false;
}
if (event.target === clickedElement && interval) {
chrome.extension.sendMessage({
command: 'updateContextMenu',
isTyping: true
});
} else {
chrome.extension.sendMessage({
command: 'updateContextMenu',
isTyping: false
});
}
if (clickedElement !== event.target) {
clickedElement = event.target;
const onBlur = () => {
clearInterval(interval);
interval = null;
if (clickedElement) {
clickedElement.removeEventListener("blur", onBlur);
clickedElement = null;
}
};
clickedElement.addEventListener("blur", onBlur);
}
},
true
);
document.addEventListener(
"keydown",
event => {
// stop script on escape
if (event.key === 'Escape' || event.keyCode === 27) {
clearInterval(interval);
}
},
true
);
chrome.runtime.onMessage.addListener(request => {
if (request.command === "contextMenuClicked") {
if (request.isTyping) {
// stop typing
clearInterval(interval);
interval = null;
} else {
writeOnElement(clickedElement, request.state);
}
}
if (request.command === 'stateUpdate') {
// Only update the wpm if the script is already typing
if (interval) {
writeOnElement(clickedElement, request.state);
}
}
});