@@ -5,6 +5,8 @@ document.addEventListener('DOMContentLoaded', initPopup)
55const popupLinks = document . querySelectorAll ( '[data-href]' )
66popupLinks . forEach ( ( el ) => el . addEventListener ( 'click' , popLinks ) )
77
8+ chrome . runtime . onMessage . addListener ( onMessage )
9+
810/**
911 * Popup Init Function
1012 * TODO: Overhaul this function
@@ -15,20 +17,29 @@ async function initPopup() {
1517 const { auth, options } = await chrome . storage . sync . get ( [ 'auth' , 'options' ] )
1618 console . log ( 'auth:' , auth )
1719 if ( ! auth ?. url || ! auth ?. token ) {
18- return displayError ( 'Missing URL or Token.' )
20+ displayError ( 'Missing URL or Token.' )
21+ const [ tab ] = await chrome . tabs . query ( {
22+ currentWindow : true ,
23+ active : true ,
24+ } )
25+ console . log ( 'tab:' , tab )
26+ await chrome . scripting . executeScript ( {
27+ target : { tabId : tab . id } ,
28+ files : [ '/js/auth.js' ] ,
29+ } )
30+ return
1931 }
32+
2033 document . getElementById ( 'django-files-links' ) . style . display = 'flex'
21- console . log ( 'options.recentFiles:' , options . recentFiles )
34+
2235 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' )
2936 return console . log ( 'Recent Files Disabled. Enable in Options.' )
3037 }
3138
39+ document
40+ . getElementById ( 'loading-spinner' )
41+ . classList . remove ( 'visually-hidden' )
42+
3243 let opts = {
3344 method : 'GET' ,
3445 headers : { Authorization : auth . token } ,
@@ -61,6 +72,7 @@ async function initPopup() {
6172 }
6273
6374 updateTable ( data )
75+ document . getElementById ( 'recent' ) . classList . remove ( 'visually-hidden' )
6476
6577 const clipboard = new ClipboardJS ( '.clip' ) // eslint-disable-line
6678 // Re-Initialize data-href after updateTable
@@ -101,6 +113,38 @@ async function popLinks(event) {
101113 return window . close ( )
102114}
103115
116+ /**
117+ * On Command Callback
118+ * @function onMessage
119+ * @param {Object } message
120+ * @param {MessageSender } sender
121+ * @param {Function } sendResponse
122+ */
123+ async function onMessage ( message , sender , sendResponse ) {
124+ console . log ( 'message, sender, sendResponse:' , message , sender , sendResponse )
125+ if ( message ?. siteUrl && message ?. authToken ) {
126+ console . log ( `url: ${ message . siteUrl } ` )
127+ console . log ( `token: ${ message . authToken } ` )
128+ const auth = { url : message . siteUrl , token : message . authToken }
129+ await chrome . storage . local . set ( { auth } )
130+ const btn = document . getElementById ( 'auth-button' )
131+ btn . classList . remove ( 'visually-hidden' )
132+ btn . addEventListener ( 'click' , authCredentials )
133+ }
134+ }
135+
136+ async function authCredentials ( event ) {
137+ console . log ( 'authCredentials:' , event )
138+ const { auth } = await chrome . storage . local . get ( [ 'auth' ] )
139+ console . log ( 'auth:' , auth )
140+ if ( auth ) {
141+ await chrome . storage . sync . set ( { auth } )
142+ document . getElementById ( 'auth-button' ) . classList . add ( 'visually-hidden' )
143+ document . getElementById ( 'error-alert' ) . classList . add ( 'visually-hidden' )
144+ await initPopup ( )
145+ }
146+ }
147+
104148/**
105149 * Update Popup Table with Data
106150 * @function updateTable
0 commit comments