11/* eslint-disable no-console */
2- /* global authButtonClick browser Notify */
2+ /* global browser Notify */
33
44const notify = new Notify ( document . querySelector ( '#notify' ) ) ;
55async function mainLoaded ( ) {
@@ -11,6 +11,9 @@ async function mainLoaded() {
1111 document
1212 . getElementById ( 'authButton' )
1313 . addEventListener ( 'click' , authButtonClick , false ) ;
14+ document
15+ . getElementById ( 'tokenGrabber' )
16+ . addEventListener ( 'click' , tokenGrabberClick , false ) ;
1417 document
1518 . getElementById ( 'logoutButton' )
1619 . addEventListener ( 'click' , logout , false ) ;
@@ -59,9 +62,7 @@ async function querySecrets(vaultServerAdress, vaultToken, policies) {
5962 ) ;
6063 if ( ! fetchListOfSecretDirs . ok ) {
6164 const returnText = await fetchListOfSecretDirs . text ( ) ;
62- notify . error (
63- `Fetching list of secret directories failed: ${ returnText } `
64- ) ;
65+ notify . error ( `Fetching list of secret directories failed: ${ returnText } ` ) ;
6566 throw new Error (
6667 `Fetching list of secret directories failed: ${ returnText } `
6768 ) ;
@@ -223,4 +224,32 @@ async function authButtonClick() {
223224 }
224225}
225226
227+ async function tokenGrabberClick ( ) {
228+ var tabs = await browser . tabs . query ( { active : true , currentWindow : true } ) ;
229+ for ( let tabIndex = 0 ; tabIndex < tabs . length ; tabIndex ++ ) {
230+ var tab = tabs [ tabIndex ] ;
231+ if ( tab . url ) {
232+ browser . tabs . sendMessage ( tab . id , {
233+ message : 'fetch_token' ,
234+ } ) ;
235+ break ;
236+ }
237+ }
238+ }
239+
226240document . addEventListener ( 'DOMContentLoaded' , mainLoaded , false ) ;
241+
242+ browser . runtime . onMessage . addListener ( async function ( message ) {
243+ switch ( message . type ) {
244+ case 'fetch_token' :
245+ await browser . storage . local . set ( { vaultToken : message . token } ) ;
246+ await browser . storage . sync . set ( { vaultAddress : message . address } ) ;
247+ await querySecrets ( message . address , message . token , message . policies ) ;
248+ break ;
249+ case 'token_missing' :
250+ notify . error ( 'Failed to find Vault info from current tab' ) ;
251+ break ;
252+ default :
253+ break ;
254+ }
255+ } ) ;
0 commit comments