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 getDownloads(release) {
|
|
|
|
const downloads = release.assets
|
|
|
|
.filter(
|
|
|
|
asset =>
|
|
|
|
asset.name.includes('mac.zip') |
|
|
|
|
(asset.name.includes('.exe') && !asset.name.includes('.exe.blockmap')) |
|
|
|
|
asset.name.includes('.deb')
|
|
|
|
)
|
|
|
|
.map(asset => {
|
|
|
|
const isMac = asset.name.includes('mac.zip')
|
|
|
|
const isWin = asset.name.includes('.exe')
|
|
|
|
|
|
|
|
return {
|
2020-03-05 03:11:15 +01:00
|
|
|
name: isMac ? 'macOS' : isWin ? 'Windows' : 'Linux, deb',
|
2020-02-26 23:28:34 +01:00
|
|
|
url: asset.browser_download_url
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return downloads
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
const downloads = getDownloads(release)
|
|
|
|
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')
|
|
|
|
|
|
|
|
const downloadAll = downloads.map(download => {
|
|
|
|
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))
|
|
|
|
}
|