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 02:58:24 +01:00
var Menu = { } ;
2018-02-02 00:21:29 +01:00
// Menu.noVideo = document.getElementById("no-videos-display");
2017-12-30 02:58:24 +01:00
Menu . general = document . getElementById ( "extension-mode" ) ;
2018-02-01 00:26:16 +01:00
Menu . thisSite = document . getElementById ( "settings-for-current-site" ) ;
2017-12-30 02:58:24 +01:00
Menu . arSettings = document . getElementById ( "aspect-ratio-settings" ) ;
2018-03-13 00:36:10 +01:00
Menu . autoAr = document . getElementById ( "autoar-basic-settings" ) ;
2017-12-30 02:58:24 +01:00
Menu . cssHacks = document . getElementById ( "css-hacks-settings" ) ;
Menu . about = document . getElementById ( "panel-about" ) ;
2017-12-29 23:34:40 +01:00
2017-12-30 02:58:24 +01:00
var MenuTab = { } ;
MenuTab . general = document . getElementById ( "_menu_general" ) ;
2018-02-01 00:26:16 +01:00
MenuTab . thisSite = document . getElementById ( "_menu_this_site" ) ;
2017-12-30 02:58:24 +01:00
MenuTab . arSettings = document . getElementById ( "_menu_aspectratio" ) ;
MenuTab . cssHacks = document . getElementById ( "_menu_hacks" ) ;
MenuTab . about = document . getElementById ( "_menu_about" ) ;
2018-03-13 00:36:10 +01:00
MenuTab . autoAr = document . getElementById ( "_menu_autoar" ) ;
2017-12-29 23:34:40 +01:00
2018-06-27 23:55:37 +02:00
var ExtPanel = { } ;
ExtPanel . siteOptions = { } ;
ExtPanel . siteOptions . disabled = document . getElementById ( "_ext_site_options_blacklist" ) ;
ExtPanel . siteOptions . enabled = document . getElementById ( "_ext_site_options_whitelist" ) ;
ExtPanel . siteOptions . default = document . getElementById ( "_ext_site_options_default" ) ;
var AutoArPanel = { } ;
AutoArPanel . siteOptions = { } ;
AutoArPanel . siteOptions . disabled = document . getElementById ( "_ext_site_options_blacklist" ) ;
AutoArPanel . siteOptions . enabled = document . getElementById ( "_ext_site_options_whitelist" ) ;
AutoArPanel . siteOptions . default = document . getElementById ( "_ext_site_options_default" ) ;
var ArPanel = { } ;
2017-12-30 18:36:08 +01:00
ArPanel . alignment = { } ;
ArPanel . alignment . left = document . getElementById ( "_align_left" ) ;
ArPanel . alignment . center = document . getElementById ( "_align_center" ) ;
ArPanel . alignment . right = document . getElementById ( "_align_right" ) ;
ArPanel . autoar = { } ;
2017-12-30 02:58:24 +01:00
var selectedMenu = "arSettings" ;
var hasVideos = false ;
2017-12-29 23:34:40 +01:00
2017-12-30 18:36:08 +01:00
var _config ;
2018-01-02 03:36:29 +01:00
var _changeAr _button _shortcuts = { "autoar" : "none" , "reset" : "none" , "219" : "none" , "189" : "none" , "169" : "none" }
2017-12-30 18:36:08 +01:00
2018-05-27 01:29:02 +02:00
var comms = new Comms ( ) ;
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-05-27 01:29:02 +02:00
async function processReceivedMessage ( message , port ) {
if ( message . cmd === 'set-config' ) {
2018-06-27 23:55:37 +02:00
this . loadConfig ( message . conf , message . site ) ;
2018-05-27 01:29:02 +02:00
}
2018-01-10 23:16:07 +01:00
}
2018-02-02 00:21:29 +01:00
function hideWarning ( warn ) {
document . getElementById ( warn ) . classList . add ( "hidden" ) ;
}
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-06-27 23:55:37 +02:00
function loadConfig ( extensionConf , site ) {
2017-12-30 18:36:08 +01:00
if ( Debug . debug )
2018-06-27 23:55:37 +02:00
console . log ( "[popup.js::loadConfig] loading config. conf object:" , extensionConf , "\n\n\n\n\n\n\n\n-------------------------------------" ) ;
2018-03-11 18:00:47 +01:00
2018-05-27 01:29:02 +02:00
_extensionConf = extensionConf ;
2018-06-27 23:55:37 +02:00
// ----------------------
//#region extension-basics - SET BASIC EXTENSION OPTIONS
if ( Debug . debug )
console . log ( "Extension mode?" , extensionConf . extensionMode , "| site & site options:" , site , "," , ( site && extensionConf . sites [ site ] ) ? extensionConf . sites [ site ] . status : "fucky wucky?" ) ;
document . getElementById ( "_checkbox_ext-enabled" ) . checked = extensionConf . extensionMode !== "disabled" ;
document . getElementById ( "_checkbox_ext-whitelist" ) . checked = extensionConf . extensionMode === "whitelist" ;
for ( var button in ExtPanel . siteOptions ) {
ExtPanel . siteOptions [ button ] . classList . remove ( "selected" ) ;
}
if ( site && extensionConf . sites [ site ] ) {
ExtPanel . siteOptions [ extensionConf . sites [ site ] . arStatus ] . classList . add ( "selected" ) ;
}
//#endregion extension-basics
//
// ------------
//#region autoar - SET AUTOAR OPTIONS
if ( Debug . debug )
console . log ( "Autodetect mode?" , extensionConf . arDetect . mode , "| site & site options:" , site , "," , ( site && extensionConf . sites [ site ] ) ? extensionConf . sites [ site ] . arStatus : "fucky wucky?" ) ;
document . getElementById ( "_checkbox_autoArEnabled" ) . checked = extensionConf . arDetect . mode !== "disabled" ;
document . getElementById ( "_checkbox_autoar-whitelist" ) . checked = extensionConf . arDetect . mode === "whitelist" ;
2018-05-27 01:29:02 +02:00
document . getElementById ( "_autoAr_disabled_reason" ) . textContent = extensionConf . arDetect . DisabledReason ;
document . getElementById ( "_input_autoAr_timer" ) . value = extensionConf . arDetect . timer _playing ;
2018-06-27 23:55:37 +02:00
for ( var button in AutoArPanel . siteOptions ) {
AutoArPanel . siteOptions [ button ] . classList . remove ( "selected" ) ;
}
if ( site && extensionConf . sites [ site ] ) {
AutoArPanel . siteOptions [ extensionConf . sites [ site ] . arStatus ] . classList . add ( "selected" ) ;
}
//#endregion
2017-12-30 18:36:08 +01:00
// process video alignment:
2018-05-27 01:29:02 +02:00
if ( extensionConf . miscFullscreenSettings . videoFloat ) {
2017-12-30 18:36:08 +01:00
for ( var button in ArPanel . alignment )
ArPanel . alignment [ button ] . classList . remove ( "selected" ) ;
2018-05-27 01:29:02 +02:00
ArPanel . alignment [ extensionConf . miscFullscreenSettings . videoFloat ] . classList . add ( "selected" ) ;
2017-12-30 18:36:08 +01:00
}
2018-01-02 03:36:29 +01:00
// process keyboard shortcuts:
2018-05-27 01:29:02 +02:00
if ( extensionConf . keyboard . shortcuts ) {
for ( var key in extensionConf . keyboard . shortcuts ) {
var shortcut = extensionConf . keyboard . shortcuts [ key ] ;
2018-01-02 03:36:29 +01:00
var keypress = stringToKeyCombo ( key ) ;
try {
2018-05-27 01:29:02 +02:00
if ( shortcut . action == "crop" ) {
if ( shortcut . arg == 2.0 ) {
2018-01-02 03:36:29 +01:00
_changeAr _button _shortcuts [ "189" ] = keypress ;
}
2018-05-27 01:29:02 +02:00
else if ( shortcut . arg == 2.39 ) {
2018-01-02 03:36:29 +01:00
_changeAr _button _shortcuts [ "219" ] = keypress ;
}
2018-05-27 01:29:02 +02:00
else if ( shortcut . arg == 1.78 ) {
2018-01-02 03:36:29 +01:00
_changeAr _button _shortcuts [ "169" ] = keypress ;
}
2018-05-27 01:29:02 +02:00
else if ( shortcut . arg == "fitw" ) {
_changeAr _button _shortcuts [ "fitw" ] = keypress ;
}
else if ( shortcut . arg == "fith" ) {
_changeAr _button _shortcuts [ "fith" ] = keypress ;
}
else if ( shortcut . arg == "reset" ) {
_changeAr _button _shortcuts [ "reset" ] = keypress ;
}
2018-01-02 03:36:29 +01:00
}
2018-05-27 01:29:02 +02:00
else if ( shortcut . action == "auto-ar" ) {
_changeAr _button _shortcuts [ "auto-ar" ] = keypress ;
2018-01-02 03:36:29 +01:00
}
}
catch ( Ex ) {
//do nothing if key doesn't exist
}
}
for ( var key in _changeAr _button _shortcuts ) {
try {
2018-05-27 01:29:02 +02:00
document . getElementById ( ` _b_changeAr_ ${ key } _key ` ) . textContent = ` ( ${ _changeAr _button _shortcuts [ key ] } ) ` ;
2018-01-02 03:36:29 +01:00
}
catch ( ex ) {
}
}
}
2018-05-27 01:29:02 +02:00
if ( Debug . debug )
console . log ( "[popup.js::loadConfig] config loaded" ) ;
2017-12-30 18:36:08 +01:00
}
2018-05-27 01:29:02 +02:00
async function getConf ( ) {
port . postMessage ( { cmd : 'get-config' } ) ;
}
2017-12-30 18:36:08 +01:00
2017-12-30 02:58:24 +01:00
function openMenu ( menu ) {
if ( Debug . debug ) {
console . log ( "[popup.js::openMenu] trying to open menu" , menu , "| element: " , Menu [ menu ] ) ;
}
for ( var m in Menu ) {
Menu [ m ] . classList . add ( "hidden" ) ;
}
for ( var m in MenuTab ) {
if ( MenuTab [ m ] )
MenuTab [ m ] . classList . remove ( "selected" ) ;
}
if ( menu == "arSettings" || menu == "cssHacks" ) {
2018-05-27 01:29:02 +02:00
// if(!hasVideos)
// Menu.noVideo.classList.remove("hidden");
// else{
2017-12-30 02:58:24 +01:00
Menu [ menu ] . classList . remove ( "hidden" ) ;
if ( Debug . debug ) {
console . log ( "[popup.js::openMenu] unhid" , menu , "| element: " , Menu [ menu ] ) ;
2018-05-27 01:29:02 +02:00
}
// }
2017-12-30 02:58:24 +01:00
}
else {
Menu [ menu ] . classList . remove ( "hidden" ) ;
if ( Debug . debug ) {
console . log ( "[popup.js::openMenu] unhid" , menu , "| element: " , Menu [ menu ] ) ;
}
}
2018-02-04 17:39:26 +01:00
selectedMenu = menu ;
MenuTab [ menu ] . classList . add ( "selected" ) ;
2017-12-30 02:58:24 +01:00
}
2017-12-29 23:34:40 +01:00
2017-12-30 18:36:08 +01:00
function _arctl _onclick ( command ) {
if ( ! _config )
return ;
if ( command . cmd == "stop-autoar" )
_config . arConf . enabled _current = false ;
else if ( command . cmd == "force-ar" )
_config . arConf . enabled _current = true ;
else if ( command . cmd == "disable-autoar" )
_config . arConf . enabled _global = false ;
else if ( command . cmd == "enable-autoar" )
_config . arConf . enabled _global = true ;
showArctlButtons ( ) ;
}
function showArctlButtons ( ) {
if ( ! _config )
return ;
2018-05-27 01:29:02 +02:00
// if(_config.arConf){
// if(! _config.arConf.enabled_global){
// ArPanel.autoar.disable.classList.add("hidden");
// ArPanel.autoar.enable.classList.remove("hidden");
// ArPanel.autoar.enable_tmp.textContent = "Temporarily enable";
// ArPanel.autoar.disable_tmp.textContent = "Temporarily disable";
// }
// else{
// ArPanel.autoar.disable.classList.remove("hidden");
// ArPanel.autoar.enable.classList.add("hidden");
// ArPanel.autoar.enable_tmp.textContent = "Re-enable";
// ArPanel.autoar.disable_tmp.textContent = "Temporarily disable";
// }
// if(! _config.arConf.enabled_current){
// ArPanel.autoar.disable_tmp.classList.add("hidden");
// ArPanel.autoar.enable_tmp.classList.remove("hidden");
// }
// else{
// ArPanel.autoar.disable_tmp.classList.remove("hidden");
// ArPanel.autoar.enable_tmp.classList.add("hidden");
// }
// }
2018-02-01 00:26:16 +01:00
}
2018-03-13 00:36:10 +01:00
2018-02-01 00:26:16 +01:00
function toggleSite ( option ) {
if ( Debug . debug )
console . log ( "[popup::toggleSite] toggling extension 'should I work' status to" , option , "on current site" ) ;
2018-02-02 00:21:29 +01:00
Comms . sendToBackgroundScript ( { cmd : "enable-for-site" , option : option } ) ;
2017-12-30 18:36:08 +01:00
}
2017-12-29 23:34:40 +01:00
2018-06-27 23:55:37 +02:00
function getMode ( isEnabled , whitelistOnly ) {
if ( isEnabled ) {
return whitelistOnly ? "whitelist" : "blacklist"
} else {
return "disabled" ;
}
}
2017-12-29 23:34:40 +01:00
document . addEventListener ( "click" , ( e ) => {
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 ;
if ( e . target . classList . contains ( "menu-item" ) ) {
if ( e . target . classList . contains ( "_menu_general" ) ) {
openMenu ( "general" ) ;
}
2018-02-01 00:26:16 +01:00
if ( e . target . classList . contains ( "_menu_this_site" ) ) {
openMenu ( "thisSite" ) ;
}
2017-12-30 02:58:24 +01:00
else if ( e . target . classList . contains ( "_menu_aspectratio" ) ) {
openMenu ( "arSettings" ) ;
}
else if ( e . target . classList . contains ( "_menu_hacks" ) ) {
openMenu ( "cssHacks" ) ;
}
else if ( e . target . classList . contains ( "_menu_about" ) ) {
openMenu ( "about" ) ;
}
2018-03-13 00:36:10 +01:00
else if ( e . target . classList . contains ( "_menu_autoar" ) ) {
openMenu ( "autoAr" ) ;
}
2017-12-30 02:58:24 +01:00
// don't send commands
return ;
}
2018-06-27 23:55:37 +02:00
if ( e . target . classList . contains ( "_ext" ) ) {
var command = { } ;
if ( e . target . classList . contains ( "_ext_enabled" ) ) {
var extStatus = document . getElementById ( "_checkbox_ext-enabled" ) . checked ;
var whitelist = document . getElementById ( "_checkbox_ext-whitelist" ) . checked ;
// if extension is set to disabled, we also disable 'whitelist only' checkbox
document . getElementById ( "_checkbox_ext-whitelist" ) . disabled = ! extStatus ;
return {
cmd : "set-extension-defaults" ,
mode : getMode ( extStatus , whitelist ) ,
sender : "popup" ,
receiver : "uwbg"
} ;
} else if ( e . target . classList . contains ( "_ext_whitelist-only" ) ) {
var extStatus = document . getElementById ( "_checkbox_ext-enabled" ) . checked ;
var whitelist = document . getElementById ( "_checkbox_ext-whitelist" ) . checked ;
return {
cmd : "set-extension-defaults" ,
mode : getMode ( extStatus , whitelist ) ,
sender : "popup" ,
receiver : "uwbg"
} ;
} else if ( e . target . classList . contains ( "_ext_site_options" ) ) {
command . cmd = "set-extension-for-site" ;
if ( e . target . classList . contains ( "_blacklist" ) ) {
command . mode = "disabled" ;
} else if ( e . target . classList . contains ( "_whitelist" ) ) {
command . mode = "enabled" ;
} else {
command . mode = "default" ;
}
return command ;
}
}
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 ;
}
if ( e . target . classList . contains ( "_ar_reset" ) ) {
2018-05-27 01:29:02 +02:00
command . cmd = "set-ar" ;
command . ratio = "reset" ;
2018-02-01 00:26:16 +01:00
return command ;
}
if ( e . target . classList . contains ( "_ar_fitw" ) ) {
2018-05-27 01:29:02 +02:00
command . cmd = "set-ar" ;
command . ratio = "fitw" ;
2018-02-01 00:26:16 +01:00
return command ;
}
if ( e . target . classList . contains ( "_ar_fitw" ) ) {
2018-05-27 01:29:02 +02:00
command . cmd = "set-ar" ;
command . ratio = "fith" ;
2017-12-29 23:34:40 +01:00
return command ;
}
if ( e . target . classList . contains ( "_ar_219" ) ) {
2018-05-27 01:29:02 +02:00
command . cmd = "set-ar" ;
command . ratio = 2.39 ;
2017-12-29 23:34:40 +01:00
return command ;
}
if ( e . target . classList . contains ( "_ar_189" ) ) {
2018-05-27 01:29:02 +02:00
command . cmd = "set-ar" ;
command . ratio = 2.0 ;
2017-12-29 23:34:40 +01:00
return command ;
}
if ( e . target . classList . contains ( "_ar_169" ) ) {
2018-05-27 01:29:02 +02:00
command . cmd = "set-ar" ;
command . ratio = 1.78 ;
2017-12-29 23:34:40 +01:00
return command ;
}
if ( e . target . classList . contains ( "_ar_1610" ) ) {
2018-05-27 01:29:02 +02:00
command . cmd = "set-ar" ;
command . ratio = 1.6 ;
2017-12-29 23:34:40 +01:00
return command ;
}
}
2018-05-27 21:41:08 +02:00
if ( e . target . classList . contains ( "_stretch" ) ) {
if ( e . target . classList . contains ( "_ar_stretch_none" ) ) {
command . cmd = "set-stretch" ;
command . mode = "NO_STRETCH" ;
} else if ( e . target . classList . contains ( "_ar_stretch_basic" ) ) {
command . cmd = "set-stretch" ;
command . mode = "BASIC" ;
} else if ( e . target . classList . contains ( "_ar_stretch_hybrid" ) ) {
command . cmd = "set-stretch" ;
command . mode = "HYBRID" ;
} else if ( e . target . classList . contains ( "_ar_stretch_conditional" ) ) {
command . cmd = "set-stretch" ;
command . mode = "CONDITIONAL" ;
}
return command ;
}
2018-03-13 00:36:10 +01:00
if ( e . target . classList . contains ( "_autoAr" ) ) {
2018-06-27 23:55:37 +02:00
2017-12-30 18:36:08 +01:00
var command = { } ;
2018-03-13 00:36:10 +01:00
if ( e . target . classList . contains ( "_autoAr_enabled" ) ) {
var arStatus = document . getElementById ( "_checkbox_autoArEnabled" ) . checked ;
2018-06-27 23:55:37 +02:00
var whitelist = document . getElementById ( "_checkbox_autoar-whitelist" ) . checked ;
// if autoar is set to disabled, we also disable 'whitelist only' checkbox
document . getElementById ( "_checkbox_autoar-whitelist" ) . disabled = ! arStatus ;
return {
cmd : "set-autoar-defaults" ,
mode : getMode ( arStatus , whitelist ) ,
sender : "popup" ,
receiver : "uwbg"
} ;
} else if ( e . target . classList . contains ( "_autoAr_whitelist-only" ) ) {
var arStatus = document . getElementById ( "_checkbox_autoArEnabled" ) . checked ;
var whitelist = document . getElementById ( "_checkbox_autoar-whitelist" ) . checked ;
return {
cmd : "set-autoar-mode" ,
mode : getMode ( arStatus , whitelist ) ,
sender : "popup" ,
receiver : "uwbg"
} ;
} else if ( e . target . classList . contains ( "_save_autoAr_frequency" ) ) {
2018-03-13 23:55:38 +01:00
var value = parseInt ( document . getElementById ( "_input_autoAr_frequency" ) . value . trim ( ) ) ;
2018-03-13 00:36:10 +01:00
if ( ! isNaN ( value ) ) {
var timeout = parseInt ( 1000 / value ) ;
command = { cmd : "autoar-set-timer-playing" , timeout : timeout , sender : "popup" , receiver : "uwbg" } ;
2018-03-13 23:55:38 +01:00
Comms . sendToBackgroundScript ( command ) ;
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" ) ) {
command . cmd = "set-autoar-for-site" ;
if ( e . target . classList . contains ( "_blacklist" ) ) {
command . mode = "disabled" ;
} else if ( e . target . classList . contains ( "_whitelist" ) ) {
command . mode = "enabled" ;
} else {
command . mode = "default" ;
}
return command ;
2017-12-29 23:34:40 +01:00
}
}
if ( e . target . classList . contains ( "_align" ) ) {
command . global = true ;
if ( e . target . classList . contains ( "_align_left" ) ) {
command . cmd = "force-video-float" ,
command . newFloat = "left"
2018-06-27 23:55:37 +02:00
// console.log(".................\n\n\n..........\n\n >>command<< \n\n\n\n ",command,"\n\n\n.........\n\n\n................................");
2017-12-29 23:34:40 +01:00
return command ;
}
if ( e . target . classList . contains ( "_align_center" ) ) {
command . cmd = "force-video-float"
command . newFloat = "center"
return command ;
}
if ( e . target . classList . contains ( "_align_right" ) ) {
command . cmd = "force-video-float" ;
command . newFloat = "right" ;
return command ;
}
}
2018-06-27 23:55:37 +02:00
if ( e . target . classList . contains ( "extensionEnabledOnCurrentSite" ) ) { // legacy? can be removed?
2018-02-01 00:26:16 +01:00
toggleSite ( document . extensionEnabledOnCurrentSite . mode . value ) ;
}
2017-12-29 23:34:40 +01:00
}
var command = getcmd ( e ) ;
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-02-02 00:21:29 +01:00
hideWarning ( "script-not-running-warning" ) ;
openMenu ( selectedMenu ) ;
2018-05-27 01:29:02 +02:00
// check4videos();
getConf ( ) ;
// check4siteStatus();