1
- const { render} = require ( 'tree-from-paths' )
2
- const container = document . querySelector ( '#container ') ;
1
+ const { render} = require ( 'tree-from-paths' ) ;
2
+ const { parse } = require ( 'node-html-parser ') ;
3
3
4
+ chrome . runtime . onMessage . addListener ( ( { action} , sender , sendRes ) => {
5
+ if ( action === 'run_main' ) {
6
+ modalShown ( )
7
+ . then ( getName )
8
+ . then ( goToFindFile )
9
+ . then ( goToPathEndpoint )
10
+ . then ( createTree )
11
+ . catch ( err => {
12
+ //TODO - Maybe do some clean up here
13
+ console . log ( err ) ;
14
+ } )
15
+ }
16
+ } )
4
17
//Make a directory tree and inject it into HTML
5
18
const createTree = async ( paths ) => {
6
- container . innerHTML = render (
19
+ document . querySelector ( '#hubtree-modal-inner' ) . innerHTML = render (
7
20
paths ,
8
21
'' ,
9
22
( parent , file , explicit ) => {
@@ -13,26 +26,46 @@ const createTree = async (paths) => {
13
26
)
14
27
}
15
28
29
+ //Add modal to webpage
30
+ const addModal = async ( ) => {
31
+ const modalDiv = document . createElement ( 'div' ) ;
32
+ modalDiv . id = 'hubtree-modal' ;
33
+ modalDiv . classList . add ( 'hubtree-modal' ) ;
34
+ const modalInnerHtml = `<div id="hubtree-modal-inner" class="hubtree-modal-inner"> </div>`
35
+ modalDiv . innerHTML = modalInnerHtml ;
36
+ document . body . appendChild ( modalDiv ) ;
37
+ }
38
+ //Check if modal is shown
39
+ const modalShown = async ( ) => {
40
+ const modal = document . querySelector ( '#hubtree-modal' ) ;
41
+ //If shown remove modal, if not shown create
42
+ if ( modal ) {
43
+ modal . parentNode . removeChild ( modal )
44
+ throw new Error ( 'Closed Modal' ) ;
45
+ }
46
+ await addModal ( )
47
+ return true
48
+ }
49
+ //Get the name of the repository
16
50
const getName = async ( ) => {
17
51
const name = document . querySelector ( 'meta[property="og:title"]' ) . content ;
18
- if ( name ) return name ;
52
+ console . log ( name )
53
+ //TODO - There is probably a better way to validate being in a repository
54
+ if ( name && name . includes ( '/' ) ) return name ;
19
55
throw new Error ( 'Name does not exist' )
20
56
}
21
-
57
+ //Go to find file to get the path for the tree-list endpoint
22
58
const goToFindFile = async ( name ) => {
23
-
59
+ const findFileUrl = `https://github.com/${ name } /find/master` ;
60
+ const htmlStr = await fetch ( findFileUrl ) . then ( res => res . text ( ) ) ;
61
+ const root = parse ( htmlStr ) ;
62
+ const path = root . querySelector ( 'fuzzy-list' ) . getAttribute ( 'data-url' ) ;
63
+ return path
24
64
}
25
-
65
+ //Make a request to the endpoint that gives us all files strings in an array.
26
66
const goToPathEndpoint = async ( endpoint ) => {
27
-
28
- }
29
-
30
- //Function calls
31
- getName ( )
32
- . then ( goToFindFile )
33
- . then ( goToPathEndpoint )
34
- . then ( createTree )
35
- . catch ( err => {
36
- console . log ( err ) ;
37
- container . innerHTML = 'Oops :/'
38
- } )
67
+ const getPathsUrl = `https://github.com${ endpoint } ` ;
68
+ const paths = ( await fetch ( getPathsUrl ) . then ( res => res . json ( ) ) ) . paths ;
69
+ if ( paths ) return paths ;
70
+ throw new Error ( 'No paths' ) ;
71
+ }
0 commit comments