2018-01-18 22:34:42 +01:00
if ( Debug . debug )
console . log ( "Loading: FullScreenDetect.js" ) ;
var _pd _isFullScreen = function ( ) {
return ( window . innerHeight == window . screen . height && window . innerWidth == window . screen . width ) ;
}
2018-01-18 00:11:03 +01:00
2018-04-10 21:35:34 +02:00
/ * s p r e j m e < v i d e o > t a g ( e l e m e n t ) i n s e z n a m i m e n , k i s e l a h k o p o j a v i j o v r a z r e d i h o z . i d s t a r š e v .
2018-01-18 00:11:03 +01:00
// vrne dimenzije predvajalnika (širina, višina)
//
// Na youtube v theater mode je razširitev rahlo pokvarjena. Video tag ostane večji od predvajalnika, ko se zapusti
// celozaslonski način. Ta funkcija skuša to težavo rešiti tako, da poišče element predvajalnika, ki je zavit okoli videa.
//
// Funkcija izkorišča lastnost, da bi načeloma moral biti vsak zunanji element večji od notranjega. Najmanjši element od
// <video> značke pa do korena drevesa bi tako moral biti predvajalnik.
//
2018-04-10 21:35:34 +02:00
// Če je podan seznam imen, potem funkcija vrne dimenzije prvega elementa, ki v id oz. razredu vsebuje katerokoli ime iz seznama
2018-01-18 00:11:03 +01:00
//
// | EN |
//
2018-04-10 21:35:34 +02:00
// accepts <video> tag (element) and list of names that can appear in id or class
2018-01-18 00:11:03 +01:00
// returns player dimensions (width, height)
//
// Theater mode is mildly broken on youtube. <video> tag remains bigger than the player after leaving the fullscreen mode, and
// there's nothing we can do about that. This function aims to solve the problem by finding the player element that's wrapped around
// the <video> tag.
//
// In general, an outer tag should be bigger than the inner tag. Therefore the smallest element between <video> tag and the document
// root should be the player.
2018-04-10 21:35:34 +02:00
//
// If list of names is provided, the function returns dimensions of the first element that contains any name from the list in either
// id or class.
2018-01-18 00:11:03 +01:00
* /
2018-01-18 22:34:42 +01:00
2018-04-10 21:35:34 +02:00
var _pd _getPlayerDimensions = function ( startElement , elementNames ) {
2018-03-10 12:30:38 +01:00
var element = startElement ;
2018-02-26 22:38:17 +01:00
2018-03-07 23:49:43 +01:00
if ( element == null || element == undefined ) {
2018-02-04 17:39:26 +01:00
if ( Debug . debug )
console . log ( "[PlayerDetect::_pd_getPlayerDimensions] element is not valid, doing nothing." , element )
return ;
}
2018-03-07 21:42:40 +01:00
2018-03-10 12:30:38 +01:00
var isFullScreen = _pd _isFullScreen ( ) ;
2018-03-07 23:49:43 +01:00
2018-02-22 22:10:13 +01:00
var trustCandidateAfterGrows = 2 ; // if candidate_width or candidate_height increases in either dimensions this many
// times, we say we found our player. (This number ignores weird elements)
2018-01-18 00:11:03 +01:00
// in case our <video> is bigger than player in one dimension but smaller in the other
// if site is coded properly, player can't be wider than that
2018-03-07 23:49:43 +01:00
var candidate _width = Math . max ( element . offsetWidth , window . innerWidth ) ;
var candidate _height = Math . max ( element . offsetHeight , window . innerHeight ) ;
2018-03-10 12:30:38 +01:00
var playerCandidateNode = startElement ;
2018-01-18 00:11:03 +01:00
// <video> can't be root in a document, so we can do this
element = element . parentNode ;
2018-03-07 23:49:43 +01:00
try {
2018-03-07 21:42:40 +01:00
var grows = trustCandidateAfterGrows ;
2018-01-18 22:34:42 +01:00
while ( element != undefined ) {
2018-01-18 00:11:03 +01:00
// odstranimo čudne elemente, ti bi pokvarili zadeve
// remove weird elements, those would break our stuff
if ( element . offsetWidth == 0 || element . offsetHeight == 0 ) {
element = element . parentNode ;
continue ;
}
2018-03-07 21:42:40 +01:00
2018-01-18 00:11:03 +01:00
if ( element . offsetHeight <= candidate _height &&
element . offsetWidth <= candidate _width ) {
2018-03-07 21:42:40 +01:00
2018-03-10 12:30:38 +01:00
// if we're in fullscreen, we only consider elements that are exactly as big as the monitor.
if ( ! isFullScreen ||
( element . offsetWidth == window . innerWidth && element . offsetHeight == window . innerHeight ) ) {
playerCandidateNode = element ;
candidate _width = element . offsetWidth ;
candidate _height = element . offsetHeight ;
grows = trustCandidateAfterGrows ;
if ( Debug . debug ) {
console . log ( "Found new candidate for player. Dimensions: w:" , candidate _width , "h:" , candidate _height , "node:" , playerCandidateNode ) ;
}
2018-02-22 22:10:13 +01:00
}
}
2018-03-07 21:42:40 +01:00
else if ( grows -- <= 0 ) {
2018-02-22 22:10:13 +01:00
if ( Debug . debug && Debug . playerDetect ) {
console . log ( "Current element grew in comparrison to the child. We probably found the player. breaking loop, returning current result" ) ;
}
break ;
2018-01-18 00:11:03 +01:00
}
element = element . parentNode ;
}
2018-03-07 21:42:40 +01:00
}
catch ( e ) {
console . log ( "pdeeee," , e ) ;
}
2018-03-10 12:30:38 +01:00
var dims ;
if ( isFullScreen && playerCandidateNode == startElement ) {
dims = {
width : window . innerWidth ,
height : window . innerHeight ,
2018-03-14 23:51:13 +01:00
element : null ,
fullscreen : true
2018-03-10 12:30:38 +01:00
}
}
else {
dims = {
width : candidate _width ,
height : candidate _height ,
2018-03-14 23:51:13 +01:00
element : playerCandidateNode ,
fullscreen : isFullScreen
2018-03-10 12:30:38 +01:00
} ;
}
2018-01-18 22:34:42 +01:00
return dims ;
2018-01-18 00:11:03 +01:00
}
2018-02-26 22:38:17 +01:00
// returns 'true' if there was a change.
var _pd _checkPlayerSizeChange = function ( ) {
2018-03-14 23:51:13 +01:00
2018-04-22 14:35:40 +02:00
// console.log("Player:", GlobalVars.playerDimensions, "Node:", GlobalVars.playerDimensions.element)
2018-04-10 21:35:34 +02:00
2018-03-14 23:51:13 +01:00
if ( Debug . debug ) {
if ( GlobalVars . playerDimensions . element == undefined )
console . log ( "[PlayerDetect] player size changed. reason: player element undefined" ) ;
if ( GlobalVars . playerDimensions . fullscreen ) {
if ( ! _pd _isFullScreen ( ) ) {
console . log ( "[PlayerDetect] player size changed. reason: exited fullscreen" ) ;
}
}
2018-05-01 23:09:58 +02:00
if ( ! GlobalVars . playerDimensions . element )
console . log ( "[PlayerDetect] player element isnt defined" ) ;
if ( GlobalVars . playerDimensions . element &&
( GlobalVars . playerDimensions . width != GlobalVars . playerDimensions . element . offsetWidth ||
GlobalVars . playerDimensions . height != GlobalVars . playerDimensions . element . offsetHeight )
) {
2018-04-22 14:35:40 +02:00
console . log ( "[PlayerDetect] player size changed. reason: dimension change. Old dimensions?" , GlobalVars . playerDimensions . width , GlobalVars . playerDimensions . height , "new dimensions:" , GlobalVars . playerDimensions . element . offsetWidth , GlobalVars . playerDimensions . element . offsetHeight ) ;
2018-03-14 23:51:13 +01:00
}
}
2018-02-26 22:38:17 +01:00
if ( GlobalVars . playerDimensions . element == undefined )
return true ;
2018-04-11 00:19:44 +02:00
2018-03-10 12:30:38 +01:00
2018-02-26 22:38:17 +01:00
if ( GlobalVars . playerDimensions . width != GlobalVars . playerDimensions . element . offsetWidth || GlobalVars . playerDimensions . height != GlobalVars . playerDimensions . element . offsetHeight ) {
return true ;
}
2018-04-11 00:19:44 +02:00
// if(GlobalVars.playerDimensions.fullscreen){
// return ! _pd_isFullScreen();
// }
2018-02-26 22:38:17 +01:00
return false ;
}
2018-01-18 00:11:03 +01:00
var PlayerDetect = {
2018-01-18 22:34:42 +01:00
getPlayerDimensions : _pd _getPlayerDimensions ,
2018-02-26 22:38:17 +01:00
checkPlayerSizeChange : _pd _checkPlayerSizeChange ,
2018-01-18 22:34:42 +01:00
isFullScreen : _pd _isFullScreen
2018-01-18 00:11:03 +01:00
}