Skip to content

Commit 50f663f

Browse files
committed
start script when message is received,included nodehtmlparser, added some new/more logic to the functions: checkmodal > find endpoints > create tree
1 parent 7920c8b commit 50f663f

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

index.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
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');
33

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+
})
417
//Make a directory tree and inject it into HTML
518
const createTree = async (paths) => {
6-
container.innerHTML = render(
19+
document.querySelector('#hubtree-modal-inner').innerHTML = render(
720
paths,
821
'',
922
(parent, file, explicit) => {
@@ -13,26 +26,46 @@ const createTree = async (paths) => {
1326
)
1427
}
1528

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
1650
const getName = async () => {
1751
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;
1955
throw new Error('Name does not exist')
2056
}
21-
57+
//Go to find file to get the path for the tree-list endpoint
2258
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
2464
}
25-
65+
//Make a request to the endpoint that gives us all files strings in an array.
2666
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

Comments
 (0)