2017-12-29 23:34:40 +01:00
if ( Debug . debug )
console . log ( "[popup.js] loading popup script!" ) ;
2017-12-30 02:58:24 +01:00
document . getElementById ( "uw-version" ) . textContent = browser . runtime . getManifest ( ) . version ;
2017-12-29 23:34:40 +01:00
2017-12-30 18:36:08 +01:00
2018-08-30 00:56:15 +02:00
var selectedMenu = "" ;
2018-11-11 22:35:08 +01:00
var selectedSubitem = {
'siteSettings' : undefined ,
'videoSettings' : undefined ,
}
2017-12-30 02:58:24 +01:00
var hasVideos = false ;
2017-12-29 23:34:40 +01:00
2018-09-23 00:38:36 +02:00
var zoom _videoScale = 1 ;
2017-12-30 18:36:08 +01:00
var _config ;
2018-07-09 23:30:11 +02:00
var _changeAr _button _shortcuts = { "autoar" : "none" , "reset" : "none" , "219" : "none" , "189" : "none" , "169" : "none" , "custom" : "none" }
2017-12-30 18:36:08 +01:00
2018-05-27 01:29:02 +02:00
var comms = new Comms ( ) ;
2018-08-30 00:56:15 +02:00
var settings = new Settings ( undefined , ( ) => updateConfig ( ) ) ;
var site = undefined ;
2018-08-29 21:23:08 +02:00
2018-11-18 23:54:54 +01:00
// aside from hostname, this can have two additional values:
// __playing — commands get sent to all frames with videos of status 'playing'
// __all — commands get sent to all frames
var selectedFrame = '__playing' ;
2018-05-27 01:29:02 +02:00
var port = browser . runtime . connect ( { name : 'popup-port' } ) ;
port . onMessage . addListener ( ( m , p ) => processReceivedMessage ( m , p ) ) ;
2018-01-08 22:48:45 +01:00
2018-11-09 00:35:18 +01:00
var _video _settings _tab _items = [ ] ;
2018-08-29 21:23:08 +02:00
2018-11-22 22:55:22 +01:00
var selectedSubitemLoaded = false ;
2018-11-11 22:35:08 +01:00
//#region build ui
var tablist = {
'extensionSettings' : new MenuItem ( '_menu_item_settings_ext' , 'Extension settings' , '' , ( ) => showMenu ( 'extensionSettings' ) ) ,
'siteSettings' : new MenuItem ( '_menu_item_settings_site' , 'Site settings' , 'Settings for current site' , ( ) => showMenu ( 'siteSettings' ) ) ,
'videoSettings' : new MenuItem ( '_menu_item_settings_video' , 'Video settings' , 'Crop & stretch options for videos on current page' , ( ) => showMenu ( 'videoSettings' ) ) ,
'about' : new MenuItem ( '_menu_item_about' , 'About Ultrawidify' , '' , ( ) => showMenu ( 'about' ) )
} ;
for ( let t in tablist ) {
tablist [ t ] . appendTo ( document . getElementById ( 'tablist' ) ) ;
}
function loadFrames ( videoTab ) {
tablist [ 'siteSettings' ] . removeSubitems ( ) ;
tablist [ 'videoSettings' ] . removeSubitems ( ) ;
2018-11-22 22:55:22 +01:00
console . log ( "VIDEO TAB" , videoTab )
if ( ! selectedSubitemLoaded ) {
if ( videoTab . selected ) {
selectedSubitem = videoTab . selected ;
selectedSubitemLoaded = true ;
}
}
2018-11-18 23:54:54 +01:00
function onTabitemClick ( item ) {
tablist [ selectedMenu ] . selectSubitem ( item ) ;
selectedSubitem [ selectedMenu ] = item ;
2018-11-22 22:55:22 +01:00
port . postMessage ( { cmd : 'select-tab' , selectedMenu : selectedMenu , selectedSubitem : item } ) ;
2018-11-18 23:54:54 +01:00
}
for ( var option of [ { id : '__playing' , label : 'Currently playing' } , { id : '__all' , label : 'All' } ] ) {
const id = option . id ;
var newItem = new TabItem (
undefined ,
option . id ,
option . label ,
false ,
( ) => onTabitemClick ( id )
) ;
tablist [ 'siteSettings' ] . insertSubitem ( newItem ) ;
tablist [ 'videoSettings' ] . insertSubitem ( newItem ) ;
}
2018-11-11 22:35:08 +01:00
for ( var frame in videoTab . frames ) {
2018-11-18 23:54:54 +01:00
const nid = ` ${ videoTab . id } - ${ videoTab . frames [ frame ] . id } ` ;
2018-11-11 22:35:08 +01:00
var newItem = new TabItem (
undefined ,
nid ,
videoTab . frames [ frame ] . host ,
videoTab . frames [ frame ] . url != videoTab . url ,
2018-11-18 23:54:54 +01:00
( click ) => onTabitemClick ( nid )
) ;
2018-11-11 22:35:08 +01:00
tablist [ 'siteSettings' ] . insertSubitem ( newItem ) ;
tablist [ 'videoSettings' ] . insertSubitem ( newItem ) ;
}
2018-11-22 22:55:22 +01:00
console . log ( "TIME TO SELECT SUBITEM" , selectedSubitem , "\nexists subitem in site settings/video settings?" , selectedSubitem && tablist [ 'siteSettings' ] . existsSubitem ( selectedSubitem . siteSettings ) , selectedSubitem && tablist [ 'videoSettings' ] . existsSubitem ( selectedSubitem . videoSettings ) )
if ( ! selectedSubitem . siteSettings || ! tablist [ 'siteSettings' ] . existsSubitem ( selectedSubitem . siteSettings ) ) {
2018-11-21 20:41:15 +01:00
selectedSubitem [ 'siteSettings' ] = tablist [ 'siteSettings' ] . selectFirstSubitem ( ) ;
2018-11-11 22:35:08 +01:00
} else {
tablist [ 'siteSettings' ] . selectSubitem ( selectedSubitem . siteSettings )
}
2018-11-22 22:55:22 +01:00
if ( ! selectedSubitem . videoSettings || ! tablist [ 'videoSettings' ] . existsSubitem ( selectedSubitem . videoSettings ) ) {
2018-11-21 20:41:15 +01:00
selectedSubitem [ 'videoSettings' ] = tablist [ 'videoSettings' ] . selectFirstSubitem ( ) ;
2018-11-11 22:35:08 +01:00
} else {
tablist [ 'videoSettings' ] . selectSubitem ( selectedSubitem . videoSettings ) ;
}
}
//#endregion
2018-05-27 01:29:02 +02:00
async function processReceivedMessage ( message , port ) {
2018-09-18 23:48:05 +02:00
if ( Debug . debug ) {
console . log ( "[popup.js] received message" , message )
}
2018-08-30 00:56:15 +02:00
if ( message . cmd === 'set-current-site' ) {
2018-11-09 00:57:05 +01:00
if ( site ) {
if ( ! site . host ) {
2018-11-09 00:57:47 +01:00
// dunno why this fix is needed, but sometimes it is
2018-11-09 00:57:05 +01:00
site . host = site . tabHostname ;
}
}
if ( ! site || site . host !== message . site . host ) {
2018-09-21 00:26:08 +02:00
port . postMessage ( { cmd : 'get-current-zoom' } ) ;
}
2018-08-30 00:56:15 +02:00
site = message . site ;
2018-11-18 03:29:16 +01:00
loadConfig ( site . host ) ;
loadFrames ( site ) ;
2018-09-21 00:26:08 +02:00
} else if ( message . cmd === 'set-current-zoom' ) {
setCurrentZoom ( message . zoom ) ;
2018-08-30 00:56:15 +02:00
}
}
2018-08-21 23:48:47 +02:00
2018-08-30 00:56:15 +02:00
async function updateConfig ( ) {
2018-09-18 23:48:05 +02:00
if ( Debug . debug ) {
2018-11-09 00:57:05 +01:00
console . log ( "[popup.js] settings changed. updating popup if site exists. Site:" , site . host ) ;
2018-09-18 23:48:05 +02:00
}
2018-11-09 00:57:05 +01:00
if ( site && site . host ) {
loadConfig ( site . host ) ;
2018-05-27 01:29:02 +02:00
}
2018-01-10 23:16:07 +01:00
}
2018-09-21 00:26:08 +02:00
async function setCurrentZoom ( scale ) {
2018-09-23 00:38:36 +02:00
zoom _videoScale = scale ;
2018-09-21 00:26:08 +02:00
if ( Debug . debug ) {
2018-09-23 00:38:36 +02:00
console . log ( "[popup.js::setCurrentZoom] we're setting zoom:" , zoom _videoScale ) ;
2018-09-21 00:26:08 +02:00
}
2018-09-23 00:38:36 +02:00
VideoPanel . inputs . zoomSlider . value = Math . log2 ( zoom _videoScale ) ;
VideoPanel . labels . zoomLevel . textContent = ( zoom _videoScale * 100 ) . toFixed ( ) ;
2018-09-21 00:26:08 +02:00
}
2018-02-02 00:21:29 +01:00
function hideWarning ( warn ) {
2018-11-11 22:35:08 +01:00
// document.getElementById(warn).classList.add("hidden");
2018-02-02 00:21:29 +01:00
}
2018-01-02 03:36:29 +01:00
function stringToKeyCombo ( key _in ) {
var keys _in = key _in . split ( "_" ) ;
var keys _out = "" ;
for ( key of keys _in ) {
if ( key == "ctrlKey" )
keys _out += "ctrl + " ;
else if ( key == "shiftKey" )
keys _out += "shift + " ;
else if ( key == "altKey" )
keys _out += "alt + " ;
else
keys _out += key ;
}
return keys _out ;
}
2018-09-16 14:14:16 +02:00
function configurePopupTabs ( site ) {
2018-11-18 23:54:54 +01:00
if ( ! selectedMenu ) {
showMenu ( 'videoSettings' ) ;
} else {
showMenu ( selectedMenu ) ;
}
2018-11-18 03:29:16 +01:00
return ;
2018-11-11 22:35:08 +01:00
// todo: this can potentially be removed
2018-09-16 14:14:16 +02:00
// Determine which tabs can we touch.
// If extension is disabled, we can't touch 'site settings' and 'video settings'
// If extension is enabled, but site is disabled, we can't touch 'video settings'
var extensionEnabled = settings . extensionEnabled ( ) ;
var extensionEnabledForSite = settings . extensionEnabledForSite ( site ) ;
2018-08-29 21:23:08 +02:00
2018-09-19 23:34:47 +02:00
2018-11-11 22:35:08 +01:00
if ( extensionEnabledForSite || extensionEnabled ) {
tablist [ 'videoSettings' ] . enable ( ) ;
2018-08-30 00:56:15 +02:00
} else {
2018-11-11 22:35:08 +01:00
tablist [ 'videoSettings' ] . disable ( ) ;
}
2018-08-30 00:56:15 +02:00
2018-11-11 22:35:08 +01:00
// if popup isn't being opened for the first time, there's no reason to switch
// we're already in this tab
if ( ! selectedMenu ) {
showMenu ( 'videoSettings' ) ;
2018-08-30 00:56:15 +02:00
}
2018-09-16 14:14:16 +02:00
}
2018-05-27 01:29:02 +02:00
2018-11-18 03:29:16 +01:00
function basicCommandHandler ( cmdArray ) {
for ( cmd of cmdArray ) {
port . postMessage ( {
cmd : cmd . action ,
2018-11-21 20:41:15 +01:00
arg : cmd . arg ,
targetFrame : selectedSubitem [ selectedMenu ]
2018-11-18 03:29:16 +01:00
} ) ;
}
2018-11-18 12:41:40 +01:00
}
function buildKeyboardShortcutString ( keypress ) {
var shortcutCombo = '' ;
if ( keypress . ctrlKey ) {
shortcutCombo += 'Ctrl + ' ;
}
if ( keypress . shiftKey ) {
shortcutCombo += 'Shift + ' ;
}
if ( keypress . metaKey ) {
shortcutCombo += 'Meta + ' ;
}
if ( keypress . altKey ) {
shortcutCombo += 'Alt + ' ;
}
shortcutCombo += keypress . key . toUpperCase ( ) ;
2018-11-18 03:29:16 +01:00
2018-11-18 12:41:40 +01:00
return shortcutCombo ;
}
function processButtonsForPopupCategory ( category , buttons ) {
if ( buttons . length === 0 ) {
category . container . hide ( ) ;
} else {
category . buttonContainer . removeChildren ( ) ;
category . container . show ( ) ;
category . buttonContainer . show ( ) ;
for ( var button of buttons ) {
var shortcutCombo = '' ;
if ( button . shortcut && button . shortcut [ 0 ] . key ) {
shortcutCombo = buildKeyboardShortcutString ( button . shortcut [ 0 ] ) ;
}
const cmd = button . cmd ;
var nb = new ShortcutButton (
'' ,
button . label ,
shortcutCombo ,
( ) => basicCommandHandler ( cmd ) ,
[ 'w24' ]
)
nb . appendTo ( category . buttonContainer ) ;
}
}
2018-11-18 03:29:16 +01:00
}
2018-09-16 14:14:16 +02:00
function configureGlobalTab ( ) {
2018-11-11 22:35:08 +01:00
return ; // todo: revisit
2018-09-16 19:28:40 +02:00
if ( Debug . debug ) {
2018-09-16 23:24:01 +02:00
console . log ( "[popup.js] Configuring global tab (ExtPanel)." ,
"\nextension mode: " , settings . active . extensionMode ,
"\narDetect mode: " , settings . active . arDetect . mode ,
2018-09-16 19:28:40 +02:00
"\nvideo float mode:" , settings . active . miscFullscreenSettings . videoFloat ,
2018-09-18 00:40:05 +02:00
"\nstretch mode: " , settings . active . stretch . initialMode ,
2018-09-16 19:28:40 +02:00
"\n.." )
}
2018-09-16 14:14:16 +02:00
for ( var button in ExtPanel . extOptions ) {
ExtPanel . extOptions [ button ] . classList . remove ( "selected" ) ;
2018-06-28 23:43:52 +02:00
}
2018-09-16 14:14:16 +02:00
for ( var button in ExtPanel . arOptions ) {
ExtPanel . arOptions [ button ] . classList . remove ( "selected" ) ;
2018-06-27 23:55:37 +02:00
}
2018-09-16 14:14:16 +02:00
for ( var button in ExtPanel . alignment ) {
ExtPanel . alignment [ button ] . classList . remove ( "selected" ) ;
2018-06-27 23:55:37 +02:00
}
2018-09-18 00:40:05 +02:00
for ( var button in ExtPanel . stretch ) {
ExtPanel . stretch [ button ] . classList . remove ( "selected" ) ;
}
2018-06-27 23:55:37 +02:00
2018-09-16 14:14:16 +02:00
ExtPanel . extOptions [ settings . active . extensionMode ] . classList . add ( "selected" ) ;
ExtPanel . arOptions [ settings . active . arDetect . mode ] . classList . add ( "selected" ) ;
ExtPanel . alignment [ settings . active . miscFullscreenSettings . videoFloat ] . classList . add ( "selected" ) ;
2018-09-18 00:40:05 +02:00
ExtPanel . stretch [ settings . active . stretch . initialMode ] . classList . add ( "selected" ) ;
2018-09-16 14:14:16 +02:00
}
2018-07-09 23:30:11 +02:00
2018-09-16 14:14:16 +02:00
function configureSitesTab ( site ) {
2018-11-11 22:35:08 +01:00
return ; // todo: revisit
2018-09-16 19:28:40 +02:00
if ( Debug . debug ) {
2018-09-16 23:24:01 +02:00
console . log ( "[popup.js] Configuring sites tab (SitePanel)." ,
"\nsite: " , site ,
2018-11-09 00:57:05 +01:00
"\nextension mode: " , settings . active . sites [ site . host ] ? settings . active . sites [ site . host ] . status : 'no site-special settings for this site' ,
"\narDetect mode: " , settings . active . sites [ site . host ] ? settings . active . sites [ site . host ] . arStatus : 'no site-special settings for this site' ,
"\nvideo float mode:" , settings . active . sites [ site . host ] ? settings . active . sites [ site . host ] . videoFloat : 'no site-special settings for this site' ,
"\ndefault ar: " , settings . active . sites [ site . host ] ? settings . active . sites [ site . host ] . ar : 'no site-special settings for this site' ,
"\nstretch mode: " , settings . active . sites [ site . host ] ? settings . active . sites [ site . host ] . stretch : 'no site-special settings for this site' ,
2018-09-16 19:28:40 +02:00
"\n..." )
}
for ( const button in SitePanel . extOptions ) {
SitePanel . extOptions [ button ] . classList . remove ( "selected" ) ;
}
for ( const button in SitePanel . arOptions ) {
SitePanel . arOptions [ button ] . classList . remove ( "selected" ) ;
2018-06-29 00:30:42 +02:00
}
2018-09-16 19:28:40 +02:00
for ( const button in SitePanel . alignment ) {
SitePanel . alignment [ button ] . classList . remove ( "selected" ) ;
}
2018-09-18 00:40:05 +02:00
for ( const button in SitePanel . stretch ) {
SitePanel . stretch [ button ] . classList . remove ( "selected" ) ;
}
2018-09-16 19:28:40 +02:00
2018-11-09 00:57:05 +01:00
if ( settings . active . sites [ site . host ] && settings . active . sites [ site . host ] ) {
2018-09-19 23:34:47 +02:00
console . log ( "settings for" , site , "exist!" )
2018-11-09 00:57:05 +01:00
SitePanel . extOptions [ settings . active . sites [ site . host ] . status ] . classList . add ( "selected" ) ;
SitePanel . arOptions [ settings . active . sites [ site . host ] . arStatus ] . classList . add ( "selected" ) ;
2018-09-19 23:34:47 +02:00
} else {
SitePanel . extOptions . default . classList . add ( "selected" ) ;
SitePanel . arOptions . default . classList . add ( "selected" ) ;
}
2018-09-16 23:24:01 +02:00
// optional settings:
2018-11-09 00:57:05 +01:00
if ( settings . active . sites [ site . host ] && settings . active . sites [ site . host ] . videoAlignment ) {
SitePanel . alignment [ settings . active . sites [ site . host ] . videoAlignment ] . classList . add ( "selected" ) ;
2018-09-16 23:24:01 +02:00
} else {
SitePanel . alignment . default . classList . add ( 'selected' ) ;
}
2018-09-18 00:40:05 +02:00
2018-11-09 00:57:05 +01:00
if ( settings . active . sites [ site . host ] && settings . active . sites [ site . host ] . stretch !== undefined ) { // can be 0
SitePanel . stretch [ settings . active . sites [ site . host ] . stretch ] . classList . add ( "selected" ) ;
2018-09-18 00:40:05 +02:00
} else {
SitePanel . stretch [ '-1' ] . classList . add ( "selected" ) ;
}
2018-09-16 14:14:16 +02:00
}
2018-06-29 00:30:42 +02:00
2018-11-18 03:29:16 +01:00
function configureVideoTab ( site ) {
const popupButtons = settings . getActionsForSite ( site ) . filter ( action => action . popup === true ) ;
const cropButtons = popupButtons . filter ( action => action . cmd . length === 1 && action . cmd [ 0 ] . action === 'set-ar' ) ;
2018-11-18 18:44:44 +01:00
const stretchButtons = popupButtons . filter ( action => action . cmd . length === 1 && action . cmd [ 0 ] . action === 'set-stretch' ) ;
const alignButtons = popupButtons . filter ( action => action . cmd . length === 1 && action . cmd [ 0 ] . action === 'set-alignment' ) ;
2018-11-18 03:29:16 +01:00
2018-11-18 12:41:40 +01:00
processButtonsForPopupCategory ( VideoPanel . elements . cropSettings , cropButtons ) ;
processButtonsForPopupCategory ( VideoPanel . elements . stretchSettings , stretchButtons ) ;
processButtonsForPopupCategory ( VideoPanel . elements . alignmentSettings , alignButtons ) ;
2018-11-18 03:29:16 +01:00
2018-09-16 14:14:16 +02:00
// todo: get min, max from settings
VideoPanel . inputs . zoomSlider . min = Math . log2 ( 0.5 ) ;
VideoPanel . inputs . zoomSlider . max = Math . log2 ( 8 ) ;
2018-09-23 00:38:36 +02:00
VideoPanel . inputs . zoomSlider . value = Math . log2 ( zoom _videoScale ) ;
2018-09-18 00:40:05 +02:00
2018-09-16 14:14:16 +02:00
VideoPanel . inputs . zoomSlider . addEventListener ( 'input' , ( event ) => {
var newZoom = Math . pow ( 2 , VideoPanel . inputs . zoomSlider . value ) ;
2018-09-23 00:38:36 +02:00
// save value so it doesn't get reset next time the popup updates
zoom _videoScale = newZoom ;
// update zoom% label
2018-09-16 14:14:16 +02:00
VideoPanel . labels . zoomLevel . textContent = ( newZoom * 100 ) . toFixed ( ) ;
2018-09-18 00:40:05 +02:00
2018-09-23 00:38:36 +02:00
// send the command to bg script
2018-09-18 00:40:05 +02:00
var command = {
cmd : 'set-zoom' ,
2018-11-21 20:41:15 +01:00
zoom : newZoom ,
targetFrame : selectedSubitem [ selectedMenu ]
2018-09-18 00:40:05 +02:00
} ;
port . postMessage ( command ) ;
2018-09-16 14:14:16 +02:00
} ) ;
}
async function loadConfig ( site ) {
2018-09-19 23:34:47 +02:00
if ( Debug . debug ) {
2018-09-16 23:24:01 +02:00
console . log ( "\n\n-------------------------------------\n[popup.js::loadConfig] loading config. conf object:" , settings . active ) ;
2018-09-19 23:34:47 +02:00
}
2018-09-16 14:14:16 +02:00
configurePopupTabs ( site ) ;
configureGlobalTab ( ) ;
configureSitesTab ( site ) ;
2018-11-18 03:29:16 +01:00
configureVideoTab ( site ) ;
2018-09-16 14:14:16 +02:00
2018-09-19 23:34:47 +02:00
if ( Debug . debug ) {
2018-09-16 23:24:01 +02:00
console . log ( "[popup.js::loadConfig] config loaded\n-----------------------\n\n" ) ;
2018-09-19 23:34:47 +02:00
}
2017-12-30 18:36:08 +01:00
}
2018-11-09 00:35:18 +01:00
function removeAll ( itemArray ) {
for ( item of itemArray ) {
item . remove ( ) ;
}
}
function unselect ( itemArray , extraClasses ) {
for ( item of itemArray ) {
item . classList . remove ( 'selected' ) ;
if ( extraClasses ) {
item . classList . remove ( extraClasses ) ;
}
}
}
2018-11-11 22:35:08 +01:00
2018-11-09 00:35:18 +01:00
2018-08-30 00:56:15 +02:00
async function getSite ( ) {
2018-09-18 23:48:05 +02:00
if ( Debug . debug ) {
console . log ( "[popup.js] requesting current site" ) ;
}
2018-11-11 22:35:08 +01:00
try {
port . postMessage ( { cmd : 'get-current-site' } ) ;
} catch ( e ) {
console . log ( "[popup::getSite] sending get-current-site failed for some reason. Reason:" , e )
}
2018-05-27 01:29:02 +02:00
}
2017-12-30 18:36:08 +01:00
2017-12-30 02:58:24 +01:00
function openMenu ( menu ) {
if ( Debug . debug ) {
2018-09-16 14:14:16 +02:00
console . log ( "[popup.js::openMenu] trying to open menu" , menu , "\n element: " , Menu [ menu ] ) ;
2017-12-30 02:58:24 +01:00
}
for ( var m in Menu ) {
2018-09-16 14:14:16 +02:00
if ( Menu [ m ] )
Menu [ m ] . classList . add ( "hidden" ) ;
2017-12-30 02:58:24 +01:00
}
for ( var m in MenuTab ) {
if ( MenuTab [ m ] )
MenuTab [ m ] . classList . remove ( "selected" ) ;
}
2018-09-16 14:14:16 +02:00
Menu [ menu ] . classList . remove ( "hidden" ) ;
2018-02-04 17:39:26 +01:00
MenuTab [ menu ] . classList . add ( "selected" ) ;
2018-09-16 14:14:16 +02:00
selectedMenu = menu ;
2017-12-30 02:58:24 +01:00
}
2017-12-29 23:34:40 +01:00
2018-11-11 22:35:08 +01:00
function showMenu ( tab ) {
if ( ! tablist ) {
// todo: fix & remove this
return ;
}
for ( const i in tablist ) {
tablist [ i ] . unselect ( ) ;
tablist [ i ] . hideSubitems ( ) ;
}
tablist [ tab ] . select ( ) ;
tablist [ tab ] . showSubitems ( ) ;
// todo: display the correct tab
selectedMenu = tab ;
}
2018-02-01 00:26:16 +01:00
2018-07-09 23:30:11 +02:00
function getCustomAspectRatio ( ) {
var textBox _value = document . getElementById ( "_input_custom_ar" ) . value . trim ( ) ;
// validate value - this spaghett will match the following stuff
// [int]/[int]
// 1:[float]
// [float]
if ( ! /(^[0-9]+\/[0-9]+$|^(1:)?[0-9]+\.?[0-9]*$)/ . test ( textBox _value ) ) {
return false ; // validation failed!
}
if ( ! isNaN ( parseFloat ( textBox _value ) ) ) {
return parseFloat ( textBox _value ) ;
}
if ( /\// . test ( textBox _value ) ) {
const vars = textBox _value . split ( '/' ) ;
return parseInt ( vars [ 0 ] ) / parseInt ( vars [ 1 ] ) ; // non-ints shouldn't make it past regex
}
if ( /:/ . test ( textBox _value ) ) {
const vars = textBox _value . split ( ':' ) ;
return parseFloat ( vars [ 1 ] ) ;
}
// we should never come this far.
// If we do, then there's something wrong with the input and our regex
return false ;
}
function validateCustomAr ( ) {
const valid = getCustomAspectRatio ( ) !== false ;
const inputField = document . getElementById ( "_input_custom_ar" ) ;
const valueSaveButton = document . getElementById ( "_b_changeAr_save_custom_ar" ) ;
if ( valid ) {
inputField . classList . remove ( "invalid-input" ) ;
valueSaveButton . classList . remove ( "disabled-button" ) ;
} else {
inputField . classList . add ( "invalid-input" ) ;
valueSaveButton . classList . add ( "disabled-button" ) ;
}
}
2018-03-13 00:36:10 +01:00
2018-07-09 23:40:26 +02:00
function validateAutoArTimeout ( ) {
const inputField = document . getElementById ( "_input_autoAr_timer" ) ;
2018-08-30 00:56:15 +02:00
const valueSaveButton = document . getElementById ( "_b_autoar_save_autoar_timer" ) ;
2018-07-09 23:40:26 +02:00
2018-08-30 00:56:15 +02:00
if ( ! isNaN ( parseInt ( inputField . value . trim ( ) . value ( ) ) ) ) {
2018-07-09 23:40:26 +02:00
inputField . classList . remove ( "invalid-input" ) ;
valueSaveButton . classList . remove ( "disabled-button" ) ;
} else {
inputField . classList . add ( "invalid-input" ) ;
valueSaveButton . classList . add ( "disabled-button" ) ;
}
}
2017-12-29 23:34:40 +01:00
document . addEventListener ( "click" , ( e ) => {
2018-09-25 23:37:08 +02:00
if ( Debug . debug ) {
console . log ( "[popup.js] something clicked. event:" , e , JSON . stringify ( e ) ) ;
}
2017-12-29 23:34:40 +01:00
function getcmd ( e ) {
var command = { } ;
command . sender = "popup" ;
command . receiver = "uwbg" ;
2017-12-30 02:58:24 +01:00
if ( e . target . classList . contains ( "disabled" ) )
return ;
2018-11-11 22:35:08 +01:00
// if(e.target.classList.contains("menu-item")){
// if(Debug.debug) {
// console.log("[popup.js::eventListener] clicked on a tab. Class list:", e.target.classList);
// }
// if(e.target.classList.contains("_menu_tab_settings_ext")){
// openMenu("extensionSettings");
// } else if(e.target.classList.contains("_menu_tab_settings_site")){
// openMenu("siteSettings");
// } else if(e.target.classList.contains("_menu_tab_settings_video")){
// openMenu("videoSettings");
// } else if(e.target.classList.contains("_menu_tab_about")){
// openMenu("about");
// }
2017-12-30 02:58:24 +01:00
2018-11-11 22:35:08 +01:00
// // don't send commands
// return;
// }
2018-06-27 23:55:37 +02:00
if ( e . target . classList . contains ( "_ext" ) ) {
var command = { } ;
2018-06-28 23:43:52 +02:00
if ( e . target . classList . contains ( "_ext_global_options" ) ) {
if ( e . target . classList . contains ( "_blacklist" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . extensionMode = "blacklist" ;
2018-06-28 23:43:52 +02:00
} else if ( e . target . classList . contains ( "_whitelist" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . extensionMode = "whitelist" ;
2018-06-28 23:43:52 +02:00
} else {
2018-08-29 21:23:08 +02:00
settings . active . extensionMode = "disabled" ;
2018-06-28 23:43:52 +02:00
}
2018-08-29 21:23:08 +02:00
settings . save ( ) ;
return ;
2018-06-27 23:55:37 +02:00
} else if ( e . target . classList . contains ( "_ext_site_options" ) ) {
2018-08-30 00:56:15 +02:00
var mode ;
2018-06-27 23:55:37 +02:00
if ( e . target . classList . contains ( "_blacklist" ) ) {
2018-08-30 00:56:15 +02:00
mode = "disabled" ;
2018-06-27 23:55:37 +02:00
} else if ( e . target . classList . contains ( "_whitelist" ) ) {
2018-08-30 00:56:15 +02:00
mode = "enabled" ;
2018-06-27 23:55:37 +02:00
} else {
2018-08-30 00:56:15 +02:00
mode = "default" ;
2018-06-27 23:55:37 +02:00
}
2018-08-30 00:56:15 +02:00
2018-11-09 00:57:05 +01:00
if ( settings . active . sites [ site . host ] ) {
settings . active . sites [ site . host ] . status = mode ;
settings . active . sites [ site . host ] . statusEmbedded = mode ;
2018-08-30 00:56:15 +02:00
} else {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] = {
2018-08-30 00:56:15 +02:00
status : mode ,
statusEmbedded : mode ,
arStatus : 'default' ,
type : 'user-defined'
}
}
settings . save ( ) ;
return ;
2018-06-27 23:55:37 +02:00
}
}
2017-12-29 23:34:40 +01:00
if ( e . target . classList . contains ( "_changeAr" ) ) {
if ( e . target . classList . contains ( "_ar_auto" ) ) {
2018-06-15 00:33:10 +02:00
command . cmd = "autoar-start" ;
2018-05-27 21:41:08 +02:00
command . enabled = true ;
2017-12-29 23:34:40 +01:00
return command ;
}
2018-11-18 18:44:44 +01:00
2017-12-29 23:34:40 +01:00
}
2018-05-27 21:41:08 +02:00
if ( e . target . classList . contains ( "_stretch" ) ) {
2018-09-18 00:40:05 +02:00
// stretch, global
2018-06-29 00:30:42 +02:00
if ( e . target . classList . contains ( "_ar_stretch_global" ) ) {
if ( e . target . classList . contains ( "_none" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . stretch . initialMode = 0 ;
2018-06-29 00:30:42 +02:00
} else if ( e . target . classList . contains ( "_basic" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . stretch . initialMode = 1 ;
2018-06-29 00:30:42 +02:00
} else if ( e . target . classList . contains ( "_hybrid" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . stretch . initialMode = 2 ;
2018-06-29 00:30:42 +02:00
} else if ( e . target . classList . contains ( "_conditional" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . stretch . initialMode = 3 ;
2018-06-29 00:30:42 +02:00
}
2018-08-29 21:23:08 +02:00
settings . save ( ) ;
return ;
2018-06-29 00:30:42 +02:00
}
2018-09-18 00:40:05 +02:00
// stretch, site
if ( e . target . classList . contains ( "_ar_stretch_site" ) ) {
if ( e . target . classList . contains ( "_none" ) ) {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] . stretch = 0 ;
2018-09-18 00:40:05 +02:00
} else if ( e . target . classList . contains ( "_basic" ) ) {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] . stretch = 1 ;
2018-09-18 00:40:05 +02:00
} else if ( e . target . classList . contains ( "_hybrid" ) ) {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] . stretch = 2 ;
2018-09-18 00:40:05 +02:00
} else if ( e . target . classList . contains ( "_conditional" ) ) {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] . stretch = 3 ;
2018-09-18 00:40:05 +02:00
} else {
2018-11-09 00:57:05 +01:00
delete ( settings . active . sites [ site . host ] . stretch ) ;
2018-09-18 00:40:05 +02:00
}
settings . save ( ) ;
return ;
}
2018-11-18 18:44:44 +01:00
2018-05-27 21:41:08 +02:00
}
2018-03-13 00:36:10 +01:00
if ( e . target . classList . contains ( "_autoAr" ) ) {
2018-07-16 20:38:16 +02:00
if ( e . target . classList . contains ( "_ar_global_options" ) ) {
2018-06-28 23:43:52 +02:00
if ( e . target . classList . contains ( "_blacklist" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . arDetect . mode = "blacklist" ;
2018-06-28 23:43:52 +02:00
} else if ( e . target . classList . contains ( "_whitelist" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . arDetect . mode = "whitelist" ;
2018-06-28 23:43:52 +02:00
} else {
2018-08-29 21:23:08 +02:00
settings . active . arDetect . mode = "disabled" ;
2018-06-28 23:43:52 +02:00
}
2018-08-29 21:23:08 +02:00
settings . save ( ) ;
return ;
2018-08-30 00:56:15 +02:00
} else if ( e . target . classList . contains ( "_save_autoAr_timer" ) ) {
var value = parseInt ( document . getElementById ( "_input_autoAr_timer" ) . value . trim ( ) ) ;
2018-03-13 23:55:38 +01:00
2018-03-13 00:36:10 +01:00
if ( ! isNaN ( value ) ) {
2018-07-09 23:30:11 +02:00
var timeout = parseInt ( value ) ;
2018-08-29 21:23:08 +02:00
settings . active . arDetect . timer _playing = timeout ;
settings . save ( ) ;
2018-03-13 00:36:10 +01:00
}
2018-03-13 23:55:38 +01:00
return ;
2018-06-27 23:55:37 +02:00
} else if ( e . target . classList . contains ( "_ar_site_options" ) ) {
2018-08-30 00:56:15 +02:00
var mode ;
2018-08-03 00:44:37 +02:00
if ( e . target . classList . contains ( "_disabled" ) ) {
2018-08-30 00:56:15 +02:00
mode = "disabled" ;
2018-08-03 00:44:37 +02:00
} else if ( e . target . classList . contains ( "_enabled" ) ) {
2018-08-30 00:56:15 +02:00
mode = "enabled" ;
2018-06-27 23:55:37 +02:00
} else {
2018-08-30 00:56:15 +02:00
mode = "default" ;
2018-06-27 23:55:37 +02:00
}
2018-08-30 00:56:15 +02:00
2018-11-09 00:57:05 +01:00
if ( settings . active . sites [ site . host ] ) {
settings . active . sites [ site . host ] . arStatus = mode ;
2018-08-30 00:56:15 +02:00
} else {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] = {
2018-08-30 00:56:15 +02:00
status : settings . active . extensionMode ,
statusEmbedded : settings . active . extensionMode ,
arStatus : mode ,
type : 'user-defined'
}
}
settings . save ( ) ;
return ;
2017-12-29 23:34:40 +01:00
}
}
2018-09-18 00:40:05 +02:00
if ( e . target . classList . contains ( "_align_ext" ) ) {
if ( e . target . classList . contains ( "_align_ext_left" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . miscFullscreenSettings . videoFloat = 'left' ;
2018-09-18 00:40:05 +02:00
} else if ( e . target . classList . contains ( "_align_ext_center" ) ) {
2018-08-29 21:23:08 +02:00
settings . active . miscFullscreenSettings . videoFloat = 'center' ;
2018-09-18 00:40:05 +02:00
} else if ( e . target . classList . contains ( "_align_ext_right" ) ) {
settings . active . miscFullscreenSettings . videoFloat = 'right' ;
}
2018-09-23 13:55:03 +02:00
settings . save ( ) ;
return ;
2018-09-18 00:40:05 +02:00
}
if ( e . target . classList . contains ( "_align_site" ) ) {
if ( ! site ) {
return ;
}
if ( e . target . classList . contains ( "_align_site_left" ) ) {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] . videoAlignment = 'left' ;
2018-09-18 00:40:05 +02:00
} else if ( e . target . classList . contains ( "_align_site_center" ) ) {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] . videoAlignment = 'center' ;
2018-09-18 00:40:05 +02:00
} else if ( e . target . classList . contains ( "_align_site_right" ) ) {
2018-11-09 00:57:05 +01:00
settings . active . sites [ site . host ] . videoAlignment = 'right' ;
2018-09-18 00:40:05 +02:00
} else {
// default case — remove this object
2018-11-09 00:57:05 +01:00
delete ( settings . active . sites [ site . host ] . videoAlignment ) ;
2017-12-29 23:34:40 +01:00
}
2018-08-29 21:23:08 +02:00
settings . save ( ) ;
return ;
2018-09-18 00:40:05 +02:00
}
if ( e . target . classList . contains ( "_align" ) ) {
command . cmd = "set-alignment" ;
2018-09-23 02:39:27 +02:00
if ( e . target . classList . contains ( "_align_video_left" ) ) {
2018-09-18 00:40:05 +02:00
command . mode = 'left' ;
2018-09-23 02:39:27 +02:00
} else if ( e . target . classList . contains ( "_align_video_center" ) ) {
2018-09-18 00:40:05 +02:00
command . mode = 'center' ;
2018-09-23 02:39:27 +02:00
} else if ( e . target . classList . contains ( "_align_video_right" ) ) {
2018-09-18 00:40:05 +02:00
command . mode = 'right' ;
}
return command ;
}
//#region zoom buttons
if ( e . target . classList . contains ( "_zoom_show_shortcuts" ) ) {
VideoPanel . misc . zoomShortcuts . classList . remove ( "hidden" ) ;
VideoPanel . buttons . zoom . hideShortcuts . classList . remove ( "hidden" ) ;
VideoPanel . buttons . zoom . showShortcuts . classList . add ( "hidden" ) ;
return ;
}
if ( e . target . classList . contains ( "_zoom_hide_shortcuts" ) ) {
VideoPanel . misc . zoomShortcuts . classList . add ( "hidden" ) ;
VideoPanel . buttons . zoom . hideShortcuts . classList . add ( "hidden" ) ;
VideoPanel . buttons . zoom . showShortcuts . classList . remove ( "hidden" ) ;
return ;
}
if ( e . target . classList . contains ( "_zoom_reset" ) ) {
2018-09-23 19:46:40 +02:00
zoom _videoScale = scale ;
2018-09-18 00:40:05 +02:00
VideoPanel . labels . zoomLevel . textContent = 100 ;
VideoPanel . inputs . zoomSlider . value = 0 ; // log₂(1)
2018-09-23 19:46:40 +02:00
2018-09-18 00:40:05 +02:00
command . cmd = 'set-zoom' ;
command . zoom = 1 ;
2018-09-23 02:39:27 +02:00
return command ;
2018-09-18 00:40:05 +02:00
}
//#endregion
//#region show/hide custom ar
if ( e . target . classList . contains ( "_changeAr_show_customAr" ) ) {
VideoPanel . misc . customArChanger . classList . remove ( "hidden" ) ;
VideoPanel . buttons . changeAr . showCustomAr . classList . add ( "hidden" ) ;
VideoPanel . buttons . changeAr . hideCustomAr . classList . remove ( "hidden" ) ;
return ;
}
if ( e . target . classList . contains ( "_changeAr_hide_customAr" ) ) {
VideoPanel . misc . customArChanger . classList . add ( "hidden" ) ;
VideoPanel . buttons . changeAr . showCustomAr . classList . remove ( "hidden" ) ;
VideoPanel . buttons . changeAr . hideCustomAr . classList . add ( "hidden" ) ;
return ;
}
//#endregion
2017-12-29 23:34:40 +01:00
}
2018-09-25 23:37:08 +02:00
var command = getcmd ( e ) ;
2018-11-21 20:41:15 +01:00
command . targetFrame = selectedSubitem [ selectedMenu ]
2018-09-25 23:37:08 +02:00
if ( Debug . debug ) {
console . log ( "[popup.js] Got command (can be undefined):" , command , JSON . stringify ( command ) )
}
2017-12-30 02:58:24 +01:00
if ( command )
2018-05-27 01:29:02 +02:00
port . postMessage ( command ) ;
2018-01-18 00:11:03 +01:00
return true ;
2017-12-29 23:34:40 +01:00
} ) ;
2017-12-30 02:58:24 +01:00
2018-07-09 23:40:26 +02:00
2018-07-09 23:30:11 +02:00
2018-07-09 23:40:26 +02:00
2018-09-19 23:34:47 +02:00
async function sleep ( t ) {
return new Promise ( ( resolve , reject ) => {
2018-09-20 21:30:45 +02:00
setTimeout ( ( ) => resolve ( ) , t ) ;
2018-09-19 23:34:47 +02:00
} ) ;
}
2018-08-30 00:56:15 +02:00
async function popup _init ( ) {
// let's init settings and check if they're loaded
await settings . init ( ) ;
if ( Debug . debug ) {
console . log ( "[popup] Are settings loaded?" , settings )
}
2018-09-16 14:22:08 +02:00
// autoarFrequencyInputField.addEventListener("blur", (event) => {
// validateAutoArTimeout();
// });
// autoarFrequencyInputField.addEventListener("mouseleave", (event) => {
// validateAutoArTimeout();
// });
2018-08-30 00:56:15 +02:00
hideWarning ( "script-not-running-warning" ) ;
2018-09-20 21:30:45 +02:00
while ( true ) {
2018-09-19 23:34:47 +02:00
getSite ( ) ;
await sleep ( 5000 ) ;
}
2018-08-30 00:56:15 +02:00
}
popup _init ( ) ;