Spacebar now also resumes automatic refreshing

This commit is contained in:
Seednode 2023-10-09 08:17:32 -05:00
parent f36c0cc999
commit 9633239b69
5 changed files with 51 additions and 21 deletions

View File

@ -53,7 +53,9 @@ The remaining four endpoints—`/available_extensions`, `/enabled_extensions`, `
## Refresh ## Refresh
If the `--refresh` flag is passed and a positive-value `refresh=<integer><unit>` query parameter is provided, the page will reload after that interval. If the `--refresh` flag is passed and a positive-value `refresh=<integer><unit>` query parameter is provided, the page will reload after that interval.
This can be used to generate a sort of slideshow of files. Pressing Space will pause automatic refreshing until the page is manually refreshed or a new page is loaded. This can be used to generate a sort of slideshow of files in any browser with Javascript support.
Pressing Spacebar will pause automatic refreshing until Spacebar is pressed again, the page is manually refreshed, or a new page is loaded.
Minimum accepted value is 500ms, as anything lower seems to cause inconsistent behavior. This might be changed in a future release. Minimum accepted value is 500ms, as anything lower seems to cause inconsistent behavior. This might be changed in a future release.

46
cmd/refresh.go Normal file
View File

@ -0,0 +1,46 @@
/*
Copyright © 2023 Seednode <seednode@seedno.de>
*/
package cmd
import (
"fmt"
"net/http"
"strings"
"time"
)
func refreshInterval(r *http.Request) (int64, string) {
interval := r.URL.Query().Get("refresh")
duration, err := time.ParseDuration(interval)
switch {
case err != nil || duration == 0 || !Refresh:
return 0, "0ms"
case duration < 500*time.Millisecond:
return 500, "500ms"
default:
return duration.Milliseconds(), interval
}
}
func refreshFunction(rootUrl string, refreshTimer int64) string {
var htmlBody strings.Builder
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){ clear = setInterval(function() {window.location.href = '%s';}, %d)};",
rootUrl,
refreshTimer))
htmlBody.WriteString("document.body.onkeyup = function(e) { ")
htmlBody.WriteString(`if (e.key == " " || e.code == "Space" || e.keyCode == 32) { `)
htmlBody.WriteString(`if (typeof clear !== 'undefined') {`)
htmlBody.WriteString(`clearInterval(clear); delete clear;`)
htmlBody.WriteString(`} else {`)
htmlBody.WriteString(fmt.Sprintf("clear = setInterval(function(){window.location.href = '%s';}, %d);}}}",
rootUrl,
refreshTimer))
htmlBody.WriteString(`</script>`)
return htmlBody.String()
}

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "2.8.0" ReleaseVersion string = "2.8.1"
) )
var ( var (

View File

@ -10,24 +10,8 @@ import (
"net/url" "net/url"
"runtime" "runtime"
"strings" "strings"
"time"
) )
func refreshInterval(r *http.Request) (int64, string) {
interval := r.URL.Query().Get("refresh")
duration, err := time.ParseDuration(interval)
switch {
case err != nil || duration == 0 || !Refresh:
return 0, "0ms"
case duration < 500*time.Millisecond:
return 500, "500ms"
default:
return duration.Milliseconds(), interval
}
}
func sortOrder(r *http.Request) string { func sortOrder(r *http.Request) string {
sortOrder := r.URL.Query().Get("sort") sortOrder := r.URL.Query().Get("sort")
if sortOrder == "asc" || sortOrder == "desc" { if sortOrder == "asc" || sortOrder == "desc" {

View File

@ -325,9 +325,7 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
htmlBody.WriteString(title) htmlBody.WriteString(title)
htmlBody.WriteString(`</head><body>`) htmlBody.WriteString(`</head><body>`)
if refreshInterval != "0ms" { if refreshInterval != "0ms" {
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){clear = setInterval(function(){window.location.href = '%s';}, %d); document.body.onkeyup = function(e) { if (e.key == \"\" || e.code == \"Space\" || e.keyCode == 32){clearInterval(clear)}}};</script>", htmlBody.WriteString(refreshFunction(rootUrl, refreshTimer))
rootUrl,
refreshTimer))
} }
body, err := format.Body(rootUrl, fileUri, path, fileName, Prefix, mimeType) body, err := format.Body(rootUrl, fileUri, path, fileName, Prefix, mimeType)