import { setIcon , getQueries , getPostArr , getCommentArr } from './main' ; import { DataType } from './types' ; chrome . tabs . onUpdated . addListener ( ( tabId , change , tab ) => { chrome . storage . sync . get ( 'clickOnly' , ( { clickOnly } ) => { if ( ! clickOnly ) { const queries = getQueries ( tab . url ! ) ; getPostArr ( queries ) . then ( ( postArr ) => { setIcon ( postArr ) ; } ) ; } } ) ; } ) ; chrome . tabs . onActivated . addListener ( ( ) => { chrome . storage . sync . get ( 'clickOnly' , ( { clickOnly } ) => { if ( ! clickOnly ) { chrome . tabs . query ( { active : true , lastFocusedWindow : true } , ( tabs ) => { const queries = getQueries ( tabs [ 0 ] . url ! ) ; getPostArr ( queries ) . then ( ( postArr ) => { setIcon ( postArr ) ; } ) ; } ) ; } } ) ; } ) ; chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => { if ( request . queries ) { getPostArr ( request . queries ) . then ( ( postArr ) => { sendResponse ( { postArr } ) ; } ) . catch ( ( ) => sendResponse ( 'err' ) ) ; } else if ( request . children ) { const promisesFetch = [ ] ; for ( let i = 0 ; i < request . children . length ; i ++ ) { promisesFetch . push ( getCommentArr ( request . permalink + request . children [ i ] ) ) ; } Promise . all ( promisesFetch ) . then ( ( value ) => { sendResponse ( { value } ) ; } ) ; } else if ( request . permalink ) { getCommentArr ( request . permalink ) . then ( ( commentArr ) => { sendResponse ( { commentArr } ) ; } ) ; } else if ( request . id ) { sendVote ( request . id , request . dir ) ; } return true ; } ) ; const sendVote = async ( id , dir ) => { const modhash = await getModhash ( ) ; const data = { dir , id , rank : 2 , uh : modhash , } ; const formData = new FormData ( ) ; if ( data ) { Object . keys ( data ) . forEach ( ( key ) => formData . append ( key , data [ key as keyof typeof data ] ) ) ; } fetch ( 'https://api.reddit.com/api/vote' , { method : 'POST' , body : formData , } ) ; } ; const getModhash = ( ) => fetch ( 'https://api.reddit.com/api/me.json' ) . then ( ( response ) => response . json ( ) ) . then ( ( json ) => json . data . modhash ) ;