@@ -5,22 +5,30 @@ chrome.runtime.onMessage.addListener(onMessage)
55document
66 . querySelectorAll ( 'a[href]' )
77 . forEach ( ( el ) => el . addEventListener ( 'click' , popupLinks ) )
8+ document
9+ . querySelectorAll ( 'input' )
10+ . forEach ( ( el ) => el . addEventListener ( 'change' , saveOptions ) )
811document
912 . querySelectorAll ( '.add-auth' )
1013 . forEach ( ( el ) => el . addEventListener ( 'click' , authCredentials ) )
14+ document
15+ . querySelectorAll ( '[data-bs-toggle="tooltip"]' )
16+ . forEach ( ( el ) => new bootstrap . Tooltip ( el ) )
1117
1218const filesTable = document . getElementById ( 'files-table' )
1319const errorAlert = document . getElementById ( 'error-alert' )
1420const authButton = document . getElementById ( 'auth-button' )
1521const mediaImage = document . getElementById ( 'media-image' )
1622const mediaOuter = document . getElementById ( 'media-outer' )
17- const smallAuth = document . getElementById ( 'small -auth' )
23+ const alwaysAuth = document . getElementById ( 'always -auth' )
1824
1925const loadingImage = '../media/loading.gif'
2026let authError = false
27+ let timeoutID
28+ let timeout
2129
2230/**
23- * Popup Init Function
31+ * Initialize Popup
2432 * TODO: Overhaul this function
2533 * @function initPopup
2634 */
@@ -29,10 +37,13 @@ async function initPopup() {
2937 const { options } = await chrome . storage . sync . get ( [ 'options' ] )
3038 console . log ( 'options:' , options )
3139
40+ // Set Options (since this is the only one)
41+ document . getElementById ( 'popupPreview' ) . checked = options . popupPreview
42+
3243 authError = false
3344 // Check auth if checkAuth is enabled in options
3445 if ( options . checkAuth ) {
35- smallAuth . classList . remove ( 'd-none' )
46+ alwaysAuth . classList . remove ( 'd-none' )
3647 }
3748
3849 // If missing auth data or options.checkAuth check current site for auth
@@ -94,7 +105,7 @@ async function initPopup() {
94105
95106 // Check auth if checkAuth is enabled in options
96107 if ( options . checkAuth ) {
97- smallAuth . classList . remove ( 'd-none' )
108+ alwaysAuth . classList . remove ( 'd-none' )
98109 await checkSiteAuth ( )
99110 }
100111
@@ -111,7 +122,8 @@ async function initPopup() {
111122 // Enable Popup Mouseover Preview if popupPreview
112123 if ( options . popupPreview ) {
113124 console . log ( 'Enabling Mouseover Preview' )
114- initPopupMouseover ( options . popupTimeout )
125+ timeout = options . popupTimeout * 1000
126+ initPopupMouseover ( )
115127 }
116128}
117129
@@ -156,8 +168,8 @@ async function onMessage(message) {
156168 await chrome . storage . local . set ( { auth } )
157169 console . log ( 'New Authentication Found.' )
158170 if ( options . checkAuth ) {
159- smallAuth . classList . remove ( 'disabled' , 'btn-outline-secondary' )
160- smallAuth . classList . add ( 'btn-warning' )
171+ alwaysAuth . classList . remove ( 'disabled' , 'btn-outline-secondary' )
172+ alwaysAuth . classList . add ( 'btn-warning' )
161173 }
162174 if ( authError ) {
163175 authButton . classList . remove ( 'd-none' )
@@ -166,6 +178,33 @@ async function onMessage(message) {
166178 }
167179}
168180
181+ /**
182+ * Save Options Callback
183+ * @function saveOptions
184+ * @param {FormDataEvent } event
185+ */
186+ async function saveOptions ( event ) {
187+ // console.log('saveOptions:', event)
188+ const { options } = await chrome . storage . sync . get ( [ 'options' ] )
189+ options [ event . target . id ] = event . target . checked
190+ console . log ( `Set: "${ event . target . id } " to target:` , event . target )
191+ console . log ( 'options:' , options )
192+ await chrome . storage . sync . set ( { options } )
193+ if ( event . target . id === 'popupPreview' ) {
194+ if ( event . target . checked ) {
195+ console . log ( 'popupPreview Enabled. Running initPopupMouseover...' )
196+ initPopupMouseover ( )
197+ } else {
198+ console . log ( 'popupPreview Disabled. Removing Event Listeners...' )
199+ document . querySelectorAll ( '.link-underline' ) . forEach ( ( el ) => {
200+ el . removeEventListener ( 'mouseover' , onMouseOver )
201+ el . removeEventListener ( 'mouseout' , onMouseOut )
202+ } )
203+ mediaOuter . classList . add ( 'd-none' )
204+ }
205+ }
206+ }
207+
169208/**
170209 * Add Site Auth Button Callback
171210 * @function authCredentials
@@ -183,11 +222,8 @@ async function authCredentials(event) {
183222 console . log ( 'Auth Credentials Updated...' )
184223 authButton . classList . add ( 'd-none' )
185224 errorAlert . classList . add ( 'd-none' )
186- smallAuth . classList . add ( 'disabled' , 'btn-outline-secondary' )
225+ alwaysAuth . classList . add ( 'disabled' , 'btn-outline-secondary' )
187226 await initPopup ( )
188- try {
189- await chrome . runtime . sendMessage ( 'reload-options' )
190- } catch ( e ) { } // eslint-disable-line no-empty
191227 } else {
192228 displayAlert ( { message : 'Error Getting or Setting Credentials.' } )
193229 }
@@ -337,16 +373,8 @@ async function checkSiteAuth() {
337373 } catch ( e ) { } // eslint-disable-line no-empty
338374}
339375
340- /**
341- * Initialize Popup Mouseover Preview
342- * @param {Number } timeout
343- */
344- function initPopupMouseover ( timeout ) {
345- timeout = timeout * 1000 || 1
346- console . log ( 'initPopupMouseover: timeout:' , timeout )
347-
348- let timeoutID
349-
376+ function initPopupMouseover ( ) {
377+ console . log ( 'initPopupMouseover' )
350378 mediaOuter . addEventListener ( 'mouseover' , ( ) => {
351379 mediaOuter . classList . add ( 'd-none' )
352380 mediaImage . src = loadingImage
@@ -358,49 +386,43 @@ function initPopupMouseover(timeout) {
358386 console . log ( 'mediaError:' , event )
359387 mediaImage . src = '../media/error.png'
360388 } )
361-
362389 document . querySelectorAll ( '.link-underline' ) . forEach ( ( el ) => {
363390 el . addEventListener ( 'mouseover' , onMouseOver )
364391 el . addEventListener ( 'mouseout' , onMouseOut )
365392 } )
393+ }
366394
367- function onMouseOver ( event ) {
368- // console.log('onMouseOver:', event)
369- if ( event . pageY < window . innerHeight / 2 ) {
370- mediaOuter . classList . remove ( 'top-0' )
371- mediaOuter . classList . add ( 'bottom-0' )
372- } else {
373- mediaOuter . classList . remove ( 'bottom-0' )
374- mediaOuter . classList . add ( 'top-0' )
375- }
376- // console.log('name:', event.target.innerText)
377- // console.log('raw:', event.target.dataset.raw)
378- const str = event . target . innerText
379- const imageExtensions = / \. ( g i f | i c o | j p e g | j p g | p n g | s v g | w e b p ) $ / i
380- if ( str . match ( imageExtensions ) ) {
381- mediaImage . src = loadingImage
382- mediaImage . src = event . target . dataset . raw
383- mediaOuter . classList . remove ( 'd-none' )
384- } else {
385- mediaOuter . classList . add ( 'd-none' )
386- mediaImage . src = loadingImage
387- }
388- // console.log('timeoutID:', timeoutID)
389- if ( timeoutID ) {
390- clearTimeout ( timeoutID )
391- }
395+ function onMouseOver ( event ) {
396+ // console.log('onMouseOver:', event)
397+ if ( event . pageY < window . innerHeight / 2 ) {
398+ mediaOuter . classList . remove ( 'top-0' )
399+ mediaOuter . classList . add ( 'bottom-0' )
400+ } else {
401+ mediaOuter . classList . remove ( 'bottom-0' )
402+ mediaOuter . classList . add ( 'top-0' )
392403 }
393-
394- function onMouseOut ( ) {
395- timeoutID = setTimeout ( function ( ) {
396- mediaOuter . classList . add ( 'd-none' )
397- mediaImage . src = loadingImage
398- timeoutID = undefined
399- } , timeout )
404+ // console.log('name:', event.target.innerText)
405+ // console.log('raw:', event.target.dataset.raw)
406+ const str = event . target . innerText
407+ const imageExtensions = / \. ( g i f | i c o | j p e g | j p g | p n g | s v g | w e b p ) $ / i
408+ if ( str . match ( imageExtensions ) ) {
409+ mediaImage . src = loadingImage
410+ mediaImage . src = event . target . dataset . raw
411+ mediaOuter . classList . remove ( 'd-none' )
412+ } else {
413+ mediaOuter . classList . add ( 'd-none' )
414+ mediaImage . src = loadingImage
415+ }
416+ // console.log('timeoutID:', timeoutID)
417+ if ( timeoutID ) {
418+ clearTimeout ( timeoutID )
400419 }
401420}
402421
403- // function mediaError(event) {
404- // console.log('mediaError:', event)
405- // mediaImage.src = '../media/error.png'
406- // }
422+ function onMouseOut ( ) {
423+ timeoutID = setTimeout ( function ( ) {
424+ mediaOuter . classList . add ( 'd-none' )
425+ mediaImage . src = loadingImage
426+ timeoutID = undefined
427+ } , timeout )
428+ }
0 commit comments