22
33document . addEventListener ( 'DOMContentLoaded' , initPopup )
44
5- document . querySelectorAll ( '[data-href]' ) . forEach ( ( el ) => {
6- el . addEventListener ( 'click' , popupLink )
7- } )
5+ const popupLinks = document . querySelectorAll ( '[data-href]' )
6+ popupLinks . forEach ( ( el ) => el . addEventListener ( 'click' , popLinks ) )
87
98/**
109 * Popup Init Function
@@ -19,6 +18,16 @@ async function initPopup() {
1918 return displayError ( 'Missing URL or Token.' )
2019 }
2120 document . getElementById ( 'django-files-links' ) . style . display = 'flex'
21+ console . log ( 'options.recentFiles:' , options . recentFiles )
22+ if ( options . recentFiles === '0' ) {
23+ document
24+ . getElementById ( 'loading-spinner' )
25+ . classList . add ( 'visually-hidden' )
26+ document
27+ . getElementById ( 'recent-uploads' )
28+ . classList . add ( 'visually-hidden' )
29+ return console . log ( 'Recent Files Disabled. Enable in Options.' )
30+ }
2231
2332 let opts = {
2433 method : 'GET' ,
@@ -38,6 +47,8 @@ async function initPopup() {
3847 }
3948 console . log ( `response.status: ${ response . status } ` , response , data )
4049
50+ document . getElementById ( 'loading-spinner' ) . classList . add ( 'visually-hidden' )
51+
4152 if ( ! response . ok ) {
4253 console . warn ( 'error: ' + data [ 'error' ] )
4354 return displayError ( data [ 'error' ] )
@@ -52,32 +63,42 @@ async function initPopup() {
5263 updateTable ( data )
5364
5465 const clipboard = new ClipboardJS ( '.clip' ) // eslint-disable-line
66+ // Re-Initialize data-href after updateTable
5567 document . querySelectorAll ( '[data-href]' ) . forEach ( ( el ) => {
56- el . addEventListener ( 'click' , popupLink )
68+ el . addEventListener ( 'click' , popLinks )
5769 } )
5870}
5971
6072/**
61- * Popup Links Callback
62- * because firefox needs us to call window.close() from the popup
63- * @function popupLink
73+ * Popup Links Click Callback
74+ * Firefox requires a call to window.close()
75+ * @function popLinks
6476 * @param {MouseEvent } event
6577 */
66- async function popupLink ( event ) {
67- console . log ( 'popupLink:' , event )
68- const { auth } = await chrome . storage . sync . get ( [ 'auth' ] )
69- let url
78+ async function popLinks ( event ) {
79+ console . log ( 'popLinks:' , event )
80+ event . preventDefault ( )
7081 const anchor = event . target . closest ( 'a' )
82+ let url
7183 if ( anchor ?. dataset ?. location ) {
84+ const { auth } = await chrome . storage . sync . get ( [ 'auth' ] )
7285 url = auth ?. url + anchor . dataset . location
73- } else if ( anchor ? .dataset ? .href . startsWith ( 'http' ) ) {
86+ } else if ( anchor . dataset . href . startsWith ( 'http' ) ) {
7487 url = anchor . dataset . href
75- } else {
88+ } else if ( anchor . dataset . href === 'homepage' ) {
89+ url = chrome . runtime . getManifest ( ) . homepage_url
90+ } else if ( anchor . dataset . href === 'options' ) {
91+ chrome . runtime . openOptionsPage ( )
92+ return window . close ( )
93+ } else if ( anchor ?. dataset ?. href ) {
7694 url = chrome . runtime . getURL ( anchor . dataset . href )
7795 }
78- console . log ( `url: ${ url } ` )
96+ console . log ( 'url:' , url )
97+ if ( ! url ) {
98+ return console . error ( 'No dataset.href for anchor:' , anchor )
99+ }
79100 await chrome . tabs . create ( { active : true , url } )
80- window . close ( )
101+ return window . close ( )
81102}
82103
83104/**
@@ -117,18 +138,38 @@ function updateTable(data) {
117138 copyBtn . setAttribute ( 'role' , 'button' )
118139 copyBtn . classList . add ( 'clip' )
119140 copyBtn . dataset . clipboardText = value
120- copyBtn . innerHTML = '<i class="fa-regular fa-clipboard text-white"></i>'
141+ copyBtn . innerHTML = '<i class="fa-regular fa-clipboard"></i>'
142+ copyBtn . classList . add ( 'link-body-emphasis' )
143+ copyBtn . onclick = clipClick
121144 const cell3 = row . insertCell ( )
122145 cell3 . appendChild ( copyBtn )
123146 } )
124147}
125148
149+ /**
150+ * Clipboard Click Callback
151+ * @function clipClick
152+ * @param {MouseEvent } event
153+ */
154+ function clipClick ( event ) {
155+ console . log ( 'clipClick:' , event )
156+ const element = event . target . closest ( 'a' )
157+ console . log ( 'element:' , element )
158+ element . classList . add ( 'link-success' )
159+ element . classList . remove ( 'link-body-emphasis' )
160+ setTimeout ( ( ) => {
161+ element . classList . add ( 'link-body-emphasis' )
162+ element . classList . remove ( 'link-success' )
163+ } , 500 )
164+ }
165+
126166/**
127167 * Display Popup Error Message
128168 * @function displayError
129169 * @param {String } message
130170 */
131171function displayError ( message ) {
172+ document . getElementById ( 'loading-spinner' ) . classList . add ( 'visually-hidden' )
132173 let div = document . getElementById ( 'error-alert' )
133174 div . innerHTML = message
134175 div . style . display = 'block'
0 commit comments