1
0
mirror of https://github.com/kremalicious/blowfish.git synced 2024-11-22 17:50:11 +01:00
blowfish/site/scripts.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

async function init() {
const response = await fetch('/api/github')
const release = await response.json()
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 {
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
releaseDateElement.innerHTML = `on ${dateFormatted}`
2020-02-26 23:28:34 +01:00
const downloads = getDownloads(release)
downloadsElement.innerHTML = ''
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
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
})
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
downloadsElement.append(downloadMain[0])
downloadSecondary.forEach(download => downloadsElement.append(download))
}