@@ -32,6 +32,7 @@ async function initOptions() {
3232
3333 const { options } = await chrome . storage . sync . get ( [ 'options' ] )
3434 console . debug ( 'options:' , options )
35+ setBackground ( options )
3536 updateOptions ( options )
3637 if ( ! options ?. siteUrl ) {
3738 const siteUrl = document . getElementById ( 'siteUrl' )
@@ -48,13 +49,37 @@ async function initOptions() {
4849 */
4950function onChanged ( changes , namespace ) {
5051 // console.debug('onChanged:', changes, namespace)
51- for ( const [ key , { newValue } ] of Object . entries ( changes ) ) {
52+ for ( const [ key , { oldValue , newValue } ] of Object . entries ( changes ) ) {
5253 if ( namespace === 'sync' && key === 'options' ) {
5354 updateOptions ( newValue )
55+ if ( oldValue . radioBackground !== newValue . radioBackground ) {
56+ setBackground ( newValue )
57+ }
5458 }
5559 }
5660}
5761
62+ function setBackground ( options ) {
63+ if ( options . radioBackground === 'bgPicture' ) {
64+ console . debug ( 'setBackground:' , options )
65+ document . body . style . background =
66+ "url('https://picsum.photos/1920/1080') no-repeat center fixed"
67+ document . body . style . webkitBackgroundSize = 'cover'
68+ document . body . style . mozBackgroundSize = 'cover'
69+ document . body . style . oBackgroundSize = 'cover'
70+ document . body . style . backgroundSize = 'cover'
71+
72+ document . querySelector ( 'video' ) . classList . add ( 'd-none' )
73+ } else if ( options . radioBackground === 'bgVideo' ) {
74+ document . querySelector ( 'video' ) . classList . remove ( 'd-none' )
75+
76+ document . body . style . cssText = ''
77+ } else {
78+ document . body . style . cssText = ''
79+ document . querySelector ( 'video' ) . classList . add ( 'd-none' )
80+ }
81+ }
82+
5883/**
5984 * Save Options Callback
6085 * @function saveOptions
@@ -63,11 +88,13 @@ function onChanged(changes, namespace) {
6388async function saveOptions ( event ) {
6489 // console.debug('saveOptions:', event)
6590 const { options } = await chrome . storage . sync . get ( [ 'options' ] )
91+ let key = event . target . id
92+ let value
6693 if ( event . target . type === 'checkbox' ) {
67- options [ event . target . id ] = event . target . checked
94+ value = event . target . checked
6895 } else if ( event . target . id === 'siteUrl' ) {
6996 event . target . value = event . target . value . replace ( / \/ + $ / , '' )
70- options [ event . target . id ] = event . target . value
97+ value = event . target . value
7198 } else if ( event . target . type === 'number' ) {
7299 const number = parseInt ( event . target . value , 10 )
73100 let min = 0
@@ -80,18 +107,31 @@ async function saveOptions(event) {
80107 }
81108 if ( ! isNaN ( number ) && number >= min && number <= max ) {
82109 event . target . value = number . toString ( )
83- options [ event . target . id ] = number
110+ value = number
84111 } else {
85112 event . target . value = options [ event . target . id ]
86113 // TODO: Add Error Handling
87114 // showToast(`Value ${number} Out of Range for ${event.target.id}`,'warning')
88115 }
116+ } else if ( event . target . type === 'radio' ) {
117+ key = event . target . name
118+ const radios = document . getElementsByName ( key )
119+ for ( const input of radios ) {
120+ if ( input . checked ) {
121+ value = input . id
122+ break
123+ }
124+ }
89125 } else {
90- options [ event . target . id ] = event . target . value
126+ value = event . target . value
127+ }
128+ if ( value !== undefined ) {
129+ options [ key ] = value
130+ console . info ( `Set: ${ key } :` , value )
131+ await chrome . storage . sync . set ( { options } )
132+ } else {
133+ console . warn ( 'No Value for key:' , key )
91134 }
92- console . info ( `Set: "${ event . target . id } " to target:` , event . target )
93- console . debug ( 'options:' , options )
94- await chrome . storage . sync . set ( { options } )
95135}
96136
97137/**
@@ -117,7 +157,7 @@ function updateOptions(options) {
117157 }
118158 if ( el . tagName !== 'INPUT' ) {
119159 el . textContent = value . toString ( )
120- } else if ( el . type === 'checkbox' ) {
160+ } else if ( [ 'checkbox' , 'radio' ] . includes ( el . type ) ) {
121161 el . checked = value
122162 } else {
123163 el . value = value
0 commit comments