This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackground.html
198 lines (174 loc) · 7.14 KB
/
background.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE HTML>
<html>
<head>
<title>KodiPlay</title>
<script type="text/javascript" src="javascript/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="javascript/modules.js"></script>
<script type="text/javascript">
function getPlaylistId(type, callback) {
var getPlaylistId = '{"jsonrpc": "2.0", "method": "Playlist.GetPlaylists", "id": 1}';
ajaxPost(getPlaylistId, function (response) {
if (response && response.result.length > 0) {
var playlists = response.result;
for (var i = 0; i < playlists.length; i++) {
if (playlists[i].type == type) {
callback(playlists[i].playlistid);
}
}
} else {
callback(null);
}
});
}
function clearPlaylist(callback) {
var clearVideoPlaylist = '{"jsonrpc": "2.0", "method": "Playlist.Clear", "params":{"playlistid":1}, "id": 1}';
var clearAudioPlaylist = '{"jsonrpc": "2.0", "method": "Playlist.Clear", "params":{"playlistid":0}, "id": 1}';
ajaxPost(clearVideoPlaylist, function () {
ajaxPost(clearAudioPlaylist, function (response) {
callback(response);
});
});
}
function getActivePlayerId(callback) {
var getActivePlayers = '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}';
ajaxPost(getActivePlayers, function (response) {
if (response && response.result.length > 0) {
var playerid = response.result[0].playerid;
var type = response.result[0].type;
callback(playerid, type);
} else {
callback(null, null);
}
});
}
function getURL() {
var url;
var port;
var username;
var password;
url = safari.extension.settings.host;
port = safari.extension.settings.port;
username = safari.extension.secureSettings.username;
password = safari.extension.secureSettings.password;
var loginPortion = '';
if (username && password) {
loginPortion = username + ':' + password + '@';
}
return 'http://'+ loginPortion + url + ':' + port;
}
function ajaxPost(data, callback, timeout) {
var url = getURL();
var fullPath = url + "/jsonrpc";
var defaultTimeout = 5000;
if (timeout) {
defaultTimeout = timeout;
}
console.log("POST " + data);
jQuery.ajax({
type: 'POST',
url: fullPath,
success: function (response) {
console.log(response);
callback(response);
},
contentType: "application/json",
data: data,
dataType: 'json',
timeout: defaultTimeout,
error: function (jqXHR, textStatus, erroThrown) {
callback(0);
}
});
}
function addItemsToPlaylist(items, callback) {
if (!items || items.length <= 0) {
callback(null);
return;
}
// assuming all of the same type
var contentType = items[0].contentType;
if (contentType != 'picture') {
getPlaylistId(contentType, function (playlistId) {
getActivePlayerId(function (playerId) {
//if nothing is playing, clear the playlist
if (playerId == null) {
clearPlaylist(function() {});
}
var addToPlaylist = "[";
for (var i = 0; i < items.length; i++) {
if (i > 0) {
addToPlaylist += ",";
}
addToPlaylist += '{"jsonrpc": "2.0", "method": "Playlist.Add", "params":{"playlistid":' + playlistId + ', "item" :{ "file" : "' + items[i].pluginPath + '" }}, "id" :' + (i + 1) + '}';
}
addToPlaylist += "]";
ajaxPost(addToPlaylist, function (response) {
var playVideo = '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"playlistid":' + playlistId + ', "position" : 0}}, "id": 1}';
//if nothing is playing, play what we inserted
if (playerId == null) {
ajaxPost(playVideo, function (response_2) {
callback(response_2);
}, 10000);
} else {
callback(response);
}
});
});
});
} else {
var playImage = '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{ "file" : "' + items[0].pluginPath + '" }}, "id": 1}';
ajaxPost(playImage, function () {});
}
}
function getPluginPath(url, callback) {
for (var i = 0; i < allModules.length; i++) {
var module = allModules[i];
if (module.canHandleUrl(url)) {
module.getPluginPath(url, function(path) {
callback(module.getMediaType(), path);
});
}
}
}
function queueItem(url, callback) {
getPluginPath(url, function (contentType, pluginPath) {
addItemsToPlaylist([
{"contentType": contentType, "pluginPath": pluginPath}
], function (result) {
callback(result);
});
});
}
function canPlayUrl(url) {
for (var i = 0; i < allModules.length; i++) {
var module = allModules[i];
if (module.canHandleUrl(url)) {
return true;
}
}
return false;
}
function playUrl(url) {
if (safari.extension.settings.host) {
queueItem(url, function(result) {
if (result == 0) {
alert("Error attempting to play item: " + url);
}
});
} else {
alert("Please configure KodiPlay settings in Preferences > Extensions > KodiPlay");
}
}
function performCommand(event) {
if (event.command === "kodiplay") {
var currentURL = event.target.browserWindow.activeTab.url;
if (currentURL && canPlayUrl(currentURL)) {
playUrl(currentURL);
}
}
}
safari.application.addEventListener("command", performCommand, false);
</script>
</head>
<body>Global Background Page</body>
</html>