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
|
package client;
import Types.VideoItem;
import Types.WsEvent;
import Types.WsEventType;
import js.Browser.document;
import js.Browser.window;
import js.Syntax;
private typedef VideoChangeFunc = (item:VideoItem) -> Void;
private typedef OnceEventFunc = (event:WsEvent) -> Void;
class JsApi {
static var main:Main;
static var player:Player;
static final subtitleFormats = [];
static final videoChange:Array<VideoChangeFunc> = [];
static final videoRemove:Array<VideoChangeFunc> = [];
static final onceListeners:Array<{type:WsEventType, func:OnceEventFunc}> = [];
public static function init(main:Main, player:Player):Void {
JsApi.main = main;
JsApi.player = player;
initPluginsSpace();
}
static function initPluginsSpace():Void {
final w:Dynamic = window;
w.synctube ??= {};
}
@:expose
static function addPlugin(id:String, ?onLoaded:() -> Void):Void {
addScriptToHead('/plugins/$id/index.js', () -> {
final obj = {
api: Syntax.plainCode("client.JsApi"),
id: id,
path: '/plugins/$id'
}
if (untyped window.synctube[id] == null) {
window.console.error('Plugin "$id" not found');
} else {
Syntax.code("new synctube[id]({0})", obj);
if (onLoaded != null) onLoaded();
}
});
}
@:expose
public static function addScriptToHead(url:String, ?onLoaded:() -> Void):Void {
final script = document.createScriptElement();
script.type = "text/javascript";
script.onload = onLoaded;
script.src = url;
document.head.appendChild(script);
}
@:expose
static function hasScriptInHead(url:String):Bool {
for (child in document.getElementsByTagName("head")[0].children) {
if ((child : Dynamic).src == url) return true;
}
return false;
}
@:expose
static function getVideoItems():Array<VideoItem> {
final items = player.getItems();
return [
for (item in items) Reflect.copy(item)
];
}
@:expose
static function addVideoItem(url:String, atEnd:Bool, isTemp:Bool, ?callback:() ->
Void, doCache = false):Void {
main.addVideo(url, atEnd, isTemp, doCache, callback);
}
@:expose
static function removeVideoItem(url:String):Void {
main.removeVideoItem(url);
}
@:expose
static function getTime():Float {
return player.getTime();
}
@:expose
static function setTime(time:Float):Void {
player.setTime(time);
}
@:expose
static function isLeader():Bool {
return main.isLeader();
}
@:expose
static function forceSyncNextTick(flag:Bool):Void {
main.forceSyncNextTick = flag;
}
@:expose
public static function setVideoSrc(url:String):Void {
player.changeVideoSrc(url);
}
/** Returns current page hostname (domain without protocol) **/
@:expose
static function getLocalIp():String {
return main.host;
}
/** Returns server global ip. **/
@:expose
static function getGlobalIp():String {
return main.globalIp;
}
/**
* If plugin adds any subtitle format (like `ass`),
* you will see subtitle input below video url input on client page.
* Plugins can listen to `notifyOnVideoChange(item => {...}`
* for raw videos and load that input data from `item.subs` url to do something.
* See `https://github.com/RblSb/SyncTube-octosubs` as example.
*/
@:expose
static function addSubtitleSupport(format:String):Void {
format = format.trim().toLowerCase();
if (subtitleFormats.contains(format)) return;
subtitleFormats.push(format);
}
@:expose
public static function hasSubtitleSupport(?format:String):Bool {
if (format == null) return subtitleFormats.length > 0;
return subtitleFormats.contains(format);
}
/**
* Listen to server event once before that event is parsed by client.
* Example:
* `JsApi.once("RemoveVideo", event => {`
* ` if (event.removeVideo.url == url) {...}`
* `});`
*/
@:expose
public static function once(type:WsEventType, func:OnceEventFunc):Void {
onceListeners.push({type: type, func: func});
}
public static function fireOnceEvent(event:WsEvent):Void {
var i = 0;
while (i < onceListeners.length) {
final listener = onceListeners[i];
if (listener.type == event.type) {
listener.func(event);
onceListeners.remove(listener);
continue;
}
i++;
}
}
@:expose
static function notifyOnVideoChange(func:VideoChangeFunc):Void {
videoChange.push(func);
}
@:expose
static function removeFromVideoChange(func:VideoChangeFunc):Void {
videoChange.remove(func);
}
public static function fireVideoChangeEvents(item:VideoItem):Void {
for (func in videoChange) {
func(item);
}
}
@:expose
static function notifyOnVideoRemove(func:VideoChangeFunc):Void {
videoRemove.push(func);
}
@:expose
static function removeFromVideoRemove(func:VideoChangeFunc):Void {
videoRemove.remove(func);
}
public static function fireVideoRemoveEvents(item:VideoItem):Void {
for (func in videoRemove) {
func(item);
}
}
}
|