-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypress.js
96 lines (86 loc) · 3.12 KB
/
keypress.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
function keypress(app){
const express = require('express')
const {globalShortcut, app: electronApp} = require('electron')
let keys = require('./keypress-keys.json')
const axios = require('axios')
const fs = require('fs')
let webhooks = require('./keypress-webhooks.json')
app.post('/keypress/addWebhook', (req, resp) => {
if(!req.body.url){
resp.status(400)
resp.send('URL not sent.')
return
}else if(!req.body.key){
resp.status(400)
resp.send('Key not sent.')
}
for(item in webhooks.subscribed){
if(webhooks.subscribed[item].url == req.body.url && webhooks.subscribed[item].key == req.body.key){
resp.status(400)
resp.send('Webhook already exists.')
return
}
}
webhooks.subscribed.push({
url: req.body.url,
key: req.body.key
})
if(!keys.subscribed.includes(req.body.key)){
keys.subscribed.push(
req.body.key
)
fs.writeFileSync('keypress-keys.json', JSON.stringify(keys, null, 4))
globalShortcut.register(req.body.key, onKeyPress(req.body.key))
}
fs.writeFileSync('keypress-webhooks.json', JSON.stringify(webhooks, null, 4))
resp.send('Webhook set!')
})
app.post('/keypress/deleteWebhook', (req, resp) => {
if(!req.body.url){
resp.status(400)
resp.send('URL not sent.')
return
}else if(!req.body.key){
resp.status(400)
resp.send('Key not sent.')
return
}
for(item in webhooks.subscribed){
if(webhooks.subscribed[item].url == req.body.url && webhooks.subscribed[item].key == req.body.key){
webhooks.subscribed.splice(item, 1)
fs.writeFileSync('keypress-webhooks.json', JSON.stringify(webhooks, null, 4))
resp.send('Webhook removed!')
for(microItem in webhooks.subscribed){
if(webhooks.subscribed[microItem].key == req.body.key){
return
}
}
keys.subscribed.splice(keys.subscribed.indexOf(req.body.key))
fs.writeFileSync('keypress-keys.json', JSON.stringify(keys, null, 4))
globalShortcut.unregister(req.body.key)
return
}
}
resp.status(400)
resp.send('Webhook not found.')
reload()
})
electronApp.whenReady().then(() => {
for(item in keys.subscribed){
globalShortcut.register(keys.subscribed[item], onKeyPress(keys.subscribed[item]))
}
})
function onKeyPress(item){
return () => {
for(microItem in webhooks.subscribed){
if(webhooks.subscribed[microItem].key == item){
axios.post(webhooks.subscribed[microItem].url, {key: item})
}
}
}
}
return new Promise((resolve, reject) => {
resolve()
})
}
module.exports = keypress