2017-01-21 18:31:47 +01:00
var browser _autodetect = true ;
var usebrowser = "chrome" ;
if ( browser _autodetect ) {
2017-03-02 20:06:53 +01:00
if ( typeof browser === "undefined" ) {
if ( chrome ) {
browser = chrome ;
usebrowser = "chrome" ;
2017-01-21 18:31:47 +01:00
}
}
else
usebrowser = "firefox" ;
}
else {
if ( usebrowser == "chrome" )
browser = chrome ;
}
2016-12-07 23:58:43 +01:00
function showAbout ( ) {
2017-01-19 21:29:54 +01:00
clearPage ( ) ;
2016-12-07 23:58:43 +01:00
2017-01-19 21:29:54 +01:00
document . getElementById ( "about" ) . classList . remove ( "hide" ) ;
2016-12-07 23:58:43 +01:00
document . getElementById ( "tab_about" ) . classList . add ( "tab-selected" ) ;
}
function showShortcuts ( ) {
2017-01-19 21:29:54 +01:00
clearPage ( ) ;
2016-12-07 23:58:43 +01:00
document . getElementById ( "uw_shortcuts" ) . classList . remove ( "hide" ) ;
2017-01-19 21:29:54 +01:00
document . getElementById ( "tab_shortcuts" ) . classList . add ( "tab-selected" ) ;
}
function showGeneralSettings ( ) {
clearPage ( ) ;
document . getElementById ( "general_settings" ) . classList . remove ( "hide" ) ;
document . getElementById ( "tab_general_settings" ) . classList . add ( "tab-selected" ) ;
}
2017-03-02 20:06:53 +01:00
function showSites ( ) {
clearPage ( ) ;
document . getElementById ( "uw_sites" ) . classList . remove ( "hide" ) ;
document . getElementById ( "tab_sites" ) . classList . add ( "tab-selected" ) ;
}
2017-01-19 21:29:54 +01:00
function clearPage ( ) {
// Hide you sections
document . getElementById ( "uw_shortcuts" ) . classList . add ( "hide" ) ;
2016-12-07 23:58:43 +01:00
document . getElementById ( "about" ) . classList . add ( "hide" ) ;
2017-01-19 21:29:54 +01:00
document . getElementById ( "general_settings" ) . classList . add ( "hide" ) ;
2017-03-02 20:06:53 +01:00
document . getElementById ( "uw_sites" ) . classList . add ( "hide" ) ;
2016-12-07 23:58:43 +01:00
2017-01-19 21:29:54 +01:00
// Hide you tabs
document . getElementById ( "tab_shortcuts" ) . classList . remove ( "tab-selected" ) ;
2016-12-07 23:58:43 +01:00
document . getElementById ( "tab_about" ) . classList . remove ( "tab-selected" ) ;
2017-01-19 21:29:54 +01:00
document . getElementById ( "tab_general_settings" ) . classList . remove ( "tab-selected" ) ;
2017-03-02 20:06:53 +01:00
document . getElementById ( "tab_sites" ) . classList . remove ( "tab-selected" ) ;
2016-12-07 23:58:43 +01:00
}
function saveopts ( ) {
2017-01-19 21:29:54 +01:00
var actions = [ "fitw" , "fith" , "reset" , "zoom" , "unzoom" , "ar219" , "ar169" , "ar1610" , "ar43" , "autoar" ] ;
2016-12-07 23:58:43 +01:00
var new _keybinds = { } ;
// Preberemo naš obrazec in iz njega naredimo nov objekt z bližnjicami.
// Let's read our form and make a new object with keybinds.
for ( var i = 0 ; i < actions . length ; i ++ ) {
var action = actions [ i ] ;
var targetAR = "" ;
if ( action == "ar219" ) {
action = "char" ;
targetAR = ( 21 / 9 ) ;
}
if ( action == "ar169" ) {
action = "char" ;
targetAR = ( 16 / 9 ) ;
}
if ( action == "ar1610" ) {
action = "char" ;
targetAR = ( 16 / 10 ) ;
}
if ( action == "ar43" ) {
action = "char" ;
targetAR = ( 4 / 3 ) ;
}
if ( targetAR != "" ) {
var keybind = {
action : action ,
targetAR : targetAR ,
key : document . querySelector ( "#" + actions [ i ] + "_letter" ) . value . toLowerCase ( ) . replace ( /[^a-z0-9]/ , "" ) ,
modifiers : [ ]
}
}
else {
var keybind = {
action : action ,
key : document . querySelector ( "#" + actions [ i ] + "_letter" ) . value . toLowerCase ( ) . replace ( /[^a-z0-9]/ , "" ) ,
modifiers : [ ]
}
}
if ( document . querySelector ( "#" + actions [ i ] + "_ctrl" ) . checked )
keybind . modifiers . push ( "ctrl" ) ;
if ( document . querySelector ( "#" + actions [ i ] + "_alt" ) . checked )
keybind . modifiers . push ( "alt" ) ;
if ( document . querySelector ( "#" + actions [ i ] + "_shift" ) . checked )
keybind . modifiers . push ( "shift" ) ;
new _keybinds [ i ] = keybind ;
}
// Preveriti moramo, da nismo dvema možnostima dodali isto bližnjico.
// We need to check if all keybinds are unique.
var fail = false ;
for ( var i = 0 ; i < actions . length ; i ++ )
document . querySelector ( "#" + actions [ i ] + "_letter" ) . classList . remove ( "dup_keybinds" ) ;
for ( var i = 0 ; i < actions . length ; i ++ ) {
if ( new _keybinds [ i ] . key == "" )
continue ;
for ( var j = i + 1 ; j < actions . length ; j ++ ) {
if ( new _keybinds [ i ] . key == new _keybinds [ j ] . key ) {
if ( compareModifiers ( new _keybinds [ i ] . modifiers , new _keybinds [ j ] . modifiers ) ) {
fail = true ;
document . querySelector ( "#" + actions [ i ] + "_letter" ) . classList . add ( "dup_keybinds" ) ;
document . querySelector ( "#" + actions [ j ] + "_letter" ) . classList . add ( "dup_keybinds" ) ;
}
}
}
}
if ( ! fail ) {
browser . storage . local . set ( { ultrawidify _keybinds : new _keybinds } ) ;
}
}
2017-01-19 21:29:54 +01:00
function saveAutoar ( ) {
setopt ( { ultrawidify _autoar : document . querySelector ( "#enable_autoar" ) . checked } ) ;
}
2017-02-22 13:53:40 +01:00
function saveUI ( ) {
var show _ui = document . querySelector ( "#enable_ui" ) ;
var ui _compact = document . querySelector ( "#enable_ui_compact" ) ;
var optionLine = document . getElementById ( "compact_ui_suboption" ) ;
if ( show _ui . checked ) {
ui _compact . disabled = false ;
optionLine . classList . remove ( "hide" ) ;
setopt ( { ultrawidify _ui : ui _compact . checked ? "compact" : "all" } ) ;
}
else {
ui _compact . disabled = true ;
optionLine . classList . add ( "hide" ) ;
setopt ( { ultrawidify _ui : "none" } ) ;
}
}
2017-01-19 21:29:54 +01:00
// setopt in getopt. Shranita oz. dobita stvari iz skladišča
// setopt, getopt. They set/get stuff from the storage
function setopt ( item ) {
browser . storage . local . set ( item ) ;
}
function getopt ( prop , callback ) {
2017-01-21 18:31:47 +01:00
if ( usebrowser == "chrome" )
browser . storage . local . get ( prop , callback ) ;
else
browser . storage . local . get ( prop ) . then ( callback ) ;
2017-01-19 21:29:54 +01:00
}
2016-12-07 23:58:43 +01:00
function compareModifiers ( a , b ) {
//NOTE: to je precej slab in neprenoslijv način primerjanja dveh tabel, ampak za naš primer deluje dovolj
// dobro, saj 'ctrl' vedno pride pred 'alt' in 'alt' vedno pride pred 'shift' (če se sploh pojavijo).
//NOTE: this is bad and totally unfoolproof practice. In our example comparing arrays the way we do works
// because values ALWAYS appear in the same order: 'ctrl' always appears before 'alt' (or it doesn't
// appear at all). 'alt' always appears before 'shift' (or it doesn't appear at all).
if ( a . length != b . length )
return false ;
var match = true ;
for ( var i = 0 ; i < a . length ; i ++ )
match &= a [ i ] == b [ i ]
return match ;
}
function printerr ( err ) {
console . log ( err ) ;
}
function gotopts ( opts ) {
2017-03-02 20:06:53 +01:00
if ( ! opts . ultrawidify _keybinds ) {
console . log ( "ultrawidify keybinds are undefined. the fuck?" , opts ) ;
return ;
}
2016-12-07 23:58:43 +01:00
var KEYBINDS = Object . keys ( opts . ultrawidify _keybinds ) . map ( function ( key ) { return opts . ultrawidify _keybinds [ key ] ; } ) ;
2017-03-02 20:06:53 +01:00
// google chrome is really the untermensch browse // google chrome is really the untermensch browserr
2017-01-19 21:29:54 +01:00
var actions = [ "fitw" , "fith" , "reset" , "zoom" , "unzoom" , "ar219" , "ar169" , "ar1610" , "ar43" , "autoar" ] ;
2016-12-07 23:58:43 +01:00
for ( var i = 0 ; i < actions . length ; i ++ ) {
document . querySelector ( "#" + actions [ i ] + "_letter" ) . classList . remove ( "dup_keybinds" ) ;
document . querySelector ( "#" + actions [ i ] + "_letter" ) . value = KEYBINDS [ i ] . key ;
for ( var j = 0 ; j < KEYBINDS [ i ] . modifiers . length ; j ++ ) {
if ( KEYBINDS [ i ] . modifiers [ j ] == "ctrl" )
document . querySelector ( "#" + actions [ i ] + "_ctrl" ) . checked = true ;
if ( KEYBINDS [ i ] . modifiers [ j ] == "alt" )
document . querySelector ( "#" + actions [ i ] + "_alt" ) . checked = true ;
if ( KEYBINDS [ i ] . modifiers [ j ] == "shift" )
document . querySelector ( "#" + actions [ i ] + "_shift" ) . checked = true ;
}
}
}
2017-02-22 13:53:40 +01:00
function gotar ( opts ) {
}
function gotui ( opts ) {
var show _ui = document . querySelector ( "#enable_ui" ) ;
var ui _compact = document . querySelector ( "#enable_ui_compact" ) ;
var optionLine = document . getElementById ( "compact_ui_suboption" ) ;
if ( opts . ultrawidify _ui == "all" ) {
show _ui . checked = true ;
ui _compact . checked = false ;
optionLine . classList . remove ( "hide" ) ;
}
else if ( opts . ultrawidify _ui == "compact" ) {
show _ui . checked = true ;
ui _compact . checked = true ;
optionLine . classList . remove ( "hide" ) ;
}
else if ( opts . ultrawidify _ui == "none" ) {
show _ui . checked = false ;
ui _compact . checked = false ;
optionLine . classList . add ( "hide" ) ;
}
}
2017-03-02 20:06:53 +01:00
function gotsites ( opts ) {
var list = document . getElementById ( "uw_sites_list" ) ;
if ( list )
list . remove ( ) ;
var anchor = document . getElementById ( "uw_sites_body" ) ;
list = document . createElement ( "div" ) ;
list . id = "uw_sites_list" ;
list . className = "uw_sites" ;
anchor . appendChild ( list ) ;
uw _sites = opts . ultrawidify _siterules ;
console . log ( "opts" , opts ) ;
for ( type in { "official" : 1 , "non-official" : 1 , "custom" : 1 } ) { // unparalleled laziness!
var head = document . createElement ( "div" ) ;
head . className = "sites_header" ;
head . textContent = type . charAt ( 0 ) . toUpperCase ( ) + type . slice ( 1 ) ;
var category _desc = document . createElement ( "div" ) ;
if ( type == "official" ) {
category _desc . textContent = "These sites are officially supported by the extension developer. These sites should always work." ;
}
else if ( type == "non-official" ) {
category _desc . textContent = "Sites in this category have been contribued by third parties. These sites will probably work, but the developer couldn't test whether they work or not." ;
}
else if ( type == "custom" ) {
category _desc . textContent = "In this section, you can define rules for sites that aren't supported either officially or non-officially. See [todo: link] contributing for details. If you define a custom site, please consider sharing configuration on github (see contributing for details)." ;
}
list . append ( head ) ;
list . append ( category _desc ) ;
var category _counter ;
for ( site in uw _sites ) {
category _counter = 0 ;
if ( uw _sites [ site ] . type == type ) {
var entry = document . createElement ( "div" ) ;
var displayedInfo = document . createElement ( "div" ) ;
displayedInfo . id = site + "_display" ;
displayedInfo . className = "uw_options_line site_details" ;
var siteTitle = document . createElement ( "div" ) ;
siteTitle . className = "site_name" ;
{
var sitecb = mkcb ( site , uw _sites [ site ] . enabled , true ) ;
var editTitle = mkebox ( site , site )
editTitle . className = "site_title_ebox" ;
siteTitle . append ( sitecb ) ;
siteTitle . append ( editTitle ) ;
var editBtn = document . createElement ( "div" ) ;
editBtn . textContent = "« edit »" ;
editBtn . className = "inline_button" ;
siteTitle . append ( editBtn ) ;
}
var urlRules = document . createElement ( "div" ) ;
{
var urlRulesLabel = document . createElement ( "span" ) ;
urlRulesLabel . textContent = "URL rule: " ;
urlRulesEbox = mkebox ( site , uw _sites [ site ] . urlRules [ 0 ] ) ;
urlRules . append ( urlRulesLabel ) ;
urlRules . append ( urlRulesEbox ) ;
}
var playerElement = document . createElement ( "div" ) ;
{
var playerName = document . createElement ( "div" ) ;
var playerNameLabel = document . createElement ( "span" ) ;
var playerNameEbox = mkebox ( site ) ;
playerNameLabel . textContent = "id of the player container:" ;
playerNameEbox = mkebox ( site , uw _sites [ site ] . player . name ) ;
playerName . append ( playerNameLabel ) ;
playerName . append ( playerNameEbox ) ;
var playerClass = document . createElement ( "div" ) ;
var pcb = document . createElement ( "input" ) ;
pcb . type = "checkbox" ;
pcb . name = site + "_pccb_name" ;
pcb . id = site + "_pccb_id" ;
pcb . checked = uw _sites [ site ] . player . isClass ;
pcb . disabled = true ;
var pcblabel = document . createElement ( "span" ) ;
pcblabel . textContent = " Name of the player container is a class" ;
playerClass . append ( pcb ) ;
playerClass . append ( pcblabel ) ;
playerElement . append ( playerName ) ;
playerElement . append ( playerClass ) ;
}
var iframe _playerName = document . createElement ( "div" ) ;
var ipn _label = document . createElement ( "span" ) ;
ipn _label . textContent = "id of the player container when in an iframe:" ;
ipn _ebox = mkebox ( site , uw _sites [ site ] . iframe ? uw _sites [ site ] . iframe . name : "" ) ;
iframe _playerName . append ( ipn _label ) ;
iframe _playerName . append ( ipn _ebox ) ;
var iframe _playerClass = document . createElement ( "div" ) ;
var ipc _label = document . createElement ( "span" ) ;
ipc _label . textContent = " Name of the player container is a class" ;
var ipc _cb = mkcb ( site , uw _sites [ site ] . iframe ? uw _sites [ site ] . iframe . isClass : false ) ;
iframe _playerClass . append ( ipc _cb ) ;
iframe _playerClass . append ( ipc _label ) ;
var imdbar = document . createElement ( "div" ) ;
var imdbar _cb = mkcb ( site , uw _sites [ site ] . autoar _imdb ) ;
var imdbar _label = document . createElement ( "span" ) ;
imdbar _label . textContent = " This site supports automatic aspect ratio detection" ;
imdbar . append ( imdbar _cb ) ;
imdbar . append ( imdbar _label ) ;
var imdbar _title = document . createElement ( "div" ) ;
var it _label = document . createElement ( "span" ) ;
it _label . textContent = "id of the element containing video title:" ;
it _ebox = mkebox ( site , uw _sites [ site ] . iframe ? uw _sites [ site ] . iframe . name : "" ) ;
imdbar _title . append ( it _label ) ;
imdbar _title . append ( it _ebox ) ;
var imdbar _class = document . createElement ( "div" ) ;
var ic _label = document . createElement ( "span" ) ;
ic _label . textContent = " Name of the title container is a class" ;
var ic _cb = mkcb ( site , uw _sites [ site ] . iframe ? uw _sites [ site ] . iframe . isClass : false ) ;
imdbar _class . append ( ic _cb ) ;
imdbar _class . append ( ic _label ) ;
displayedInfo . append ( siteTitle ) ;
displayedInfo . append ( urlRules ) ;
displayedInfo . append ( playerElement ) ;
displayedInfo . append ( iframe _playerName ) ;
displayedInfo . append ( iframe _playerClass ) ;
displayedInfo . append ( imdbar ) ;
displayedInfo . append ( imdbar _title ) ;
displayedInfo . append ( imdbar _class ) ;
entry . append ( displayedInfo ) ;
list . append ( entry ) ;
category _counter ++ ;
}
}
if ( ! category _counter ) {
var noEntriesMsg = document . createElement ( "div" ) ;
noEntriesMsg . textContent = "There's no entries in this category yet" ;
noEntriesMsg . classList = "red" ;
list . append ( noEntriesMsg ) ;
}
}
}
function mkebox ( site , value ) {
var ebox = document . createElement ( "input" ) ;
ebox . className = "site_details details_ebox " + site + "_ebox" ,
ebox . type = "text" ;
ebox . value = value ;
ebox . disabled = true ;
return ebox ;
}
function mkcb ( site , checked , forceEnable ) {
var cb = document . createElement ( "input" ) ;
cb . type = "checkbox" ;
cb . name = site + "_cb_name" ;
cb . id = site + "_cb_id" ;
cb . checked = checked ;
if ( ! forceEnable )
cb . disabled = true ;
return cb ;
}
2016-12-07 23:58:43 +01:00
function loadopts ( ) {
2017-02-22 13:53:40 +01:00
getopt ( "ultrawidify_keybinds" , gotopts ) ;
// getopt("ultrawidify_autoar", gotar)
getopt ( "ultrawidify_ui" , gotui ) ;
2017-03-02 20:06:53 +01:00
getopt ( "ultrawidify_siterules" , gotsites ) ;
// We build ui for 'site options' here
// buildSites();
2016-12-07 23:58:43 +01:00
}
2017-01-19 21:29:54 +01:00
// page init
2016-12-07 23:58:43 +01:00
document . addEventListener ( "DOMContentLoaded" , loadopts ) ;
document . querySelector ( "#tab_shortcuts" ) . addEventListener ( "click" , showShortcuts ) ;
document . querySelector ( "#tab_about" ) . addEventListener ( "click" , showAbout ) ;
2017-01-19 21:29:54 +01:00
document . querySelector ( "#tab_general_settings" ) . addEventListener ( "click" , showGeneralSettings ) ;
2017-03-02 20:06:53 +01:00
document . querySelector ( "#tab_sites" ) . addEventListener ( "click" , showSites ) ;
2016-12-07 23:58:43 +01:00
document . querySelector ( "#kb_save" ) . addEventListener ( "click" , saveopts ) ;
document . querySelector ( "#kb_cancel" ) . addEventListener ( "click" , loadopts ) ;
2017-01-19 21:29:54 +01:00
document . querySelector ( "#enable_autoar" ) . addEventListener ( "click" , saveAutoar ) ;
2017-02-22 13:53:40 +01:00
document . querySelector ( "#enable_ui" ) . addEventListener ( "click" , saveUI ) ;
document . querySelector ( "#enable_ui_compact" ) . addEventListener ( "click" , saveUI ) ;
2017-01-21 18:31:47 +01:00
getopt ( "ultrawidify_autoar" , function ( obj ) { document . querySelector ( "#enable_autoar" ) . checked = obj . ultrawidify _autoar } ) ;