-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
55 lines (52 loc) · 1.79 KB
/
main.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
const app = {
cht: null,
async inject(callback, args = []) {
let permissionDesc = { active: true, currentWindow: true }
let [tab] = await chrome.tabs.query(permissionDesc)
const { result } = (
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: callback,
args,
})
).find(frameResult => frameResult.frameId === 0)
return result
},
clickListener({ cps, targetKey }) {
return async ev =>
(this.cht =
(await this.inject(
...(ev.currentTarget.checked
? [
(cps, key) => {
return setInterval(() => {
document.dispatchEvent(
new KeyboardEvent('keydown', {
key,
}),
)
}, 1000 / cps)
},
[cps.value, targetKey.value || 'a'],
]
: [_ => clearInterval(_), [this.cht || 0]]),
)) || this.cht)
},
start(ctx) {
ctx.checkbox.addEventListener('change', this.clickListener(ctx))
},
}
app.start({
checkbox: document.getElementById('onoff'),
cps: document.getElementById('cps'),
targetKey: document.getElementById('targetKey'),
})
;(() =>
document
.getElementById('cps')
.addEventListener(
'input',
ev =>
(document.getElementById('per').innerText =
ev.currentTarget.value),
))()