aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/players/Iframe.hx
blob: ae6e6dc76fc94127893a4b6319dfc68aa203f9e7 (plain) (blame)
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
package client.players;

import Types.PlayerType;
import Types.VideoData;
import Types.VideoDataRequest;
import Types.VideoItem;
import client.Main.getEl;
import js.Browser.document;
import js.Browser;
import js.html.Element;

class Iframe implements IPlayer {
	static final matchTwitchChannel = ~/^(?:https?:\/\/)?(?:www\.|m\.)?twitch\.tv\/([A-Za-z0-9_]+)\/?$/i;

	final main:Main;
	final player:Player;
	final playerEl:Element = getEl("#ytapiplayer");
	var video:Element;

	public function new(main:Main, player:Player) {
		this.main = main;
		this.player = player;
	}

	public function getPlayerType():PlayerType {
		return IframeType;
	}

	public static function extractTwitchChannel(url:String):String {
		url = url.trim();
		if (!matchTwitchChannel.match(url)) return "";
		final channel = matchTwitchChannel.matched(1);
		return channel;
	}

	public function isSupportedLink(url:String):Bool {
		final trimmed = url.trim();
		if (extractTwitchChannel(trimmed).length > 0) return true;
		if (trimmed.startsWith("<iframe") || trimmed.startsWith("<object")) return true;
		return false;
	}

	public function getVideoData(data:VideoDataRequest, callback:(data:VideoData) -> Void):Void {
		final rawUrl = data.url.trim();
		final channel = extractTwitchChannel(rawUrl);
		if (channel.length > 0) {
			final iframeStr = '<iframe src="https://player.twitch.tv/?channel=$channel&parent=www.example.com" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>';
			callback({
				duration: 99 * 60 * 60,
				title: 'Twitch: $channel',
				url: iframeStr
			});
			return;
		}

		final iframe = document.createDivElement();
		iframe.innerHTML = rawUrl;
		if (isValidIframe(iframe)) {
			callback({
				duration: 99 * 60 * 60,
				title: "Iframe media",
				url: rawUrl
			});
		} else {
			callback({duration: 0});
		}
	}

	function isValidIframe(iframe:Element):Bool {
		if (iframe.children.length != 1) return false;
		return (iframe.firstChild.nodeName == "IFRAME"
			|| iframe.firstChild.nodeName == "OBJECT");
	}

	public function loadVideo(item:VideoItem):Void {
		removeVideo();
		video = document.createDivElement();
		var data = item.url;

		if (data.contains("player.twitch.tv")) {
			final hostname = Browser.location.hostname;
			data = data.replace("parent=www.example.com", 'parent=$hostname');
			if (!~/[A-z]/.match(hostname)) {
				Main.instance.serverMessage(
					'Twitch player blocks access from ips, please use SyncTube from any domain for it.
You can register some on <a href="https://nya.pub" target="_blank">nya.pub</a>.',
					false
				);
			}
		}

		video.innerHTML = data;
		if (!isValidIframe(video)) {
			video = null;
			return;
		}
		if (video.firstChild.nodeName == "IFRAME") {
			video.setAttribute("sandbox", "allow-scripts");
			video.classList.add("videoplayerIframeParent");
		}
		video.firstElementChild.id = "videoplayer";
		playerEl.appendChild(video);
	}

	public function removeVideo():Void {
		if (video == null) return;
		playerEl.removeChild(video);
		video = null;
	}

	public function isVideoLoaded():Bool {
		return video != null;
	}

	public function play():Void {}

	public function pause():Void {}

	public function isPaused():Bool {
		return false;
	}

	public function getTime():Float {
		return 0;
	}

	public function setTime(time:Float):Void {}

	public function getPlaybackRate():Float {
		return 1;
	}

	public function setPlaybackRate(rate:Float):Void {}

	public function getVolume():Float {
		return 1;
	}

	public function setVolume(volume:Float) {}

	public function unmute():Void {}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage