Last active
December 12, 2023 17:55
-
-
Save lyellick/49ad04991450cfe9a734a9add0fb11d2 to your computer and use it in GitHub Desktop.
Hudu KB Exploder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const key = ""; // Your Hudu API Key | |
const wrapper = document.getElementsByClassName("index__folders")[0]; | |
const params = new URLSearchParams(window.location.search); | |
const companyIdParam = params.get('company_id'); | |
const folderParam = params.get('folder'); | |
if (companyIdParam && folderParam === null && location.pathname === "/kba") { | |
await showTree(companyIdParam); | |
} | |
async function showTree(companyIdParam) { | |
const companyId = parseInt(companyIdParam); | |
const articles = await fetchApiData(`/api/v1/articles?company_id=${companyId}`); | |
const folders = await fetchApiData(`/api/v1/folders?company_id=${companyId}`); | |
const tree = buildTree(folders, articles); | |
wrapper.innerHTML = ""; | |
tree.forEach(rootNode => { | |
walkData(rootNode); | |
}); | |
} | |
async function fetchApiData(url) { | |
const response = await fetch(url, { headers: { "x-api-key": key } }); | |
const data = await response.json(); | |
return data.articles || data.folders || []; | |
} | |
function buildTree(data, articles, parentId = null) { | |
const folders = data.filter(item => item.parent_folder_id === parentId); | |
return folders.map(folder => { | |
const children = buildTree(data, articles, folder.id); | |
const found = articles.filter(item => item.folder_id === folder.id); | |
folder.children = children || []; | |
folder.articles = found || []; | |
return folder; | |
}); | |
} | |
function walkData(node, level = 0) { | |
wrapper.innerHTML += `<a style="margin-left: ${level}rem;" href="/kba?company_id=${node.company_id}&folder=${node.id}" class="index__folder"><h1><i class="fas fa-folder"></i>${node.name}</h1> <p>${node.description}</p></a>`; | |
if (node.articles.length > 0) { | |
node.articles.forEach(article => { | |
wrapper.innerHTML += `<a style="margin-left: ${level + 1}rem;" href="${article.url}" class="index__folder"><h1>${article.name}</h1></a>`; | |
}); | |
} | |
if (node.children.length > 0) { | |
node.children.forEach(child => { | |
walkData(child, level + 1); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment