-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKodi.js
More file actions
122 lines (94 loc) · 2.73 KB
/
Copy pathKodi.js
File metadata and controls
122 lines (94 loc) · 2.73 KB
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
+(function (window, Connection) {
'use strict';
var Playlist = {
AUDIO: 0,
VIDEO: 1,
PICTURE: 2
};
function Kodi(endpoint) {
this._conn = createConnection(endpoint);
this._conn.connect();
}
Kodi.prototype = Object.create(null, {
constructor: {
value: Kodi
},
connection: {
get: function () {
return this._conn;
}
}
});
Kodi.prototype.getActivePlayers = function () {
return call(this._conn, 'Player.getActivePlayers');
};
Kodi.prototype.getCurrentPlayedItem = function () {
var conn = this._conn;
return this.getActivePlayers().then(function (result) {
if (result && result.length) {
return call(conn, 'Player.GetItem', { playerid: result[0].playerid }).then(function (result) {
return result ? result.item.label : '';
});
}
return '';
});
};
Kodi.prototype.addItems = function (playlistId, path) {
return call(this._conn, 'Playlist.Add', [playlistId,
(path.constructor === Array ?
path.map(function (p) { return { file: p } }) :
{ file: path })
]);
};
Kodi.prototype.clearItems = function (playlistId) {
return call(this._conn, 'Playlist.Clear', [playlistId]);
};
Kodi.prototype.open = function (playlistId) {
return call(this._conn, 'Player.Open', {
item: {
playlistid: playlistId,
position: 0
},
options: {}
});
};
Kodi.prototype.stop = function (playlistId) {
return call(this._conn, 'Player.Stop', [playlistId]);
};
Kodi.prototype.pause = function (playlistId) {
return call(this._conn, 'Player.PlayPause', [playlistId, 'toggle']);
};
Kodi.prototype.next = function (playlistId) {
return call(this._conn, 'Player.GoTo', [playlistId, 'next']);
};
Kodi.prototype.previous = function (playlistId) {
return call(this._conn, 'Player.GoTo', [playlistId, 'previous']);
};
function createConnection(endpoint) {
var conn;
if (endpoint.startsWith('http')) {
conn = new Connection(pseudoId(), endpoint, null, Kodi.PATH);
} else {
conn = new Connection(pseudoId(), null, endpoint, Kodi.PATH);
}
conn.connect();
return conn;
}
function call(conn, method, params) {
return new Promise(function (resolve) {
conn.call(method, params, function (result) {
resolve(result);
});
});
}
function pseudoId() {
var text = "",
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
Kodi.PATH = '/jsonrpc';
Kodi.Playlist = Playlist;
window.Kodi = Kodi;
}(window, window.Connection));