-
Notifications
You must be signed in to change notification settings - Fork 0
/
synology.js
104 lines (99 loc) · 2.34 KB
/
synology.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
97
98
99
100
101
102
103
104
var sendToMySyno = function (urls) {
console.log("get options stored.");
chrome.storage.sync.get(
{
hostname: "",
username: "",
password: "",
},
function (r) {
if (r.hostname == "" || r.username == "" || r.password == "") {
toastr["info"](
"Please fill in your synology info first at the options page!"
);
return;
}
getApiInfo(r.hostname, (result) => {
auth(
r.hostname,
result["data"]["SYNO.API.Auth"],
r.username,
r.password,
(sid) => {
download(
r.hostname,
result["data"]["SYNO.DownloadStation.Task"],
urls,
r.username,
r.password,
sid
);
}
);
});
}
);
};
var getApiInfo = function (host, callback) {
var synoApiInfoUrl =
host +
"/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth,SYNO.DownloadStation.Task";
fetch(synoApiInfoUrl)
.then((r) => r.text())
.then((result) => {
obj = JSON.parse(result);
console.log(obj);
if (callback) {
callback(obj);
}
});
};
var auth = function (host, authInfo, account, password, callback) {
var authUrl =
host +
"/webapi/" +
authInfo.path +
"?api=SYNO.API.Auth&version=" +
authInfo.maxVersion +
"&method=login&account=" +
account +
"&passwd=" +
password +
"&session=DownloadStation&format=cookie";
fetch(authUrl)
.then((r) => r.text())
.then((result) => {
obj = JSON.parse(result);
console.log(obj);
if (callback) {
callback("auth", obj.data.sid);
}
});
};
var download = function (host, downloadInfo, urls, account, password, sid) {
var url = host + "/webapi/" + downloadInfo.path;
var data =
"api=SYNO.DownloadStation.Task" +
"&version=" +
downloadInfo.maxVersion +
"&method=create&uri=" +
urls +
"&username=" +
account +
"&password=" +
password +
"&sid=" +
sid;
$.ajax({
type: "POST",
url: url,
data: data,
success: (result) => {
console.log("download", result);
obj = JSON.parse(result);
if (result.success == true) {
toastr["info"]("Successfully send to your synology DS!");
}
},
});
};