-
Notifications
You must be signed in to change notification settings - Fork 72
/
SyncAutoxJs.js
92 lines (82 loc) · 2.36 KB
/
SyncAutoxJs.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
// START User Config
const url = 'http://192.168.5.194:5033'
const username = 'admin'
const token = 'admin'
const intervalTime = 3 * 1000 // 3 seconds
const showToastNotification = true
// END User Config
const axios = require('axios');
const authHeader = 'basic ' + $base64.encode(`${username}:${token}`)
let urlWithoutSlash = url
while (urlWithoutSlash.endsWith('/'))
urlWithoutSlash = url.substring(0, url.length - 1)
const apiUrl = urlWithoutSlash + '/SyncClipboard.json'
let running = false
let remoteCache;
function loop() {
if (!device.isScreenOn())
return
if (running)
return
running = true
upload()
.then(ifContinue => {
if (ifContinue)
download()
})
.then(() => { running = false })
.catch(error => {
running = false
toast('Failed: \n' + error)
});
}
function download() {
axios({
method: 'get',
url: apiUrl,
responseType: 'json',
headers: { 'authorization': authHeader },
})
.then(res => {
if (res.status < 200 || res.status >= 300) {
throw res.status + ' ' + res.statusText
} else {
var profile = res.data;
if (profile.Type != 'Text')
return
var text = profile.Clipboard
if (text != remoteCache) {
remoteCache = text
setClip(text)
if(showToastNotification)
toast('Cipboard Setted\n' + text)
}
}
})
}
function upload() {
let text = getClip()
if (text != remoteCache && text != null && text.length != 0) {
return axios({
method: 'put',
url: apiUrl,
headers: {
'authorization': authHeader,
'Content-Type': 'application/json',
},
data: {
'File': '',
'Clipboard': text,
'Type': 'Text'
}
}).then(res => {
if (res.status < 200 || res.status >= 300) {
throw res.status + ' ' + res.statusText
}
remoteCache = text
return false
})
}
return Promise.resolve(true);
}
setInterval(loop, intervalTime)