import { SubredditType , DataType } from './types' ; export { getVote , getDir , setIcon , handleClickOnly , toggleYoutube , detectTheme , getQueries , getPostArr , getSubreddits , getCommentArr , fixHref , decodeHtml , formatNumber , convertDate , } ; const getVote = ( likes ) => { if ( likes ) return 1 ; if ( likes === false ) return - 1 ; return 0 ; } ; const getDir = ( voteDir , vote ) => { let dir = voteDir ; if ( ( dir === 1 && vote === 1 ) || ( dir === - 1 && vote === - 1 ) ) { dir = 0 ; } return dir ; } ; const setIcon = ( postArr ) => { const icon = postArr . length ? '../images/logo_16.png' : '../images/logo_grey_16.png' ; chrome . action . setIcon ( { path : { 16 : icon , } , } ) ; } ; const handleClickOnly = ( ) => { chrome . storage . sync . get ( 'clickOnly' , ( { clickOnly } ) => { const value = ! clickOnly ; chrome . storage . sync . set ( { clickOnly : value } ) ; if ( value === true ) { chrome . action . setIcon ( { path : { 16 : '../images/logo_16.png' , } , } ) ; } } ) ; } ; const toggleYoutube = ( ) => { document . getElementById ( 'redComments' ) ! . style . display = 'none' ; document . getElementById ( 'comments' ) ! . style . display = 'block' ; document . getElementById ( 'redditImgWrap' ) ! . style . display = 'flex' ; window . scrollBy ( 0 , 1 ) ; } ; const detectTheme = ( ) => { if ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ) { document . documentElement . setAttribute ( 'data-theme' , 'dark' ) ; } chrome . storage . sync . get ( 'theme' , ( { theme } ) => { if ( theme === 'dark' || theme === 'light' ) { document . documentElement . setAttribute ( 'data-theme' , theme ) ; } } ) ; } ; const getQueries = ( url ) : string [ ] => { const queries = [ `https://api.reddit.com/submit?url=${ url }` ] ; if ( url . startsWith ( 'https' ) ) { queries . push ( `https://api.reddit.com/submit?url=${ url . replace ( 'https' , 'http' ) }` ) ; } else { queries . push ( `https://api.reddit.com/submit?url=${ url . replace ( 'http' , 'https' ) }` ) ; } for ( let i = 0 ; i < 2 ; i ++ ) { if ( url . endsWith ( '/' ) ) { queries . push ( queries [ i ] . slice ( 0 , - 1 ) ) ; } else { queries . push ( `${ queries [ i ] }/` ) ; } } for ( let i = 0 ; i < 4 ; i ++ ) { queries . push ( queries [ i ] . replace ( 'api.reddit.com/submit?url=' , 'www.reddit.com/api/info.json?url=' ) ) ; } if ( url . indexOf ( 'www.youtube.com/watch?v=' ) !== - 1 ) { for ( let i = 0 ; i < 8 ; i ++ ) { queries . push ( queries [ i ] . replace ( 'www.youtube.com/watch?v=' , 'youtu.be/' ) ) ; } } return queries ; } ; const getPostArr = async ( queries ) : Promise < DataType [ ] > => { const promisesFetch = [ ] ; const promisesJson = [ ] ; let postArr = [ ] ; for ( let i = 0 ; i < queries . length ; i ++ ) { promisesFetch . push ( fetch ( queries [ i ] ) ) ; } await Promise . all ( promisesFetch ) . then ( async ( resFetch ) => { for ( let i = 0 ; i < resFetch . length ; i ++ ) { promisesJson . push ( resFetch [ i ] . json ( ) ) ; } await Promise . all ( promisesJson ) . then ( ( resJson ) => { for ( let i = 0 ; i < resJson . length ; i ++ ) { if ( ( < any > resJson [ i ] ) . kind === 'Listing' && ( < any > resJson [ i ] ) . data . children . length > 0 ) { postArr = postArr . concat ( ( < any > resJson [ i ] ) . data . children ) ; } } postArr = [ ... new Map ( postArr . map ( ( item ) => [ item . data . id , item ] ) ) . values ( ) , ] ; postArr = postArr . sort ( compare ) ; } ) ; } ) ; return postArr ; } ; const compare = ( a , b ) : number => b . data . num_comments - a . data . num_comments ; const getSubreddits = ( data ) : SubredditType [ ] => { const subreddits = [ ] ; for ( let i = 0 ; i < data . length ; i ++ ) { subreddits . push ( { id : i , name : data [ i ] . data . subreddit , commentNum : `(${ data [ i ] . data . num_comments })` , } ) ; } return subreddits ; } ; const getCommentArr = async ( permalink ) : Promise < DataType [ ] > => { let commentArr = [ ] ; await fetch ( `https://api.reddit.com${ permalink }` ) . then ( ( response ) => response . json ( ) ) . then ( ( json ) => { if ( json && json [ 1 ] && json [ 1 ] . kind === 'Listing' && json [ 1 ] . data && json [ 1 ] . data . children . length ) { commentArr = json [ 1 ] . data . children ; } } ) ; return commentArr ; } ; const fixHref = ( html ) : string => { let fixed = decodeHtml ( html ) . replace ( '<a href=' , '<a target="_blank" href=' ) ; fixed = fixed . replace ( '<a target="_blank" href="/' , '<a target="_blank" href="https://reddit.com/' ) ; return fixed ; } ; const decodeHtml = ( html ) : string => { const txt = document . createElement ( 'textarea' ) ; txt . innerHTML = html ; return txt . value ; } ; const formatNumber = ( num ) => Math . abs ( num ) > 9999 ? `${ ( Math . sign ( num ) * ( Math . abs ( num ) / 1000 ) ) . toFixed ( 0 ) }k` : `${ Math . sign ( num ) * Math . abs ( num ) }` ; const convertDate = ( timestamp ) => { let diff = Date . now ( ) - timestamp * 1000 ; if ( diff < 1000 ) { return 'just now' ; } diff /= 1000 ; if ( diff < 60 ) { return `${ Math . trunc ( diff ) } second${ diff < 2 ? '' : 's' } ago` ; } diff /= 60 ; if ( diff < 60 ) { return `${ Math . trunc ( diff ) } minute${ diff < 2 ? '' : 's' } ago` ; } diff /= 60 ; if ( diff < 24 ) { return `${ Math . trunc ( diff ) } hour${ diff < 2 ? '' : 's' } ago` ; } diff /= 24 ; if ( diff < 7 ) { return `${ Math . trunc ( diff ) } day${ diff < 2 ? '' : 's' } ago` ; } diff /= 7 ; if ( diff < 4 ) { return `${ Math . trunc ( diff ) } week${ diff < 2 ? '' : 's' } ago` ; } diff /= 4 ; if ( diff < 13 ) { return `${ Math . trunc ( diff ) } month${ diff < 2 ? '' : 's' } ago` ; } diff /= 12 ; return `${ Math . trunc ( diff ) } year${ diff < 2 ? '' : 's' } ago` ; } ;