Skip to content

Commit fad84c2

Browse files
committed
script now uses github tree api instead of requesting the page and parsing html, removed import of node-html-parser as it is no longer needed
1 parent c2d78d1 commit fad84c2

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

index.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
const {render} = require('tree-from-paths');
2-
const { parse } = require( 'node-html-parser');
32

43
chrome.runtime.onMessage.addListener(({action}, sender, sendRes)=>{
54
if(action ==='run_main'){
65
modalShown()
76
.then(getName)
8-
.then(goToFindFile)
9-
.then(goToPathEndpoint)
7+
.then(getTree)
108
.then(createTree)
119
.catch(err=>{
1210
//TODO - Maybe do some clean up here
@@ -15,16 +13,7 @@ chrome.runtime.onMessage.addListener(({action}, sender, sendRes)=>{
1513
}
1614
})
1715
//Make a directory tree and inject it into HTML
18-
const createTree = async (paths) => {
19-
document.querySelector('#hubtree-modal-inner').innerHTML = render(
20-
paths,
21-
'',
22-
(parent, file, explicit) => {
23-
// return `<a class='link' href='${parent}${file}'>${parent}${file}</a><br>`
24-
return `${parent}${file}<br>`
25-
}
26-
)
27-
}
16+
const createTree = async (paths) => document.querySelector('#hubtree-modal-inner').innerHTML = render( paths, '',(parent, file, explicit) => `${file}<br>`);
2817

2918
//Add modal to webpage
3019
const addModal = async () =>{
@@ -54,18 +43,14 @@ const getName = async () => {
5443
if(name && name.includes('/')) return name;
5544
throw new Error('Name does not exist')
5645
}
57-
//Go to find file to get the path for the tree-list endpoint
58-
const goToFindFile = async (name) =>{
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
64-
}
65-
//Make a request to the endpoint that gives us all files strings in an array.
66-
const goToPathEndpoint = async (endpoint) =>{
67-
const getPathsUrl = `https://github.com${endpoint}`;
68-
const paths = (await fetch(getPathsUrl).then(res=>res.json())).paths;
69-
if(paths) return paths;
46+
47+
const getTree = async (name) =>{
48+
const endpoint = `https://api.github.com/repos/${name}/git/trees/master?recursive=1`
49+
const treeData = await fetch(endpoint).then(res=>{
50+
if(res.ok) return res.json();
51+
throw new Error('Problem with request')
52+
})
53+
const paths = treeData.tree.map(item=>item.path);
54+
if(paths.length > 0) return paths;
7055
throw new Error('No paths');
7156
}

0 commit comments

Comments
 (0)