(function(data, sendResponse){
function addVideoToPopup(srces, tracks, poster) {
console.log(srces, tracks, poster);
OV.messages.send({
func: "addVideoToPopup",
data: { playerData: { src: srces, tracks: tracks, poster: poster, host: location.href } },
bgdata: { func: "toTopWindow" }
});
}
function toSaveUrl(url) {
return OV.tools.getAbsoluteUrl(url);
//return x + (x.indexOf("?") == -1 ? "?" : "&") + "OVreferer="+encodeURIComponent(location.href)
}
function getVJSPlayerSrces(player) {
var hash;
if(player.options_.sources && player.options_.sources.length > 0) {
hash = player.options_.sources;
}
else if(player.cache_.sources) {
hash = player.cache_.sources;
}
else if(player.cache_.source) {
hash = player.cache_.source;
}
else if(player.cache_.src){
hash = player.cache_;
}
else {
hash = {src: player.src(), type: "video/mp4" }
}
if(hash.concat) {
hash.forEach(function (elem) {
elem.src = toSaveUrl(elem.src);
if(elem["data-res"]) {
elem.label = elem["data-res"];
}
if(!elem.type) {
elem.type = "video/mp4";
}
});
}
else {
hash.src = toSaveUrl(hash.src);
if(!hash.type) {
hash.type = "video/mp4";
}
}
return hash;
}
function getVJSPlayerCaptions(player) {
var tracks = [];
OV.tools.forEach(player.textTracks(), function(textTrack) {
var track = {};
if(textTrack.options_ && textTrack.options_.src) {
track.src = OV.tools.getAbsoluteUrl(textTrack.options_.src);
}
else if(textTrack.cues_.length != 0) {
track.cues = [];
textTrack.cues_.forEach(function (cue) {
track.cues.push({ startTime: cue.startTime, endTime: cue.endTime, text: cue.text });
});
}
else {
return;
}
if(typeof textTrack.kind == "function") {
track.kind = textTrack.kind();
track.language = textTrack.language();
track.label = textTrack.label();
if(textTrack.default) {
track.default = textTrack.default();
}
}
else {
track.kind = textTrack.kind;
track.language = textTrack.language;
track.label = textTrack.label;
track.default = textTrack.default;
}
tracks.push(track);
});
return tracks;
}
function getVideoJSPlayers() {
if(window['videojs'] != undefined) {
console.log("VIDEOJS FOUND");
return OV.wrap(videojs.players);
}
}
function getJWPlayers() {
if(window['jwplayer'] == undefined) {
return null;
}
console.log("JWPLAYER FOUND");
var arr = [];
for(var i=0, player=jwplayer(0);player.on;player=jwplayer(++i)) {
arr.push(player);
}
return OV.wrap(arr);
}
function isPlayerLibrary() {
return window['jwplayer'] != null || window['videojs'] != null;
}
function getJWPlayerSrces(player) {
var srces = OV.wrap(player.getPlaylist()[0].sources);
srces.forEach(function(src) {
src.renameKey("file", "src");
src.src = toSaveUrl(src.src);
if(src.type == "hls") {
src.type = "application/x-mpegURL";
}
else {
src.type = "video/"+src.type;
}
});
return srces;
}
function getJWPlayerCaptions(player) {
var tracks = OV.wrap(player.getPlaylist()[0].tracks);
tracks.forEach(function(track) {
track.renameKey("file", "src");
track.src = OV.tools.getAbsoluteUrl(track.src);
});
return tracks;
}
function SetupVideo(videoNode) {
if(!videoNode.dataset.isRegistred) {
videoNode.dataset.isRegistred = true;
}
}
function getSrc(videoNode) {
var srces = [];
OV.tools.forEach(videoNode.getElementsByTagName("source"), function (source){
var hash = {src: toSaveUrl(source.src), type: source.type};
if(source.label) {
hash.label = source.label;
}
else if(source.dataset.res) {
hash.label = source.dataset.res;
}
if(source.default) {
srces.unshift(hash);
}
else {
srces.push(hash);
}
});
if(srces.length == 0) {
addVideoToPopup({src: toSaveUrl(videoNode.src), type: "video/mp4" }, [], videoNode.poster);
}
else {
addVideoToPopup(srces, [], videoNode.poster);
}
}
document.addEventListener("DOMContentLoaded", function() {
console.log("OpenVideo Search is here!", location.href);
/*var videoArr = document.getElementsByTagName("video");
OV.tools.forEach(videoArr, function(videoNode){
SetupVideo(videoNode);
});*/
var videoJSPlayers = getVideoJSPlayers();
if(videoJSPlayers) {
videoJSPlayers.forEach(function(player) {
addVideoToPopup(getVJSPlayerSrces(player), getVJSPlayerCaptions(player), player.poster());
player.on('loadstart', function(){
console.log("testest")
addVideoToPopup(getVJSPlayerSrces(player), getVJSPlayerCaptions(player), player.poster());
});
});
if(videojs.hook) {
videojs.hook('setup', function(player) {
addVideoToPopup(getVJSPlayerSrces(player), getVJSPlayerCaptions(player), player.poster());
player.on('loadstart', function(){
addVideoToPopup(getVJSPlayerSrces(player), getVJSPlayerCaptions(player), player.poster());
});
});
}
}
var jwPlayers = getJWPlayers();
if(jwPlayers) {
jwPlayers.forEach(function (player){
addVideoToPopup(getJWPlayerSrces(player), getJWPlayerCaptions(player), player.getPlaylist()[0].image);
player.on('meta', function(){
addVideoToPopup(getJWPlayerSrces(player), getJWPlayerCaptions(player), player.getPlaylist()[0].image);
});
})
}
function setupPlainVideoListener(video) {
//video.play();
console.log(video);
if(!isPlayerLibrary()) {
getSrc(video);
video.addEventListener('loadedmetadata', function(){
console.log("METADATA")
getSrc(video);
});
}
else {
var jwPlayers = getJWPlayers();
if(jwPlayers) {
jwPlayers.forEach(function (player){
addVideoToPopup(getJWPlayerSrces(player), getJWPlayerCaptions(player), player.getPlaylist()[0].image);
player.on('meta', function(){
addVideoToPopup(getJWPlayerSrces(player), getJWPlayerCaptions(player), player.getPlaylist()[0].image);
});
})
}
}
}
OV.tools.forEach(document.getElementsByTagName("video"), function(videoNode) {
setupPlainVideoListener(videoNode);
});
document.addEventListener("DOMNodeInserted", function(e){
if(e.target.tagName && e.target.tagName.toLowerCase() == "video") {
setupPlainVideoListener(e.target);
}
});
});
})(null,null);