Add API card type

This commit is contained in:
Arsen Musayelyan 2021-03-26 17:42:02 -07:00
parent c6af685621
commit 4a7e6b16af
3 changed files with 53 additions and 2 deletions

View File

@ -0,0 +1,35 @@
{{- $format := splitList "\n" (trim .Data.format) -}}
{{- $title := replace " " "" .Title -}}
<div class="card-header">
<a class="card-header-title" href="{{.URL}}">
{{if ne .Icon ""}}
{{template "icon" .Icon}}&nbsp;
{{end}}
{{.Title}}
</a>
</div>
<div class="card-content">
<p id="{{$title}}LoadingText">Loading...</p>
{{range $_, $accessStr := $format}}
{{- $id := printf `%s_%s` $title (b64enc $accessStr) -}}
<p id="{{$id}}"></p>
{{end}}
</div>
{{if .Data.footer}}
<div class="card-footer" style="margin-top: auto">
<p class="card-footer-item">{{.Data.footer}}</p>
</div>
{{end}}
<script>
var request = new XMLHttpRequest()
request.open('GET', "{{proxy .URL}}", true)
request.onload = function () {
const data = JSON.parse(this.response)
document.getElementById("{{$title}}LoadingText").classList.add("is-hidden")
{{range $_, $accessStr := $format}}
{{- $id := printf `%s_%s` $title (b64enc $accessStr) -}}
document.getElementById("{{$id}}").innerHTML = `{{unescJS (trim $accessStr)}}`
{{end}}
}
request.send()
</script>

View File

@ -1,7 +1,7 @@
title = "SimpleDash"
theme = "dark"
loginRequired = false
allowProxy = ["https://www.metaweather.com/"]
allowProxy = ["https://www.metaweather.com/", "https://ifconfig.co/json"]
[session]
name = "simpledash-session"
@ -12,6 +12,16 @@ allowProxy = ["https://www.metaweather.com/"]
title = "Weather"
data = {"woeid" = "2442047"}
[[users._public_.card]]
type = "api"
title = "Server IP (API card example)"
url = "https://ifconfig.co/json"
data = {"format" = """
<p class="subtitle">${data.ip}</p>
Country: ${data.country} (${data.country_iso})
Time zone: ${data.time_zone}
"""}
[users.admin]
passwordHash = "$2a$10$w00dzQ1PP6nwXLhuzV2pFOUU6m8bcZXtDX3UVxpOYq3fTSwVMqPge"
showPublic = true
@ -42,4 +52,4 @@ allowProxy = ["https://www.metaweather.com/"]
type = "collection"
title = "Science"
icon = "ic:outline-science"
data = {"Google Scholar" = {"url" = "https://robinhood.com/", "target" = "sameTab"}}
data = {"Google Scholar" = {"url" = "https://scholar.google.com/", "target" = "sameTab"}}

View File

@ -28,11 +28,17 @@ func wrapProxy(url string) string {
return fmt.Sprint("/proxy/", b64url)
}
// Wrap string in template.JS to unescape JS code
func unescapeJS(s string) template.JS {
return template.JS(s)
}
// Function to get template function map
func getFuncMap() template.FuncMap {
// Return function map with template functions
return template.FuncMap{
"dyn_template": dynamicTemplate,
"proxy": wrapProxy,
"unescJS": unescapeJS,
}
}