-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathglobal.html
89 lines (78 loc) · 3.28 KB
/
global.html
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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" charset="utf-8">
// generic cross-origin AJAX function
function ajax(method, url, data, cb, headers){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = readystatechange;
xhr.open(method, url);
xhr.setRequestHeader("Content-Type", "application/json");
if (headers){
for(i in headers){
xhr.setRequestHeader(i, headers[i]);
}
}
if (data){
data = JSON.stringify(data);
}
xhr.send(data);
function readystatechange(){
if(this.readyState === this.DONE) {
if (this.status == 200){
if(this.getResponseHeader("Content-Type").split(";")[0] === "application/json"){
return cb(null, JSON.parse(this.response), this);
}else{
return cb(null, this.response, this);
}
}else{
return cb(this.status, this.response, this);
}
}
}
}
// pass console.log() to current window/tab end-script
var console = {log: function(){
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("log", arguments);
}};
var settings={};
// global settings
function settingsChanged(){
settings = {
client: safari.extension.settings.getItem('client'),
host: safari.extension.settings.getItem('host'),
port: safari.extension.settings.getItem('port'),
username: safari.extension.secureSettings.getItem('username'),
password: safari.extension.secureSettings.getItem('password')
};
}
safari.extension.settings.addEventListener("change", settingsChanged, false);
settingsChanged();
// callbacks, keyed by client
var add = {};
add["Transmission WebUI"] = function(torrent){
var url = 'http://' + settings.username + ':' + settings.password + '@' + settings.host + ':' + settings.port + '/transmission/rpc';
ajax('POST', url, {"method": "torrent-add", arguments:{"filename": torrent}}, function(err, res, req){
if (err){
if (err == 409){
add["Transmission WebUI"].token = req.getResponseHeader('X-Transmission-Session-Id');
return add["Transmission WebUI"](torrent);
}else{
console.log('unknown err', err);
}
}
}, {'X-Transmission-Session-Id': add["Transmission WebUI"].token});
};
safari.application.addEventListener("message",function(msg){
switch(msg.name){
case 'addTorrent':
add[settings.client](msg.message);
break;
default:
return console.log("unhandled global message: " + msg.name, msg);
}
}, false);
</script>
</head>
<body></body>
</html>