2017-01-15 18:56:36 +01:00
var debugmsg = false ;
var debugmsg _click = true ;
2017-01-04 23:23:41 +01:00
var debugmsg _periodic = false ;
2016-12-04 23:02:15 +01:00
if ( debugmsg ) {
console . log ( ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " ) ;
console . log ( "\nLoading ultrawidify (uw)\nIf you can see this, extension at least tried to load\n\nRandom number: " , Math . floor ( Math . random ( ) * 20 ) + 1 , "\n" ) ;
console . log ( ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " ) ;
}
2017-01-02 12:29:43 +01:00
const playercheck _recursion _depth _limit = 3 ;
2016-12-04 23:02:15 +01:00
2016-10-16 13:13:34 +02:00
var extraClassAdded = false ;
var inFullScreen = false ;
var cssmod = "" ;
var zoomStep = 0.05 ;
2016-10-18 23:07:28 +02:00
var whatdo _persistence = true ;
2017-01-04 00:07:34 +01:00
var last _whatdo = { type : "reset" , what _do : "reset" } ;
2016-12-04 23:02:15 +01:00
var page _url = window . location . toString ( ) ;
2016-10-18 23:07:28 +02:00
2016-10-20 23:34:45 +02:00
var ctlbar _classnames = [ "ytp-chrome-controls" ] ;
2017-01-04 00:07:34 +01:00
var serviceArray = [ ".video-stream" ] ; //Youtube
2016-10-16 13:13:34 +02:00
2016-10-22 20:49:15 +02:00
var buttons = [ ] ;
2017-01-02 12:29:43 +01:00
//BEGIN determining which site we're on and the associated names
var control _bar ;
var ui _anchor ;
var player ;
var vid _el ;
var sample _button _class ; // Class of a sample button
var sample _button _index = 0 ; // index of a sample button
var button _size _base = "x" ; // Determines if size of extension buttons is defined by width or height of sample button
var video _wrap ;
2017-01-04 23:23:41 +01:00
// Here we store the window size, so we know when to trigger css change.
var winsize = { w : window . innerWidth , h : window . innerHeight } ;
// provider-specific variables
var netflix _cltbar _visibility = - 1 ; // -1 for invisible, anything non-negative for visible
var netflix _periodic _timer ;
2017-01-03 20:56:01 +01:00
function init ( force _reload ) {
2017-01-02 12:29:43 +01:00
if ( debugmsg )
console . log ( "uw::init | starting init" ) ;
//Youtube:
if ( page _url . indexOf ( "youtu" ) != - 1 ) {
if ( debugmsg )
console . log ( "uw::init | we're on youtube. Page url:" , page _url ) ;
control _bar = "ytp-chrome-controls" ;
sample _button _class = "ytp-button ytp-settings-button" ;
if ( inIframe ( ) )
player = document . getElementById ( "player" )
else
player = document . getElementById ( "movie_player" ) ;
video _wrap = "video-stream" ;
2017-01-02 17:45:51 +01:00
return true ;
2017-01-02 12:29:43 +01:00
}
//Netflix:
if ( page _url . indexOf ( "netflix.com" ) != - 1 ) {
if ( debugmsg )
console . log ( "uw::init | we're on netflix. Page url:" , page _url ) ;
player = document . getElementById ( "playerContainer" ) ;
var tmp = document . getElementsByClassName ( "player-status" ) [ 0 ] ;
if ( ! tmp )
return false ;
2017-01-03 20:56:01 +01:00
if ( force _reload )
ui _anchor . parentNode ( ) . remove ( ui _anchor ) ;
2017-01-02 12:29:43 +01:00
ui _anchor = document . getElementsByClassName ( "uw-button-row" ) [ 0 ] ;
2017-01-03 20:56:01 +01:00
2017-01-02 12:29:43 +01:00
if ( ! ui _anchor ) {
ui _anchor = document . createElement ( "div" ) ;
ui _anchor . className = "uw-button-row" ;
}
if ( debugmsg )
console . log ( "uw::init | we're on netflix. ui anchor: " , ui _anchor , "; ui anchor parent:" , tmp ) ;
tmp . appendChild ( ui _anchor ) ;
vid _el = document . getElementsByTagName ( "video" ) [ 0 ] ;
sample _button _class = "player-control-button player-fill-screen" ;
video _wrap = "player-video-wrapper" ;
button _size _base = "y" ;
2017-01-02 17:45:51 +01:00
return true ;
2017-01-02 12:29:43 +01:00
}
2017-01-02 17:45:51 +01:00
return false ;
2017-01-02 12:29:43 +01:00
}
//END
2016-11-29 19:54:05 +01:00
2016-12-04 23:02:15 +01:00
//BEGIN ADDING CSS
// Če ponovno naložimo dodatek, potem odstranimo star CSS. Lahko se zgodi, da je Tam spremenil CSS in hoče
// preveriti, če stvari zgledajo tako kot morajo. Če ima en razred/id več nasprotujoćih si definicij, potem
// nam to lahko povzroča težave. Za uporabnike je načeloma odstranjevanje obstoječega CSS brezpredmetno, ker
// uporabnik ponavadi ne bo reloadal razširitve.
//
// If we reload the extension, then we also remove our old CSS. It can easily happen that Tam changed CSS a bit
// and wants to see if things look roughly the way they should. We do this because if a class/id has multiple
// mutually exclusive definitions, we can get some problems with CSS not working the way it should. People who
// aren't Tam generally don't see the benefit as they won't reload the extension — let alone reload the extension
// after messing with CSS.
var uwcss = document . getElementsByClassName ( "uw_css" ) ;
while ( uwcss && uwcss . length > 0 )
uwcss [ 0 ] . parentNode . removeChild ( uwcss [ 0 ] ) ;
// funkcija pomagač za ustvarjanje css linkov
// helper function for creating css links
function addLink ( css _url ) {
var link = document . createElement ( "link" ) ;
link . className = "uw_css" ;
link . setAttribute ( "rel" , "stylesheet" ) ;
link . setAttribute ( "type" , "text/css" ) ;
link . setAttribute ( "href" , resourceToUrl ( css _url ) ) ;
$ ( "head" ) . append ( link ) ;
}
// Vsaka stran dobi uw_common.css
// We add uw_common.css on every page
addLink ( "res/css/uw_common.css" ) ;
// Če smo na Youtube/youtube popupu, dodamo css za youtube elemente
// If we're on youtube/youtube popup, we add css for youtube elements
if ( page _url . indexOf ( "youtu" ) != - 1 ) {
addLink ( "res/css/uw_yt.css" ) ;
}
2017-01-02 12:29:43 +01:00
if ( page _url . indexOf ( "netflix.com" ) != - 1 ) {
addLink ( "res/css/uw_netflix.css" ) ;
}
2016-12-04 23:02:15 +01:00
//END ADDING CSS
2017-01-04 23:23:41 +01:00
2017-01-02 12:29:43 +01:00
//BEGIN keybind-related stuff
2016-11-16 19:52:09 +01:00
// Yeah hi /r/badcode.
// Anyway, because nazi localstorage flat out refuses to store arrays:
var DEFAULT _KEYBINDINGS = {
2016-12-07 23:58:43 +01:00
0 : { action : "fitw" ,
2016-11-15 22:28:39 +01:00
key : 'w' ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
1 : {
2016-11-16 19:52:09 +01:00
action : "fith" ,
2016-11-15 22:28:39 +01:00
key : 'e' ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
2 : {
2016-11-15 22:28:39 +01:00
action : "reset" ,
key : 'r' ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
3 : {
2016-11-15 22:28:39 +01:00
action : "zoom" ,
key : "z" ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
4 : {
2016-11-15 22:28:39 +01:00
action : "unzoom" ,
key : "u" ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
5 : {
2016-11-15 22:28:39 +01:00
action : "char" ,
targetAR : ( 21 / 9 ) ,
key : "d" ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
6 : {
2016-11-15 22:28:39 +01:00
action : "char" ,
targetAR : ( 16 / 9 ) ,
key : "s" ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
7 : {
2016-11-15 22:28:39 +01:00
action : "char" ,
targetAR : ( 16 / 10 ) ,
key : "x" ,
modifiers : [ ]
} ,
2016-12-07 23:58:43 +01:00
8 : {
2016-11-15 22:28:39 +01:00
action : "char" ,
targetAR : ( 4 / 3 ) ,
key : "a" ,
modifiers : [ ]
}
2016-11-16 19:52:09 +01:00
} ;
2016-12-21 23:25:05 +01:00
var last _location = "" ;
2016-11-16 19:52:09 +01:00
var KEYBINDS = { } ;
var ask4keybinds = browser . storage . local . get ( "ultrawidify_keybinds" ) ;
ask4keybinds . then ( ( res ) => {
if ( res . length == 1 && jQuery . isEmptyObject ( res [ 0 ] ) ) {
if ( debugmsg )
console . log ( "uw::<init keybinds> | No keybindings found. Loading default keybinds as keybinds" ) ;
browser . storage . local . set ( { ultrawidify _keybinds : DEFAULT _KEYBINDINGS } ) ;
KEYBINDS = DEFAULT _KEYBINDINGS ;
}
else {
KEYBINDS = res [ 0 ] . ultrawidify _keybinds ;
}
2016-12-04 23:02:15 +01:00
// console.log("res. ", res[0].ultrawidify_keybinds);
2016-11-16 19:52:09 +01:00
} ) ;
2017-01-02 12:29:43 +01:00
//END keybind-related stuff
//BEGIN comms with uw-bg
2016-12-21 23:25:05 +01:00
if ( debugmsg )
console . log ( "uw | Setting up comms with background scripts" ) ;
2016-11-16 19:52:09 +01:00
2017-01-02 17:45:51 +01:00
var num _of _msg = 0 ;
2016-12-21 23:25:05 +01:00
browser . runtime . onMessage . addListener ( function ( message , sender , stuff ) {
if ( debugmsg )
2017-01-03 20:56:01 +01:00
console . log ( "uw::onMessage | message number: " , num _of _msg ++ , "; message:" , message . message , "player-status elements:" , document . getElementsByClassName ( "player-status" ) . length , document . getElementsByClassName ( "player-status" ) , "uw_elements:" , document . getElementsByClassName ( "uw_element" ) . length , document . getElementsByClassName ( "uw_element" ) ) ;
2016-12-21 23:25:05 +01:00
if ( message . message == "page-change" ) {
2017-01-03 20:56:01 +01:00
if ( document . getElementsByClassName ( "uw_element" ) . length === 0 ) {
if ( debugmsg )
console . log ( "uw::onMessage | page was updated but control buttons aren't there. Trying to readd." )
init ( ) ;
addCtlButtons ( 0 ) ;
2017-01-04 00:07:34 +01:00
console . log ( last _whatdo ) ;
2017-01-03 20:56:01 +01:00
}
2017-01-14 15:17:26 +01:00
// We don't do that if we zoomed or unzoomed
if ( last _whatdo . what _do != "zoom" && last _whatdo . what _do != "unzoom" )
changeCSS ( last _whatdo . type , last _whatdo . what _do ) ;
2017-01-03 20:56:01 +01:00
// Velikost gumbov posodobimo v vsakem primeru
// We update the button size in any case
updateCtlButtonSize ( ) ;
2017-01-04 23:23:41 +01:00
if ( debugmsg )
console . log ( "uw::onMessage | message number:" , num _of _msg , " »» everything is done. Buttons: " , document . getElementsByClassName ( "uw-button" ) ) ;
2016-12-21 23:25:05 +01:00
}
} ) ;
2017-01-04 23:23:41 +01:00
//Because onUpdated event isn't reliable enough for what we're doing on netflix.
function periodic ( ) {
if ( debugmsg _periodic )
console . log ( "uw::periodic started!" ) ;
if ( document . getElementsByClassName ( "uw_element" ) . length === 0 ) {
if ( debugmsg )
console . log ( "uw::periodic | no buttons detected. Readding." ) ;
init ( ) ;
addCtlButtons ( 0 ) ;
}
var w = window . innerWidth ;
var h = window . innerHeight ;
if ( winsize . w != w && winsize . h != h ) {
console . log ( "uw::periodic | detected change in window size. Triggering css change" ) ;
winsize . w = w ;
winsize . h = h ;
2017-01-14 15:17:26 +01:00
// We don't do that if we zoomed or unzoomed
if ( last _whatdo . what _do != "zoom" && last _whatdo . what _do != "unzoom" ) {
changeCSS ( last _whatdo . type , last _whatdo . what _do ) ;
}
2017-01-04 23:23:41 +01:00
updateCtlButtonSize ( ) ;
}
var controls = document . getElementsByClassName ( "player-controls-wrapper" ) [ 0 ] ;
if ( controls ) {
if ( debugmsg _periodic )
console . log ( "uw::periodic | we found controls!" ) ;
var ind = controls . className . indexOf ( "display-none" ) ;
if ( debugmsg _periodic )
console . log ( "uw::periodic | ind:" , ind , "last ind:" , netflix _cltbar _visibility ) ;
// controls must be visible. We must have not called updateCtlButtonSize before.
if ( ind != netflix _cltbar _visibility ) {
if ( debugmsg )
console . log ( "uw::periodic | toggled visibility" ) ;
netflix _cltbar _visibility = ind ;
if ( ind == - 1 )
updateCtlButtonSize ( ) ;
}
}
}
2016-12-21 23:25:05 +01:00
// browser.runtime.onMessage.addListener(request => {
// console.log("Message from the background script:");
// console.log(request.greeting);
// return Promise.resolve({response: "Hi from content script"});
// });
if ( debugmsg )
console . log ( "uw | Comms with background scripts: done" ) ;
2017-01-02 12:29:43 +01:00
//END comms with uw-bg
2016-11-16 19:52:09 +01:00
2016-10-16 13:13:34 +02:00
$ ( document ) . ready ( function ( ) {
2016-10-22 20:49:15 +02:00
if ( debugmsg )
2016-12-21 23:25:05 +01:00
console . log ( "uw::document.ready | document is ready. Starting extension setup ..." ) ;
extSetup ( ) ;
} ) ;
//BEGIN EXTENSION SETUP
function extSetup ( ) {
if ( debugmsg )
console . log ( "==============================================================================================" ) ;
last _location = window . location ;
2016-12-29 19:41:54 +01:00
2016-12-21 23:25:05 +01:00
if ( debugmsg ) {
console . log ( "uw::extSetup | our current location is:" , last _location ) ;
2017-01-02 17:45:51 +01:00
console . log ( "uw::extSetup | initiating extension" ) ;
}
var ini = init ( ) ;
if ( debugmsg ) {
console . log ( "uw::extSetup | init exited with" , ini ) ;
2016-12-21 23:25:05 +01:00
console . log ( "uw::extSetup | removing existing keydown event from document (useful if extension was previously loaded and we navigated to a different video)" ) ;
}
$ ( document ) . off ( "keydown" ) ;
2016-10-16 13:13:34 +02:00
2016-12-21 23:25:05 +01:00
if ( debugmsg )
console . log ( "uw::extSetup | setting up keyboard shortcuts" ) ;
2017-01-02 17:45:51 +01:00
keydownSetup ( ) ;
2016-12-21 23:25:05 +01:00
addCtlButtons ( 0 ) ;
2017-01-04 23:23:41 +01:00
if ( page _url . indexOf ( "netflix.com" ) != - 1 ) {
console . log ( "uw::extSetup | starting netflix-specific setup steps" ) ;
if ( netflix _periodic _timer )
clearInterval ( netflix _periodic _timer ) ;
netflix _periodic _timer = setInterval ( function ( ) { periodic ( ) ; } , 100 ) ;
}
2016-12-21 23:25:05 +01:00
if ( debugmsg )
console . log ( "======================================[ setup finished ]======================================" ) ;
}
function keydownSetup ( ) {
2017-01-02 17:45:51 +01:00
if ( debugmsg )
console . log ( "uw::keydownSetup | starting keybord shortcut setup" ) ;
2016-11-15 22:28:39 +01:00
$ ( document ) . keydown ( function ( event ) { // Tukaj ugotovimo, katero tipko smo pritisnili
// Tipke upoštevamo samo, če smo v celozaslonskem načinu oz. če ne pišemo komentarja
// v nasprotnem primeru ne naredimo nič.
// We only take actions if we're in full screen or not writing a comment
if ( ! ( inFullScreen || (
2016-12-21 23:25:05 +01:00
( document . activeElement . getAttribute ( "role" ) != "textbox" ) &&
( document . activeElement . getAttribute ( "type" ) != "text" )
2016-11-15 22:28:39 +01:00
) ) ) {
if ( debugmsg )
console . log ( "We're writing a comment or something. Doing nothing" ) ;
return ;
}
2016-12-08 22:34:32 +01:00
if ( debugmsg ) {
2016-12-21 23:25:05 +01:00
// console.log(KEYBINDS);
2016-12-08 22:34:32 +01:00
console . log ( "we pressed a key: " , event . key , " | keydown: " , event . keydown ) ;
2016-12-21 23:25:05 +01:00
if ( event . key == 'p' ) {
console . log ( "uw/keydown: attempting to send message" )
var sending = browser . runtime . sendMessage ( {
test : "test message pls dont ignore"
} ) ;
sending . then ( function ( ) { } , function ( ) { console . log ( "uw/keydown: there was an error while sending a message" ) } ) ;
console . log ( "uw/keydown: test message sent! (probably)" ) ;
return ;
}
2016-12-08 22:34:32 +01:00
}
2016-12-21 23:25:05 +01:00
2016-11-16 19:52:09 +01:00
for ( i in KEYBINDS ) {
2016-12-08 22:34:32 +01:00
if ( debugmsg )
console . log ( "i: " , i , "keybinds[i]:" , KEYBINDS [ i ] ) ;
2016-12-21 23:25:05 +01:00
2016-11-16 19:52:09 +01:00
if ( event . key == KEYBINDS [ i ] . key ) {
2016-12-08 22:34:32 +01:00
if ( debugmsg )
console . log ( "Key matches!" ) ;
2016-11-15 22:28:39 +01:00
//Tipka se ujema. Preverimo še modifierje:
//Key matches. Let's check if modifiers match, too:
var mods = true ;
2016-11-16 19:52:09 +01:00
for ( var j = 0 ; j < KEYBINDS [ i ] . modifiers . length ; j ++ ) {
if ( KEYBINDS [ i ] . modifiers [ j ] == "ctrl" )
2016-11-15 22:28:39 +01:00
mods &= event . ctrlKey ;
2016-11-16 19:52:09 +01:00
else if ( KEYBINDS [ i ] . modifiers [ j ] == "alt" )
2016-11-15 22:28:39 +01:00
mods &= event . altKey ;
2016-11-16 19:52:09 +01:00
else if ( KEYBINDS [ i ] . modifiers [ j ] == "shift" )
2016-11-15 22:28:39 +01:00
mods &= event . shiftKey ;
}
2016-12-08 22:34:32 +01:00
if ( debugmsg )
console . log ( "we pressed a key: " , event . key , " | mods match?" , mods , "keybinding: " , KEYBINDS [ i ] ) ;
2016-11-15 22:28:39 +01:00
if ( mods ) {
2017-01-09 20:31:07 +01:00
event . stopPropagation ( ) ;
2016-12-21 23:25:05 +01:00
console . log ( "uw::keydown | keys match. calling changeCSS()" ) ;
2016-11-16 19:52:09 +01:00
if ( KEYBINDS [ i ] . action == "char" ) {
changeCSS ( "char" , KEYBINDS [ i ] . targetAR ) ;
2016-11-15 22:28:39 +01:00
return ;
}
2016-11-16 19:52:09 +01:00
changeCSS ( "anything goes" , KEYBINDS [ i ] . action ) ;
2016-11-15 22:28:39 +01:00
return ;
}
}
2016-10-16 13:13:34 +02:00
}
} ) ;
2016-12-21 23:25:05 +01:00
2016-10-16 13:13:34 +02:00
document . addEventListener ( "mozfullscreenchange" , function ( event ) {
2016-10-22 20:49:15 +02:00
onFullScreenChange ( ) ;
2016-10-16 13:13:34 +02:00
inFullScreen = ( window . innerHeight == window . screen . height && window . innerWidth == window . screen . width ) ;
inFullScreen ? onFullscreenOn ( ) : onFullscreenOff ( ) ;
} ) ;
2016-12-21 23:25:05 +01:00
}
2016-11-29 19:54:05 +01:00
//BEGIN UI
2017-01-02 12:29:43 +01:00
function check4player ( recursion _depth ) {
2016-12-21 23:25:05 +01:00
try {
2017-01-02 12:29:43 +01:00
var button _width = document . getElementsByClassName ( sample _button _class ) [ sample _button _index ] . scrollWidth ;
2016-12-21 23:25:05 +01:00
return true ;
}
catch ( e ) {
// Zato, ker predvajalnik ni vselej prisoten. Če predvajalnik ni prisoten,
// potem tudi knofov ni. Kar pomeni problem.
//
// Because the player isn't always there, and when the player isn't there the buttons aren't, either.
// In that case, the above statement craps out, throws an exception and trashes the extension.
if ( debugmsg )
console . log ( "uw::addCtlButtons | seems there was a fuckup and no buttons were found on this page. No player (and therefore no buttons) found." ) ;
2017-01-02 12:29:43 +01:00
if ( ! recursion _depth )
recursion _depth = 0 ;
// If buttons weren't found, we relaunch init() just
init ( ) ;
return recursion _depth < playercheck _recursion _depth _limit ? check4player ( ++ recursion _depth ) : false ;
2016-12-21 23:25:05 +01:00
}
return false ;
}
2017-01-02 12:29:43 +01:00
function addCtlButtons ( recursion _depth ) {
2016-10-20 23:34:45 +02:00
2017-01-03 20:56:01 +01:00
if ( debugmsg )
console . log ( "uw::addCtlButtons | function started" ) ;
2016-12-04 12:01:38 +01:00
2016-10-22 20:49:15 +02:00
// Gumb za nastavitve je bolj kot ne vselej prisoten, zato širino tega gumba uporabimo kot širino naših gumbov
// Settings button is more or less always there, so we use its width as width of our buttons
2016-12-21 23:25:05 +01:00
try {
2017-01-02 12:29:43 +01:00
// Na različnih straneh širino gumba poberemo na različne načine.
if ( button _size _base == "y" )
var button _width = document . getElementsByClassName ( sample _button _class ) [ sample _button _index ] . scrollHeight ;
else
var button _width = document . getElementsByClassName ( sample _button _class ) [ sample _button _index ] . scrollWidth ;
2017-01-04 23:23:41 +01:00
if ( debugmsg )
console . log ( "uw::addCtlButtons | width of the element is " , button _width , "and is based on the height of this element:" , document . getElementsByClassName ( sample _button _class ) [ sample _button _index ] , " <extra tags: onMessage>" )
2016-12-21 23:25:05 +01:00
}
catch ( e ) {
// Zato, ker predvajalnik ni vselej prisoten. Če predvajalnik ni prisoten,
// potem tudi knofov ni. Kar pomeni problem.
//
// Because the player isn't always there, and when the player isn't there the buttons aren't, either.
// In that case, the above statement craps out, throws an exception and trashes the extension.
2017-01-02 12:29:43 +01:00
2017-01-04 00:07:34 +01:00
if ( recursion _depth === undefined )
2017-01-02 12:29:43 +01:00
recursion _depth = 0 ;
2017-01-04 00:07:34 +01:00
if ( debugmsg )
console . log ( "uw::addCtlButtons | seems there was a fuckup and no buttons were found on this page. No player (and therefore no buttons) found. Recursion depth:" , recursion _depth ) ;
// If buttons weren't found, we relaunch init() just in case
2017-01-02 12:29:43 +01:00
init ( ) ;
2017-01-04 00:07:34 +01:00
return recursion _depth < playercheck _recursion _depth _limit ? addCtlButtons ( ++ recursion _depth ) : false ;
2017-01-02 12:29:43 +01:00
2016-12-21 23:25:05 +01:00
return false ;
}
2017-01-04 00:07:34 +01:00
var button _def = [ ] ;
2017-01-15 18:56:36 +01:00
// if(page_url.indexOf("netflix.com") != -1)
// button_def = [ "fitw", "fith", "reset", "zoom", "uzoom", "settings" ]; // No settings button on netflix until further notice
// else
2017-01-04 00:07:34 +01:00
button _def = [ "fitw" , "fith" , "reset" , "zoom" , "uzoom" , "settings" ] ;
2016-10-20 23:34:45 +02:00
if ( debugmsg )
console . log ( "uw::addCtlButtons | trying to add buttons" ) ;
2016-11-29 19:54:05 +01:00
// Če je ta dodatek že nameščen, potem odstranimo vse elemente. Vsi top-level elementi imajo definiran prazen razred
// uw_element, prek katerega jih naberemo na kup. Ta del kode je tu bolj ali manj zaradi debugiranja.
//
// If this addon is already installed, we have to remove all prevously added elements (to avoid duplicating).
// The easiest way to gather all of them is to tack an empty class, 'uw_element,' to each top-level element we add.
// This is here mostly because debugging, but could also be useful in case of unforseen happenings.
var previousElements = document . getElementsByClassName ( "uw_element" ) ;
2016-12-04 00:43:05 +01:00
// Uporabimo while loop in vselej odstranimo prvi element. Tabela previousElements se z odstranjevanjem elementov
// krajša.
//
// Da, to je bil bug.
//
// --------------------------------
//
// As previousElements.length decreases as we remove elements, we use a while loop and remove the first element
// for as long as the array contains elements.
//
// Yes, that used to be a bug.
2016-12-29 20:41:23 +01:00
if ( ! debugmsg ) {
2016-12-29 19:41:54 +01:00
// Če je debugmsg false, potem verjetno ne dodajamo nobenih novih funkcionalnosti, zaradi katerih bi bilo potrebno
// ponovno naložiti vmesnik. Zato tega ne storimo, temveč ohranimo stare gumbe. Ker so ok.
//
// If debugging is false, then we aren't adding any new features that would require us to reload UI. So we leave
// the old UI in place, because it should be good enough.
if ( previousElements && previousElements . length > 0 ) {
return ;
}
}
2016-12-04 00:43:05 +01:00
while ( previousElements && previousElements . length > 0 ) {
previousElements [ 0 ] . parentNode . removeChild ( previousElements [ 0 ] ) ;
}
2016-11-29 19:54:05 +01:00
2017-01-02 12:29:43 +01:00
var check _width = false ;
2016-10-22 20:49:15 +02:00
// If we're on youtube:
2017-01-02 12:29:43 +01:00
if ( page _url . indexOf ( "youtu" ) != - 1 ) {
check _width = true ;
2016-10-20 23:34:45 +02:00
2017-01-02 12:29:43 +01:00
var rctl ;
var rctl _width ;
var lctl _width ;
var ctlbar _width ;
2016-12-04 00:43:05 +01:00
if ( inIframe ( ) )
2017-01-02 12:29:43 +01:00
player = document . getElementById ( "player" ) ;
2016-12-04 00:43:05 +01:00
else
2017-01-02 12:29:43 +01:00
player = document . getElementById ( "movie_player" ) ;
2016-12-04 00:43:05 +01:00
2017-01-02 12:29:43 +01:00
rctl = document . getElementsByClassName ( "ytp-right-controls" ) [ 0 ] ;
rctl _width = rctl . offsetWidth ;
lctl _width = document . getElementsByClassName ( "ytp-left-controls" ) [ 0 ] . offsetWidth ;
ctlbar _width = document . getElementsByClassName ( "ytp-chrome-controls" ) [ 0 ] . offsetWidth ;
2016-12-08 22:34:32 +01:00
2017-01-02 12:29:43 +01:00
}
// Ker na različne strani knofe dodajamo na različne načine, določanje lastnosti in dodajanje gumbov na
// vmesnik izvedemo posebej
// Because different pages require adding buttons to the UI in a different order, we handle defining button
// properties and adding buttons to the UI in different loops.
2017-01-04 00:07:34 +01:00
var btns = button _def . length ;
for ( var i = 0 ; i < btns ; i ++ ) {
2017-01-02 12:29:43 +01:00
buttons [ i ] = document . createElement ( 'div' ) ;
buttons [ i ] . style . backgroundImage = 'url(' + resourceToUrl ( "/res/img/ytplayer-icons/" + button _def [ i ] + ".png" ) + ')' ;
buttons [ i ] . style . width = ( button _width * 0.75 ) + "px" ;
buttons [ i ] . style . height = ( button _width ) + "px" ;
2017-01-04 23:23:41 +01:00
buttons [ i ] . style . width = 0 ;
2017-01-02 12:29:43 +01:00
// buttons[i].style.marginLeft = (button_width * 0.3) + "px";
buttons [ i ] . style . paddingLeft = ( button _width * 0.15 ) + "px" ;
buttons [ i ] . style . paddingRight = ( button _width * 0.15 ) + "px" ;
buttons [ i ] . className += " uw-button uw_element" ;
}
2016-11-29 19:54:05 +01:00
2017-01-02 12:29:43 +01:00
// Tukaj dodamo gumbe na stran
// Here we add the buttons
2016-11-29 19:54:05 +01:00
2017-01-02 12:29:43 +01:00
if ( page _url . indexOf ( "netflix.com" ) != - 1 ) {
2017-01-04 00:07:34 +01:00
for ( var i = 0 ; i < btns ; i ++ ) {
2017-01-02 12:29:43 +01:00
ui _anchor . appendChild ( buttons [ i ] ) ;
2016-12-04 00:43:05 +01:00
}
2017-01-02 12:29:43 +01:00
}
else {
2017-01-04 00:07:34 +01:00
for ( var i = ( btns - 1 ) ; i >= 0 ; i -- ) {
2017-01-02 12:29:43 +01:00
$ ( rctl ) . prepend ( buttons [ i ] ) ;
2016-12-04 00:43:05 +01:00
}
2017-01-02 12:29:43 +01:00
}
// Če na ctlbar ni prostora za vse knofe, potem skrijemo vse knofe razen tistega, ki ima popup z vsemi možnostmi
//
// If ctlbar doesn't have the space for all the buttons, we hide all except the one that contains the popup
// with all the options
2017-01-04 00:07:34 +01:00
if ( check _width && ( rctl _width + lctl _width ) * 1.1 > ctlbar _width ) { //TODO: don't hardcode that 4
2017-01-02 12:29:43 +01:00
for ( var i = 4 ; i >= 0 ; i -- ) {
buttons [ i ] . classList . add ( "uw_hide" ) ;
2016-12-04 00:43:05 +01:00
}
2017-01-02 12:29:43 +01:00
}
2016-12-04 23:02:15 +01:00
2017-01-02 12:29:43 +01:00
buttons [ 0 ] . onclick = function ( ) { changeCSS ( "fit" , "fitw" ) } ;
buttons [ 1 ] . onclick = function ( ) { changeCSS ( "fit" , "fith" ) } ;
buttons [ 2 ] . onclick = function ( ) { changeCSS ( "reset" , "reset" ) } ;
buttons [ 3 ] . onclick = function ( ) { changeCSS ( "fit" , "zoom" ) } ;
buttons [ 4 ] . onclick = function ( ) { changeCSS ( "fit" , "unzoom" ) } ;
// Knof za nastavitve ima še vgnezden meni, ki ga dodamo tu (privzeto je ta meni skrit)
// Settings button contains a menu that's nested in the element. By default, that menu is
// hidden.
2017-01-15 18:56:36 +01:00
if ( btns > 5 ) {
buttons [ 5 ] . onclick = function ( ) {
if ( debugmsg || debugmsg _click )
console . log ( "uw::kbm | we clicked the button 5 with id ‘ uw-smenu’ . Button:" , document . getElementById ( "uw-smenu" ) ) ;
toggleMenu ( "uw-smenu" )
} ;
2017-01-04 00:07:34 +01:00
buttons [ 5 ] . id = "uw-settings-button" ;
}
2017-01-02 12:29:43 +01:00
var settings _menu = document . createElement ( "div" ) ;
var smenu _ar _menu = document . createElement ( "div" ) ;
2016-12-08 22:34:32 +01:00
2017-01-02 12:29:43 +01:00
var smenu _el = [ ] ;
for ( var i = 0 ; i < 7 ; i ++ ) {
smenu _el [ i ] = document . createElement ( "div" ) ;
}
var smenu _ar _options = [ ] ;
2017-01-04 00:07:34 +01:00
if ( buttons [ 5 ] )
buttons [ 5 ] . appendChild ( settings _menu ) ;
2017-01-02 12:29:43 +01:00
//Če rabimo skriti gumb za nastavitve, potem mora biti i=1
//If we need to hide settings button, then we should make i=1
//FIXME: knof za nastavitve ne radi (nastavitve se ne odprejo)
//FIXME: 'settings' button on the player doesn't work
for ( var i = 1 ; i < smenu _el . length ; i ++ ) {
settings _menu . appendChild ( smenu _el [ i ] ) ;
smenu _el [ i ] . className += "uw-setmenu-item" ;
}
for ( var i = 0 ; i < 4 ; i ++ ) {
smenu _ar _options [ i ] = document . createElement ( "div" ) ;
smenu _ar _options [ i ] . className = "uw-setmenu-item uw_element" ;
smenu _ar _menu . appendChild ( smenu _ar _options [ i ] ) ;
}
settings _menu . id = "uw-smenu" ;
settings _menu . className = "uw-setmenu uw_element" ;
smenu _el [ 0 ] . id = "uw-smenu_settings" ;
smenu _el [ 6 ] . id = "uw-smenu_ar" ;
smenu _ar _menu . id = "uw-armenu" ;
smenu _ar _menu . className = "uw-setmenu uw_element" ;
// Stvari, ki se spreminjajo, se določijo tukaj
//
// Things that can change are defined here.
var smenu _item _width = ( button _width * 7.5 ) ;
var smenu _item _fontSize = ( button _width * 0.5 ) ;
var smenu _ar _item _width = ( smenu _item _width / 3 ) ;
var smenu _item _height = button _width ;
// Popup meni je lahko visok največ 75% višine predvajalnika
// Popup menu can be at most 75% of the video player tall
var smenu _max _h = player . clientHeight * 0.75 ;
// Če je popup večji, kot 80% predvajalnika, potem ga pomanjšamo. Višina elementa na popupu je približno enaka
// višini knofa. Gumbi so načeloma kvadratni, zato je višina enaka širini.
// If the popup menu is taller than 80% of the player, we resize it. height of an element in the popup is roughly
// equal to the height of a button. Buttons are generally squares, so width is equal to heigth. (And if it's not,
// that's still close enough for us!)
var smenu _default _h = button _width * smenu _el . length ;
if ( smenu _max _h < smenu _default _h ) {
var scale _factor = smenu _max _h / smenu _default _h ;
2016-12-02 23:30:21 +01:00
2017-01-02 12:29:43 +01:00
smenu _item _width *= scale _factor ;
smenu _item _fontSize *= scale _factor ;
smenu _item _height = button _width * scale _factor ;
smenu _ar _item _width *= scale _factor ;
2016-11-15 22:28:39 +01:00
2016-10-20 23:34:45 +02:00
}
2017-01-02 12:29:43 +01:00
settings _menu . style . bottom = ( button _width * 1.5 ) + "px" ;
settings _menu . style . width = smenu _item _width + "px" ;
settings _menu . style . fontSize = smenu _item _fontSize + "px" ;
smenu _ar _menu . style . right = smenu _item _width + "px" ;
smenu _ar _menu . style . width = smenu _ar _item _width + "px" ;
smenu _ar _menu . style . bottom = "0px" ;
for ( var i = 0 ; i < smenu _el . length ; i ++ ) {
smenu _el [ i ] . style . width = smenu _item _width + "px" ;
smenu _el [ i ] . style . height = smenu _item _height + "px" ;
smenu _el [ i ] . style . fontSize = smenu _item _fontSize + "px" ;
}
for ( var i = 0 ; i < smenu _ar _options . length ; i ++ ) {
smenu _ar _options [ 0 ] . height = smenu _item _height + "px" ;
}
// Tukaj se določa notranji HTML knofov
// Inner HTML of elements is defined here
2017-01-15 18:56:36 +01:00
smenu _el [ 6 ] . textContent = "Force aspect ratio" ;
2017-01-02 12:29:43 +01:00
smenu _el [ 6 ] . appendChild ( smenu _ar _menu ) ;
2017-01-15 18:56:36 +01:00
smenu _el [ 0 ] . textContent = "Settings" ;
2017-01-02 12:29:43 +01:00
2017-01-15 18:56:36 +01:00
smenu _ar _options [ 0 ] . textContent = "4:3" ;
smenu _ar _options [ 1 ] . textContent = "16:10" ;
smenu _ar _options [ 2 ] . textContent = "16:9" ;
smenu _ar _options [ 3 ] . textContent = "21:9" ;
2017-01-02 12:29:43 +01:00
2017-01-15 18:56:36 +01:00
smenu _el [ 5 ] . textContent = "Fit width" ;
smenu _el [ 4 ] . textContent = "Fit height" ;
smenu _el [ 3 ] . textContent = "Reset" ;
smenu _el [ 1 ] . textContent = "Zoom in" ;
smenu _el [ 2 ] . textContent = "Zoom out" ;
2017-01-02 12:29:43 +01:00
// Pritisneš gumb, nekej zakon se more narest.
// — Bioware
// ( https://www.youtube.com/watch?v=hMcVZQI6ybw | [NVZD] )
//
// Press the button, something awesome has to happen.
// — Bioware
// ( https://www.youtube.com/watch?v=hMcVZQI6ybw | [NSFW] )
$ ( smenu _el [ 6 ] ) . on ( "mouseenter" , function ( ) { showMenu ( "uw-armenu" ) } ) ;
$ ( smenu _el [ 6 ] ) . on ( "mouseleave" , function ( ) { hideMenu ( "uw-armenu" ) } ) ;
// event.stopPropagation, ker nočemo togglati še funkcij od knofa za popup z nastavitvami
// event.stopPropagation, because we don't want to trigger onclick functions of the settings popup button in
// the player bar
smenu _ar _options [ 0 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "char" , ( 4 / 3 ) ) ; } ;
smenu _ar _options [ 1 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "char" , ( 16 / 10 ) ) ; } ;
smenu _ar _options [ 2 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "char" , ( 16 / 9 ) ) ; } ;
smenu _ar _options [ 3 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "char" , ( 21 / 9 ) ) ; } ;
// smenu_el[0].onclick = function (event) {event.stopPropagation(); showSettings() };
smenu _el [ 5 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "fit" , "fitw" ) } ;
smenu _el [ 4 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "fit" , "fith" ) } ;
smenu _el [ 3 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "reset" , "reset" ) } ;
smenu _el [ 1 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "fit" , "zoom" ) } ;
smenu _el [ 2 ] . onclick = function ( event ) { event . stopPropagation ( ) ; changeCSS ( "fit" , "unzoom" ) } ;
2016-10-22 20:49:15 +02:00
2016-10-23 20:47:14 +02:00
2016-10-20 23:34:45 +02:00
if ( debugmsg )
console . log ( "uw::addCtlButtons | buttons added" ) ;
2016-12-21 23:25:05 +01:00
return true ;
2016-10-20 23:34:45 +02:00
}
2017-01-03 20:56:01 +01:00
function updateCtlButtonSize ( ) {
// Gumb za nastavitve je bolj kot ne vselej prisoten, zato širino tega gumba uporabimo kot širino naših gumbov
// Settings button is more or less always there, so we use its width as width of our buttons
try {
// Na različnih straneh širino gumba poberemo na različne načine.
if ( button _size _base == "y" )
var button _width = document . getElementsByClassName ( sample _button _class ) [ sample _button _index ] . scrollHeight ;
else
var button _width = document . getElementsByClassName ( sample _button _class ) [ sample _button _index ] . scrollWidth ;
}
catch ( e ) {
// Zato, ker predvajalnik ni vselej prisoten. Če predvajalnik ni prisoten,
// potem tudi knofov ni. Kar pomeni problem.
//
// Because the player isn't always there, and when the player isn't there the buttons aren't, either.
// In that case, the above statement craps out, throws an exception and trashes the extension.
if ( debugmsg )
console . log ( "uw::updateCtlButtonSize | seems there was a fuckup and no buttons were found on this page. No player (and therefore no buttons) found." ) ;
return ;
}
var buttons = document . getElementsByClassName ( "uw-button" ) ;
for ( var i = 0 ; i < buttons . length ; i ++ ) {
buttons [ i ] . style . width = ( button _width * 0.75 ) + "px" ;
buttons [ i ] . style . height = ( button _width ) + "px" ;
buttons [ i ] . style . paddingLeft = ( button _width * 0.15 ) + "px" ;
buttons [ i ] . style . paddingRight = ( button _width * 0.15 ) + "px" ;
}
}
2016-11-29 19:54:05 +01:00
//END UI
2016-12-21 23:25:05 +01:00
//END EXTENSION SETUP
2016-11-29 19:54:05 +01:00
2016-12-07 23:58:43 +01:00
function onOpen ( ) {
if ( debugmsg )
console . log ( "uw | Options page opened" ) ;
2016-11-15 22:28:39 +01:00
}
2016-12-07 23:58:43 +01:00
function onError ( err ) {
if ( debug ) {
console . log ( ` Error: ${ error } ` ) ;
console . log ( "uw | Error opening the page" , err ) ;
}
2016-11-15 22:28:39 +01:00
}
2016-11-29 19:54:05 +01:00
2016-12-07 23:58:43 +01:00
function showSettings ( ) {
2017-01-02 17:45:51 +01:00
2016-12-07 23:58:43 +01:00
}
2016-11-15 22:28:39 +01:00
2016-10-22 21:25:19 +02:00
// Ta funkcija se proži, ko vstopimo ali izstopimo iz celozaslonskega načina
// This function gets triggered by full screen state change
2016-10-22 20:49:15 +02:00
function onFullScreenChange ( ) {
// Popravimo velikost gumbov
// Let's fix the button size:
2017-01-02 12:29:43 +01:00
var button _width = document . getElementsByClassName ( sample _button _class ) [ sample _button _index ] . scrollWidth ;
2016-10-23 20:47:14 +02:00
for ( var i = 5 ; i >= 0 ; i -- ) {
2016-10-22 20:49:15 +02:00
buttons [ i ] . style . width = ( button _width * 0.75 ) + "px" ;
2016-10-23 20:47:14 +02:00
buttons [ i ] . style . paddingLeft = ( button _width * 0.15 ) + "px" ;
buttons [ i ] . style . paddingRight = ( button _width * 0.15 ) + "px" ;
2016-10-22 20:49:15 +02:00
}
2016-10-23 20:47:14 +02:00
document . getElementById ( "uw-smenu" ) . style . bottom = ( button _width * 1.5 ) + "px" ;
2016-12-04 12:01:38 +01:00
//Sedaj poglejmo še, če lahko v nadzorno vrstico spravimo vse gumbe
//Let's see if we can get all the buttons in the control bar
2017-01-02 12:29:43 +01:00
// var rctl = document.getElementsByClassName("ytp-right-controls")[0];
2016-10-22 20:49:15 +02:00
}
2016-10-22 21:25:19 +02:00
// Ta funkcija se proži, ko gremo v celozaslonski način
// This function triggers when we enter fullscreen mode
2016-10-16 13:13:34 +02:00
function onFullscreenOn ( ) {
}
2016-10-22 21:25:19 +02:00
// Ta funkcija se proži, ko gremo ven iz celozaslonskega načina
// This function triggers when we leave fullscreen mode
2016-10-16 13:13:34 +02:00
function onFullscreenOff ( ) {
2016-10-22 21:25:19 +02:00
2016-10-16 13:13:34 +02:00
}
2016-10-23 20:47:14 +02:00
function changeCSS ( type , what _do ) {
2017-01-04 00:07:34 +01:00
console . log ( "uw::changeCSS | starting function" ) ;
2016-10-23 20:47:14 +02:00
hideMenu ( "uw-armenu" ) ;
hideMenu ( "uw-smenu" ) ;
2016-10-20 23:34:45 +02:00
2016-10-16 13:13:34 +02:00
2017-01-04 23:23:41 +01:00
var evideo = $ ( "video" ) [ 0 ] ;
2017-01-04 00:07:34 +01:00
2017-01-04 23:23:41 +01:00
if ( ! evideo ) {
if ( debugmsg )
console . log ( "uw::changeCSS | no video element found. Doing nothing." ) ;
return ;
}
2016-10-16 13:13:34 +02:00
2017-01-14 15:48:46 +01:00
var video = { width : evideo . videoWidth , height : evideo . videoHeight } ;
var nplayer = { width : player . clientWidth , height : player . clientHeight } ;
2016-10-18 23:07:28 +02:00
2017-01-04 00:07:34 +01:00
if ( debugmsg )
console . log ( "uw::changeCSS | video dimensions:" , video . width , "x" , video . height , "; player:" , nplayer . width , "x" , nplayer . height ) ;
2016-10-18 23:07:28 +02:00
// Youtube predvajalnik privzeto resetira CSS ob prehodu v/iz fullscreen. Tukaj shranimo zadnje dejanje,
// da ga lahko onFullscreenOff/onFullscreenOn uveljavita.
//
// Youtube player resets CSS on fullscreen state change. Here we save the last action taken, so
// onFullscreenOff/onFullscreenOn are able to preserve it (if we want).
2017-01-04 00:07:34 +01:00
last _whatdo = { type : type , what _do : what _do } ;
2016-10-18 23:07:28 +02:00
// -----------------------------------------------------------------------------------------
// Handlanje dejanj se zgodi pod to črto
//
// Handling actions happens below this line
// -----------------------------------------------------------------------------------------
2016-10-23 20:47:14 +02:00
if ( type == "char" ) {
2017-01-04 00:07:34 +01:00
2017-01-02 12:29:43 +01:00
if ( debugmsg )
console . log ( "uw::changeCSS | trying to change aspect ratio." ) ;
2017-01-04 00:07:34 +01:00
2016-10-23 20:47:14 +02:00
// char = CHange Aspect Ratio
2017-01-02 12:29:43 +01:00
char ( what _do , video , nplayer ) ;
2016-10-18 23:07:28 +02:00
return ;
}
if ( what _do == "reset" ) {
2017-01-04 00:07:34 +01:00
2016-10-18 23:07:28 +02:00
if ( debugmsg )
console . log ( "uw::changeCSS | issuing reset." ) ;
2016-10-16 13:13:34 +02:00
2017-01-02 12:29:43 +01:00
resetCSS ( video , nplayer ) ;
2016-10-16 13:13:34 +02:00
return ;
}
// Velikost videa spreminjamo samo, če smo v celozaslonskem načinu ALI če NE pišemo komentarja
// Videa ne spreminjamo tudi, če uporabljamo vrstico za iskanje.
//
// We only change video size when we're in full screen OR if we are NOT writing a comment.
// We also leave video alone if we're using the search bar
if ( inFullScreen || (
( document . activeElement . getAttribute ( "role" ) != "textbox" ) &&
( document . activeElement . getAttribute ( "type" ) != "text" )
2017-01-02 12:29:43 +01:00
) ) {
if ( debugmsg )
console . log ( "uw::changeCSS | trying to fit width or height" ) ;
2017-01-04 00:07:34 +01:00
2017-01-02 12:29:43 +01:00
changeCSS _nofs ( what _do , video , nplayer ) ;
}
2016-10-16 13:13:34 +02:00
}
2016-10-23 20:47:14 +02:00
function char ( new _ar , video , player ) {
// Kot vhodni argument dobimo razmerje stranic. Problem je, ker pri nekaterih ločljivostih lahko razmerje stranic
// videa/našega zaslona minimalno odstopa od idealnega razmerja — npr 2560x1080 ni natanko 21:9, 1920x1080 ni
// natanko 16:9. Zato ob podanem razmerju stranic izračunamo dejansko razmerje stranic.
//
// The aspect ratio we get as an argument is an ideal aspect ratio. Some (most) resolutions' aspect ratios differ
// from that ideal aspect ratio (by a minimal amount) — e.g. 2560x1080 isn't exactly 21:9, 1920x1080 isn't exactly
// 16:9. What is more, both 3440x1440 and 2560x1080 are considered "21:9", but their aspect ratios are slightly
// different. This has the potential to result in annoying black bars, so we correct the aspect ratio we're given
// to something that's slightly more correct.
var ar ;
var res _219 = [ [ 2560 , 1080 ] , [ 3440 , 1440 ] ] ;
var res _169 = [ [ 1920 , 1080 ] , [ 1280 , 720 ] , [ 1366 , 768 ] ] ;
if ( new _ar == ( 21 / 9 ) ) {
for ( var i = 0 ; i < res _219 . length ; i ++ ) {
if ( player . height == res _219 [ i ] [ 1 ] ) {
ar = res _219 [ i ] [ 0 ] / res _219 [ i ] [ 1 ] ;
set _video _ar ( ar , video , player ) ;
return ;
}
}
}
else if ( new _ar == ( 16 / 9 ) ) {
for ( var i = 0 ; i < res _169 . length ; i ++ ) {
if ( player . height == res _169 [ i ] [ 1 ] ) {
ar = res _169 [ i ] [ 0 ] / res _169 [ i ] [ 1 ] ;
set _video _ar ( ar , video , player ) ;
return ;
}
}
}
set _video _ar ( new _ar , video , player ) ;
}
2016-10-18 23:07:28 +02:00
/ * T u k a j p o v e m o , k a k š n o r a z m e r j e s t r a n i c i m a v i d e o .
// Kaj to pomeni:
// Mi rečemo, da ima video razmerje stranic 16:9. Dejanski video
// ima razmerje 4:3. To pomeni, da ima video zgoraj in spodaj črno
// obrobo, ki je nočemo, zato video povečamo toliko, da se ta obroba odreže.
2016-10-23 20:47:14 +02:00
//
// OBROB TUKAJ NE DODAJAMO.
2016-10-18 23:07:28 +02:00
//
// With this function, we specify the aspect ratio of the video.
// What does this mean?
// If we specify that the aspect ratio of a video is 16:9 when video is
// actually 4:3, that means the video has black bars above and below.
// We zoom the video just enough for the black lines to disappear.
2016-10-23 20:47:14 +02:00
//
// WE DO NOT ADD ANY BLACK BORDERS. If we get to a scenario when we'd have to add
// black borders, we do nothing instead.
2016-10-18 23:07:28 +02:00
* /
function set _video _ar ( aspect _ratio , video , player ) {
var video _ar = video . width / video . height ;
var display _ar = player . width / player . height ;
2016-12-08 22:34:32 +01:00
if ( debugmsg ) {
2016-10-18 23:07:28 +02:00
console . log ( "uw::set_video_ar | aspect ratio: " + aspect _ratio + "; video_ar: " + video _ar + "; display_ar: " + display _ar ) ;
2016-10-23 20:47:14 +02:00
console . log ( "uw::set_video_ar | player dimensions: " + player . width + "x" + player . height + "; video dimensions: " + video . width + "x" + video . height ) ;
2016-12-08 22:34:32 +01:00
}
2016-10-18 23:07:28 +02:00
if ( aspect _ratio * 1.1 > video _ar && video _ar > aspect _ratio * 0.9 ) {
// Ta hack nas reši problema, ki ga predstavlja spodnji if stavek — če se legit 21:9 videu na 16:9 monitorju
// obreže na 16:9, potem ga s klicem te funkcije ne moremo spremeniti nazaj na 21:9. Vendar pa bi za tak primer
// radi imeli izjemo.
//
// This hack solves the problem that the bottom if statement presents. If we crop a 21:9 video on a 16:9 monitor,
// we can't change it back to 21:9 in this function, even though we kinda want that to happen — so we add an
// exception.
if ( debugmsg )
console . log ( "uw::set_video_ar | ar matches our display ar. resetting" ) ;
resetCSS ( video , player ) ;
return ;
}
2016-12-04 23:02:15 +01:00
// Širina, višina, top, left za nov video
// Width, height, top and left for the new video
2016-10-18 23:07:28 +02:00
var nv = { "w" : 0 , "h" : 0 , "top" : 0 , "left" : 0 } ;
2016-10-16 13:13:34 +02:00
/ *
2016-10-18 23:07:28 +02:00
// Video hočemo pretvoriti v video z drugačnim razmerjem stranic.
// To storimo tako, da širino videa nastavimo relativno na višino prikazovalnika, torej:
//
// širina = višina_prikazovalnika * razmerje_stranic
// višina = širina / video_ar
//
//
//
// ----------------------------------------------------------------------------------------------
//
// In this case, the video is narrower than we want (think 4:3, which we want to make into 16:9)
// We achieve this by setting video width relative to the display width, so:
//
// width = display_height * aspect_ratio
// height = width / video_ar
//
* /
if ( video _ar <= aspect _ratio ) {
if ( debugmsg ) {
console . log ( "uw::set_video_ar | reached pre-calc. Video is taller than ar. target ar: " + aspect _ratio ) ;
}
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
nv . w = player . height * aspect _ratio ;
nv . h = nv . w / video _ar ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
nv . top = ( player . height - nv . h ) / 2 ;
nv . left = ( player . width - nv . w ) / 2 ;
}
else {
if ( debugmsg ) {
console . log ( "uw::set_video_ar | reached pre-calc. Video is wider than ar. target ar: " + aspect _ratio ) ;
}
nv . h = player . width / aspect _ratio ;
nv . w = nv . h * video _ar ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
nv . top = ( player . height - nv . h ) / 2 ;
nv . left = ( player . width - nv . w ) / 2 ;
}
2016-10-23 20:47:14 +02:00
if ( nv . w > ( player . width * 1.1 ) && nv . h > ( player . height * 1.1 ) )
return ;
2016-10-18 23:07:28 +02:00
applyCSS ( nv ) ;
}
function resetCSS ( video , player ) {
if ( debugmsg )
console . log ( "uw::resetCSS | resetting video size" ) ;
var nv = { "w" : 0 , "h" : 0 , "top" : 0 , "left" : 0 } ;
var vidaspect = video . width / video . height ;
var scraspect = player . width / player . height ;
if ( vidaspect > scraspect ) { // Video je širši od okna | video is wider than window
nv . w = player . width ;
nv . h = player . width / video . width * video . height ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
// Lahko se zgodi, da je prišlo do zaokroževalne napake ter da je dejanska višina videa le nekaj pikslov drugačna,
// kot višina predvajalnika. V tem primeru zavržemo prej dobljeni rezultat in namesto tega privzamemo, da je višina
// videa enaka višini predvajalnika.
//
// It's possible to have a rounding error where calculated height of the video is only a few pixels different from
// the player height. In such cases, we discard the calculated video height and use player height instead.
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
if ( player . height - 4 < nv . h && nv . h < player . height + 4 )
nv . h = player . height ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
nv . top = ( player . height - nv . h ) / 2 ;
nv . left = 0 ;
}
else {
nv . h = player . height ;
nv . w = player . height / video . height * video . width ;
if ( player . width - 4 < nv . w && nv . w < player . width + 4 )
nv . w = player . width ;
nv . top = 0 ; //itak zasedemo 100% višine
nv . left = ( player . width - nv . w ) / 2 ;
2016-10-16 13:13:34 +02:00
}
2016-10-18 23:07:28 +02:00
applyCSS ( nv ) ;
}
function changeCSS _nofs ( what _do , video , player ) {
2017-01-02 12:29:43 +01:00
if ( debugmsg ) {
console . log ( "uw::changeCSS_nofs | arguments: what_do:" , what _do , "; video:" , video , "; player:" , player ) ;
}
2016-10-18 23:07:28 +02:00
var w ;
var h ;
var top ;
var left ;
2017-01-02 12:29:43 +01:00
var evideo = $ ( "video" ) [ 0 ] ;
2017-01-14 11:03:53 +01:00
var video = { width : evideo . videoWidth , height : evideo . videoHeight , scrollw : evideo . scrollWidth , scrollh : evideo . scrollWidth } ;
2017-01-02 12:29:43 +01:00
var ar = video . width / video . height ;
if ( debugmsg ) {
console . log ( "uw::changeCSS_nofs | video dimensions:" , video . width , "x" , video . height , "; ar:" , ar ) ;
}
2016-10-18 23:07:28 +02:00
2016-11-15 22:28:39 +01:00
if ( what _do == "fitw" || what _do == "fit-width" ) {
2016-10-16 13:13:34 +02:00
// Ker bi bilo lepo, da atribut 'top' spremenimo hkrati z width in height, moramo najprej naračunati,
// za kakšen faktor se poviša višina. To potrebujemo, da se pravilno izračuna offset.
//
// 100vw = window.innerWidth
2016-10-18 23:07:28 +02:00
// window.innerWidth / videoWidth = x
2016-10-16 13:13:34 +02:00
//
// Če pomnožimo videoHeight z x, dobimo novo višino videa. Nova višina videa je lahko večja ali manjša
// kot višina ekrana. Če je višina videa manjša kot višina ekrana, bo top pozitiven, drugače negativen:
//
2016-10-18 23:07:28 +02:00
// nvideoh = x * videoWidth
// top = (window.innerHeight - nvideoh) / 2
2016-10-16 13:13:34 +02:00
//
// Z 2 delimo, ker hočemo video vertikalno poravnati.
2016-10-18 23:07:28 +02:00
w = player . width ;
h = player . width / video . width * video . height ;
2016-10-16 13:13:34 +02:00
2017-01-04 00:07:34 +01:00
if ( debugmsg )
console . log ( "uw::changeCSS_nofs | w:" , w , "; h:" , h ) ;
2016-10-18 23:07:28 +02:00
top = ( player . height - h ) / 2 ;
2016-10-16 13:13:34 +02:00
left = 0 ; // Ker zavzamemo vso širino | cos we take up all the width
}
2016-11-15 22:28:39 +01:00
if ( what _do == "fith" || what _do == "fit-height" ) {
2016-10-18 23:07:28 +02:00
h = player . height ;
w = player . height / video . height * video . width ;
2016-10-16 13:13:34 +02:00
top = 0 ; //itak zasedemo 100% višine
2016-10-18 23:07:28 +02:00
left = ( player . width - w ) / 2 ;
2016-10-16 13:13:34 +02:00
}
if ( what _do == "zoom" ) {
// Video povečujemo na tak način, da sta zoom in unzoom povečata oz. zmanjšata video za enak korak
// We do this so zoom and unzoom steps change video sizes for the same amount
2017-01-14 15:17:26 +01:00
h = video . scrollh + ( player . height * zoomStep ) ;
w = video . scrollw + ( player . height * zoomStep * ar ) ;
2016-10-16 13:13:34 +02:00
/ * Z a k a j r a č u n a m o š i r i n o n a t a k n a č i n ?
//
// Predstavljajte si, da imamo 2100:900 video v 1600:900 škatli, zoomStep = 0.1. Če bi širino računali po formuli:
//
// širina = širina_videa + (širina zaslona * zoomStep)
//
// Potem bi bila nova velikost videa 2260 x 990. Razmerje stranic: 2.28 (moglo bi biti 2.33 — video je popačen).
// Zaradi tega novo širino rajši povečamo za razliko_v_višini * razmerje_stranic
//
// 2100 + (900 * 0.1 * (2100/900)) =
// 2100 + (90 * 2.333) = 2310
//
// Razmerje stranic (2310x990) je tako 2.333 — tako, kot bi moglo biti.
//
//
// ============================================================================================================
//
// Why did we calculate width this way?
//
// Imagine we have a 2100x900 video in a 1600:900 container, zoomStep = 0.1. If we calculated width using this:
//
// width = video_width + (container_width * zoomStep)
//
// then the new size would be 2260 x 990. This gives us an aspect ratio of 2.28 instead of 2.33 (which is what it
// should be). Because of that we rather increase the width by delta_height * aspect_ratio:
//
// 2100 + (900 * 0.1 * (2100/900)) =
// 2100 + (90 * 2.333) = 2310
//
// This gives us the correct aspect ratio and prevents video deformations.
* /
2016-10-18 23:07:28 +02:00
top = ( player . height - h ) / 2
left = ( player . width - w ) / 2 ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
if ( h > player . height * 4 ) {
2016-11-15 22:28:39 +01:00
if ( debugmsg ) {
console . log ( "But this video is ... I mean, it's fucking huge. This is bigger than some rooms, this is bigger than some people's flats!" ) ;
// Insert obligatory omnishambles & coffee machine quote here
console . log ( "(No really, mate, you took this way too far already. Can't let you do that, Dave.)" ) ;
}
2016-10-16 13:13:34 +02:00
return ;
}
}
if ( what _do == "unzoom" ) {
// Video povečujemo na tak način, da sta zoom in unzoom povečata oz. zmanjšata video za enak korak
// We do this so zoom and unzoom steps change video sizes for the same amount
2017-01-14 11:03:53 +01:00
h = video . scrollh - ( player . height * zoomStep ) ;
w = video . scrollw - ( player . height * zoomStep * ar ) ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
top = ( player . height - h ) / 2
left = ( player . width - w ) / 2 ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
if ( h < player . height * 0.25 ) {
2016-11-15 22:28:39 +01:00
if ( debugmsg ) {
console . log ( "don't you think this is small enough already? You don't need to resize the video all the way down to the size smaller than your penis." ) ;
console . log ( "(if you're a woman, substitute 'penis' with whatever the female equivalent is.)" ) ;
}
2016-10-16 13:13:34 +02:00
return ;
}
}
2016-10-18 23:07:28 +02:00
var dimensions = { h : h , w : w , top : top , left : left } ;
applyCSS ( dimensions ) ;
}
function applyCSS ( dimensions ) {
dimensions . top += "px" ;
dimensions . left += "px" ;
dimensions . w += "px" ;
dimensions . h += "px" ;
$ ( "video" ) . css ( { "width" : dimensions . w , "height" : dimensions . h , "top" : dimensions . top , "left" : dimensions . left } ) ;
2016-10-16 13:13:34 +02:00
2016-10-18 23:07:28 +02:00
if ( debugmsg )
2017-01-02 12:29:43 +01:00
console . log ( "uw::applycss | css applied. Dimensions/pos: w:" , dimensions . w , "; h:" , dimensions . h , "; top:" , dimensions . top , "; left:" , dimensions . left ) ;
2016-10-16 13:13:34 +02:00
}
2016-10-18 23:07:28 +02:00
function inIframe ( ) {
2016-10-16 13:13:34 +02:00
try {
return window . self !== window . top ;
} catch ( e ) {
return true ;
}
}
2016-10-22 20:49:15 +02:00
2016-12-04 23:02:15 +01:00
function resourceToUrl ( img ) {
2016-10-22 20:49:15 +02:00
return chrome . extension . getURL ( img ) ;
}
2016-10-23 20:47:14 +02:00
function showMenu ( id ) {
if ( debugmsg )
2017-01-15 18:56:36 +01:00
console . log ( "uw::showMenu | showing menu with id " , id , "\n\n" , document . getElementById ( id ) ) ;
2016-12-02 23:30:21 +01:00
document . getElementById ( id ) . classList . add ( "show" ) ;
}
function toggleMenu ( id ) {
if ( debugmsg )
2017-01-15 18:56:36 +01:00
console . log ( "uw::toggleMenu | toggling menu with id" , id , "\n\n" , document . getElementById ( id ) ) ;
2016-10-23 20:47:14 +02:00
document . getElementById ( id ) . classList . toggle ( "show" ) ;
}
function hideMenu ( id ) {
2016-12-02 23:30:21 +01:00
if ( debugmsg )
console . log ( "uw::hideMenu | hiding menu with id " + id ) ;
2017-01-04 00:07:34 +01:00
if ( document . getElementById ( id ) ) //Safety check in case there's no element with such id
document . getElementById ( id ) . classList . remove ( "show" ) ;
2016-10-23 20:47:14 +02:00
}