2020-03-05 03:11:15 +01:00
|
|
|
async function init() {
|
2020-03-06 03:10:05 +01:00
|
|
|
const response = await fetch('/api/github')
|
|
|
|
const release = await response.json()
|
2020-03-05 03:11:15 +01:00
|
|
|
replaceDom(release)
|
|
|
|
}
|
|
|
|
window.addEventListener('load', () => init())
|
|
|
|
|
2020-02-26 23:28:34 +01:00
|
|
|
function replaceDom(release) {
|
|
|
|
if (!release) return
|
|
|
|
|
|
|
|
const releaseTagElement = document.querySelector('.release__tag')
|
|
|
|
const releaseDateElement = document.querySelector('.release__date')
|
|
|
|
const downloadsElement = document.querySelector('.downloads')
|
|
|
|
const dateFormatted = new Date(release.published_at).toLocaleDateString()
|
|
|
|
|
|
|
|
releaseTagElement.innerHTML = release.tag_name
|
2020-03-05 03:11:15 +01:00
|
|
|
releaseDateElement.innerHTML = `on ${dateFormatted}`
|
2020-02-26 23:28:34 +01:00
|
|
|
|
|
|
|
downloadsElement.innerHTML = ''
|
|
|
|
|
2020-03-05 03:11:15 +01:00
|
|
|
const isMac = navigator.userAgent.includes('Mac OS')
|
|
|
|
const isWin = navigator.userAgent.includes('Windows')
|
|
|
|
const isLinux = navigator.userAgent.includes('Linux')
|
|
|
|
|
2020-03-11 04:08:32 +01:00
|
|
|
const downloadAll = release.downloads.map(download => {
|
2020-03-05 03:11:15 +01:00
|
|
|
const isTargetOs =
|
|
|
|
isMac & download.name.includes('mac') ||
|
|
|
|
isWin & download.name.includes('Windows') ||
|
|
|
|
isLinux & download.name.includes('Linux')
|
|
|
|
|
2020-02-26 23:28:34 +01:00
|
|
|
const li = document.createElement('li')
|
|
|
|
const a = document.createElement('a')
|
|
|
|
a.href = download.url
|
2020-03-05 03:11:15 +01:00
|
|
|
|
|
|
|
if (isTargetOs) {
|
|
|
|
a.className = 'button'
|
|
|
|
a.innerHTML = `Download <span>${download.name}</span>`
|
|
|
|
} else {
|
|
|
|
a.appendChild(document.createTextNode(download.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
li.appendChild(a)
|
|
|
|
|
|
|
|
return li
|
2020-02-26 23:28:34 +01:00
|
|
|
})
|
|
|
|
|
2020-03-05 03:11:15 +01:00
|
|
|
const downloadMain = downloadAll.filter(
|
|
|
|
link => link.querySelector('a').className === 'button'
|
|
|
|
)
|
|
|
|
const downloadSecondary = downloadAll.filter(
|
|
|
|
link => link.querySelector('a').className !== 'button'
|
|
|
|
)
|
2020-02-26 23:28:34 +01:00
|
|
|
|
2020-03-05 03:11:15 +01:00
|
|
|
downloadsElement.append(downloadMain[0])
|
|
|
|
downloadSecondary.forEach(download => downloadsElement.append(download))
|
|
|
|
}
|