1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
async function _populateSocialMediaIcons()
{
// helper to grab a url and convert it to a js object
async function _fetchJson(url)
{
console.log(`fetching: ${url}`)
let response = await fetch(url)
if (!response.ok)
{
throw Error(`unable to retrieve ${url}: ${response.status}`)
}
return response.json()
}
try
{
// sphinx happily puts a relative path to the project root in each of its html files :)
const root = document.getElementById("documentation_options").getAttribute('data-url_root')
if (!root)
{
throw new Error("unable to find data-url_root in header")
}
// grab project information
const project = await _fetchJson(`${root}project.json`)
const site2icon = {
"gitlab": "",
"github": "",
"youtube": "",
"twitter": "",
"instagram": "",
"www": "",
"linkedin": "",
"twitch": "",
"facebook": "",
"pinterest": "",
"bitbucket": "",
}
if (!("social_media" in project) || (project.social_media.length < 1))
{
console.log("project does not define 'social_media'. no social media icon will be shown.")
return
}
const elems = document.querySelectorAll("li.wy-breadcrumbs-aside")
if (elems.length < 1)
{
console.log("could not find breadcrumb aside to place social media icons")
return
}
const elem = elems[0]
elem.classList.add("fa")
html = ""
for (let i = 0; i < project.social_media.length; i++)
{
sm = project.social_media[i]
let site = sm.site
let url = sm.url
if (site in site2icon)
{
site = site2icon[site]
}
html += `<span class='social-media-icon'><a href='${url}'>${site}</a></span>`
}
elem.innerHTML = html
}
catch (e)
{
console.log("warning: failed to populate social media icons:", e)
}
}
// wait until the page is loaded to modify the DOM
window.addEventListener("load", _populateSocialMediaIcons)
|