@@ -6,48 +6,31 @@ document.querySelectorAll('[data-href]').forEach((el) => {
66 el . addEventListener ( 'click' , popupLink )
77} )
88
9- /**
10- * Popup Links Callback
11- * because firefox needs us to call window.close() from the popup
12- * @function popupLink
13- * @param {MouseEvent } event
14- */
15- async function popupLink ( event ) {
16- console . log ( 'popupLink:' , event )
17- const { auth } = await chrome . storage . sync . get ( [ 'auth' ] )
18- let url
19- if ( event . target . dataset . location ) {
20- url = auth ?. url + event . target . dataset . location
21- } else if ( event . target . dataset . href . startsWith ( 'http' ) ) {
22- url = event . target . dataset . href
23- } else {
24- url = chrome . runtime . getURL ( event . target . dataset . href )
25- }
26- console . log ( `url: ${ url } ` )
27- await chrome . tabs . create ( { active : true , url } )
28- window . close ( )
29- }
30-
319/**
3210 * Popup Init Function
3311 * TODO: Overhaul this function
3412 * @function initPopup
3513 */
3614async function initPopup ( ) {
3715 console . log ( 'initPopup' )
38- const { auth } = await chrome . storage . sync . get ( [ 'auth' ] )
16+ const { auth, options } = await chrome . storage . sync . get ( [ 'auth' , 'options '] )
3917 console . log ( 'auth:' , auth )
4018 if ( ! auth ?. url || ! auth ?. token ) {
4119 return displayError ( 'Missing URL or Token.' )
4220 }
4321 document . getElementById ( 'django-files-links' ) . style . display = 'flex'
4422
45- let headers = { Authorization : auth . token }
46- let options = { method : 'GET' , headers : headers , cache : 'no-cache' }
23+ let opts = {
24+ method : 'GET' ,
25+ headers : { Authorization : auth . token } ,
26+ cache : 'no-cache' ,
27+ }
4728 let response
4829 let data
4930 try {
50- response = await fetch ( auth . url + '/api/recent/' , options )
31+ const url = new URL ( auth . url + '/api/recent/' )
32+ url . searchParams . append ( 'amount' , options ?. recentFiles || '10' )
33+ response = await fetch ( url , opts )
5134 data = await response . json ( )
5235 } catch ( error ) {
5336 console . warn ( error )
@@ -66,36 +49,80 @@ async function initPopup() {
6649 return displayError ( 'No Files Returned.' )
6750 }
6851
52+ updateTable ( data )
53+ const clipboard = new ClipboardJS ( '.clip' ) // eslint-disable-line
54+ document . querySelectorAll ( '[data-href]' ) . forEach ( ( el ) => {
55+ el . addEventListener ( 'click' , popupLink )
56+ } )
57+ }
58+
59+ /**
60+ * Popup Links Callback
61+ * because firefox needs us to call window.close() from the popup
62+ * @function popupLink
63+ * @param {MouseEvent } event
64+ */
65+ async function popupLink ( event ) {
66+ console . log ( 'popupLink:' , event )
67+ const { auth } = await chrome . storage . sync . get ( [ 'auth' ] )
68+ let url
69+ if ( event . target . dataset . location ) {
70+ url = auth ?. url + event . target . dataset . location
71+ } else if ( event . target . dataset . href . startsWith ( 'http' ) ) {
72+ url = event . target . dataset . href
73+ } else {
74+ url = chrome . runtime . getURL ( event . target . dataset . href )
75+ }
76+ console . log ( `url: ${ url } ` )
77+ await chrome . tabs . create ( { active : true , url } )
78+ window . close ( )
79+ }
80+
81+ /**
82+ * Update Popup Table with Data
83+ * @function updateTable
84+ * @param {Object } data
85+ */
86+ function updateTable ( data ) {
6987 let tbodyRef = document
7088 . getElementById ( 'recent' )
7189 . getElementsByTagName ( 'tbody' ) [ 0 ]
7290
7391 data . forEach ( function ( value , i ) {
74- let name = String ( value . split ( '/' ) . reverse ( ) [ 0 ] )
92+ const name = String ( value . split ( '/' ) . reverse ( ) [ 0 ] )
93+ const row = tbodyRef . insertRow ( )
7594
76- let copyLink = document . createTextNode ( i + 1 )
77- // let copyLink = document.createElement('a')
78- // copyLink.text = 1 + i
79- // copyLink.title = name
80- // copyLink.href = value
81- // copyLink.target = '_blank'
95+ const copyLink = document . createTextNode ( i + 1 )
96+ const cell1 = row . insertCell ( )
97+ cell1 . appendChild ( copyLink )
8298
83- let fileLink = document . createElement ( 'a' )
99+ const fileLink = document . createElement ( 'a' )
84100 fileLink . text = name
85101 fileLink . title = name
86- fileLink . href = value
102+ fileLink . dataset . href = value
103+ fileLink . setAttribute ( 'role' , 'button' )
104+ fileLink . classList . add (
105+ 'link-underline' ,
106+ 'link-underline-opacity-0' ,
107+ 'link-underline-opacity-75-hover'
108+ )
87109 fileLink . target = '_blank'
88-
89- let row = tbodyRef . insertRow ( )
90- let cell1 = row . insertCell ( )
91- cell1 . appendChild ( copyLink )
92- let cell2 = row . insertCell ( )
110+ const cell2 = row . insertCell ( )
93111 cell2 . appendChild ( fileLink )
112+
113+ const copyBtn = document . createElement ( 'a' )
114+ copyBtn . title = 'Copy'
115+ copyBtn . setAttribute ( 'role' , 'button' )
116+ copyBtn . classList . add ( 'clip' )
117+ copyBtn . dataset . clipboardText = value
118+ copyBtn . innerHTML = '<i class="fa-regular fa-clipboard text-white"></i>'
119+ const cell3 = row . insertCell ( )
120+ cell3 . appendChild ( copyBtn )
94121 } )
95122}
96123
97124/**
98- * Popup Action Init
125+ * Display Popup Error Message
99126 * @function displayError
100127 * @param {String } message
101128 */
0 commit comments