-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleDocsWpm.js
More file actions
80 lines (70 loc) · 2.27 KB
/
googleDocsWpm.js
File metadata and controls
80 lines (70 loc) · 2.27 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
69
70
71
72
73
74
75
76
77
78
79
80
import { BaseTracker } from './baseTracker.js'
export class GoogleDocsTracker extends BaseTracker {
constructor(popup, settingsManager) {
super(popup, settingsManager)
this.pollAttempts = 0
this.maxPollAttempts = 20
this.iframeDoc = null
this.waitForEditor()
}
waitForEditor() {
const iframe = document.querySelector('.docs-texteventtarget-iframe')
if (iframe && iframe.contentDocument) {
this.initializeEventListeners(iframe.contentDocument)
} else {
this.pollAttempts++
if (this.pollAttempts < this.maxPollAttempts) {
setTimeout(() => this.waitForEditor(), 500)
} else {
console.warn('TypeTrack: Could not find Google Docs editor after 10 seconds')
}
}
}
initializeEventListeners(iframeDoc) {
// Store reference to prevent duplicate listeners
if (this.iframeDoc === iframeDoc) return
this.iframeDoc = iframeDoc
// Listen on the iframe's document for keypress events
iframeDoc.addEventListener('keypress', (e) => this.handleKeyPress(e), { passive: true })
}
handleKeyPress(e) {
if (!this.settingsManager.currentSettings.extensionEnabled) return
if (e.key.length !== 1) return
this.updateWPMWithCursor()
}
updateWPMWithCursor() {
const cursor = document.querySelector('.kix-cursor-caret')
this.updateWPM(cursor)
}
positionAtTextCursor(targetRect) {
if (targetRect) {
this.popup.style.left = `${targetRect.left + 10}px`
this.popup.style.top = `${targetRect.top - 30}px`
} else {
this.popup.style.right = "10px"
this.popup.style.top = "60px"
}
}
positionAtMouse(targetRect) {
// For Google Docs, mouse position isn't tracked, so fall back to text cursor
this.positionAtTextCursor(targetRect)
}
positionAbove(targetRect) {
if (targetRect) {
this.popup.style.left = `${targetRect.left}px`
this.popup.style.top = `${targetRect.top - 40}px`
} else {
this.popup.style.right = "10px"
this.popup.style.top = "60px"
}
}
positionBelow(targetRect) {
if (targetRect) {
this.popup.style.left = `${targetRect.left}px`
this.popup.style.top = `${targetRect.bottom + 20}px`
} else {
this.popup.style.right = "10px"
this.popup.style.bottom = "10px"
}
}
}