-
Notifications
You must be signed in to change notification settings - Fork 2
/
sidebar.js
66 lines (54 loc) · 2.02 KB
/
sidebar.js
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
import './search.js'
import { applyDefaults } from './defaults.js'
// Use import.meta.url to create a URL relative to the module's location
const defaultJsonUrl = new URL('./config/defaults.json', import.meta.url).href
// GitHub Releases Page URL
const githubReleasesPage = 'https://github.com/hyphacoop/reader.distributed.press/releases'
async function fetchLocalVersion () {
try {
const response = await fetch(defaultJsonUrl)
if (!response.ok) {
throw new Error(`Error fetching defaults.json: ${response.statusText}`)
}
const defaults = await response.json()
return defaults.version || 'Unknown Version'
} catch (error) {
console.error('Error fetching local version:', error)
return 'Unknown Version'
}
}
const response = await fetch(new URL('./sidebar.html', import.meta.url).href)
const text = await response.text()
const template = document.createElement('template')
template.innerHTML = text
const style = document.createElement('style')
style.textContent = '@import url("./sidebar.css");'
document.head.appendChild(style)
class SidebarNav extends HTMLElement {
constructor () {
super()
this.init()
}
async connectedCallback () {
await applyDefaults()
// Fetch the local version from defaults.json and display it in the sidebar
const versionElement = this.querySelector('#release-version')
if (versionElement) {
const localVersion = await fetchLocalVersion()
// Create the anchor element
const versionLink = document.createElement('a')
versionLink.href = githubReleasesPage
versionLink.textContent = `${localVersion}`
versionLink.target = '_blank' // Open in a new tab
versionLink.rel = 'noopener noreferrer' // Security best practices
// Clear any existing content and append the link
versionElement.innerHTML = ''
versionElement.appendChild(versionLink)
}
}
init () {
const instance = template.content.cloneNode(true)
this.appendChild(instance)
}
}
customElements.define('sidebar-nav', SidebarNav)