This is extern for Haxe. A tool for controlling Youtube with Javascript for everyone.
Original API document: https://developers.google.com/youtube/iframe_api_reference
If you use Youtube.init
, loading Youtube IFrame Player API with HTML is unnecessary.
Please refer to the following haxe code or example directory.
HTML code.
<div id="video" data-video-id="M7lc1UVf-VE"></div>
Haxe code:
import js.Browser;
import js.html.Element;
import js.youtube.Youtube;
import js.youtube.YoutubePlayer;
class Main {
public static function main():Void {
Browser.window.addEventListener('DOMContentLoaded',function() {
Youtube.init(onAPIReady);
});
}
private static function onAPIReady():Void {
var target:Element = Browser.document.getElementById('video');
var player:YoutubePlayer = new YoutubePlayer(target.id,{
videoId:target.dataset.videoId,
events: {
onReady:function(event:ReadyEvent) {
}
}
});
}
}
Generate JavaScript code:
// Generated by Haxe 3.4.7
(function () { "use strict";
var Main = function() { };
Main.main = function() {
window.addEventListener("DOMContentLoaded",function() {
js_youtube_Youtube.init(Main.onAPIReady);
});
};
Main.onAPIReady = function() {
var target = window.document.getElementById("video");
var player = new YT.Player(target.id,{ videoId : target.dataset.videoId, events : { onReady : function(event) {
}}});
};
var js_youtube_Youtube = function() { };
js_youtube_Youtube.init = function(onAPIReady) {
var firstElement = window.document.getElementsByTagName("script")[0];
var script = window.document.createElement("script");
script.src = "https://www.youtube.com/player_api";
firstElement.parentNode.insertBefore(script,firstElement);
window.onYouTubePlayerAPIReady = function() {
js_youtube_Youtube.isLoadedAPI = true;
if(onAPIReady != null) {
onAPIReady();
}
};
};
js_youtube_Youtube.isLoadedAPI = false;
js_youtube_Youtube.API_URL = "https://www.youtube.com/player_api";
Main.main();
})();
Original API document: https://developers.google.com/youtube/iframe_api_reference