Fix tabs for jsonp

This commit is contained in:
Cadence Ember
2025-11-08 13:43:12 +13:00
parent 1dd90f5a7d
commit 5c3ff9b37c
7 changed files with 62 additions and 24 deletions

View File

@@ -164,7 +164,8 @@
#:siteinfo [siteinfo-in #f] #:siteinfo [siteinfo-in #f]
#:user-cookies [user-cookies-in #f] #:user-cookies [user-cookies-in #f]
#:online-styles [online-styles #t] #:online-styles [online-styles #t]
#:path [path-in #f]) #:path [path-in #f]
#:jsonp [jsonp-in #f])
(define siteinfo (or siteinfo-in siteinfo-default)) (define siteinfo (or siteinfo-in siteinfo-default))
(define head-data (or head-data-in ((head-data-getter wikiname)))) (define head-data (or head-data-in ((head-data-getter wikiname))))
(define user-cookies (or user-cookies-in (user-cookies-getter req))) (define user-cookies (or user-cookies-in (user-cookies-getter req)))
@@ -200,12 +201,14 @@
(script "const BWData = " (script "const BWData = "
,(jsexpr->string (hasheq 'wikiname wikiname ,(jsexpr->string (hasheq 'wikiname wikiname
'strict_proxy (config-true? 'strict_proxy) 'strict_proxy (config-true? 'strict_proxy)
'path path))) 'path path
'jsonp jsonp-in)))
(script (@ (type "importmap")) ,importmap)
,(if (config-true? 'feature_search_suggestions) ,(if (config-true? 'feature_search_suggestions)
`(script (@ (type "module") (src ,(get-static-url "search-suggestions.js")))) `(script (@ (type "module") (src ,(get-static-url "search-suggestions.js"))))
"") "")
(script (@ (type "module") (src ,(get-static-url "countdown.js")))) (script (@ (type "module") (src ,(get-static-url "countdown.js"))))
(script (@ (defer) (src ,(get-static-url "tabs.js")))) (script (@ (type "module") (src ,(get-static-url "tabs.js"))))
(link (@ (rel "icon") (href ,(u (λ (v) (config-true? 'strict_proxy)) (link (@ (rel "icon") (href ,(u (λ (v) (config-true? 'strict_proxy))
(λ (v) (u-proxy-url v)) (λ (v) (u-proxy-url v))
(head-data^-icon-url head-data)))))) (head-data^-icon-url head-data))))))

View File

@@ -61,7 +61,8 @@ END
#:wikiname wikiname #:wikiname wikiname
#:title (url-segments->guess-title segments) #:title (url-segments->guess-title segments)
#:siteinfo siteinfo-default #:siteinfo siteinfo-default
#:path path)) #:path path
#:jsonp #t))
(when (config-true? 'debug) (when (config-true? 'debug)
(xexp->html body)) (xexp->html body))
(response/output (response/output

View File

@@ -1,16 +1,20 @@
#lang typed/racket/base #lang typed/racket/base
(require racket/path (require racket/path
racket/runtime-path racket/runtime-path
racket/string) racket/string
typed/json)
(provide (provide
get-static-url get-static-url
importmap
link-header) link-header)
(define-runtime-path path-static "../static") (define-runtime-path path-static "../static")
(define static-data (define static-data
(for/hash : (Immutable-HashTable Path Nonnegative-Integer)([f (directory-list path-static)]) (for/hash : (Immutable-HashTable Path Nonnegative-Integer)
([f (directory-list path-static)]
#:when (not (regexp-match? #rx"^.#|^#|~$" (path->string f))))
(define built (simple-form-path (build-path path-static f))) (define built (simple-form-path (build-path path-static f)))
(values built (file-or-directory-modify-seconds built)))) (values built (file-or-directory-modify-seconds built))))
@@ -21,12 +25,21 @@
(build-path path-static path-or-filename)))) (build-path path-static path-or-filename))))
(format "/static/~a?t=~a" (file-name-from-path the-path) (hash-ref static-data the-path))) (format "/static/~a?t=~a" (file-name-from-path the-path) (hash-ref static-data the-path)))
(: importmap String)
(define importmap
(jsexpr->string
`#hasheq((imports . ,(for/hasheq : (Immutable-HashTable Symbol String)
([(k v) (in-hash static-data)]
#:when (equal? (path-get-extension k) #".js"))
(values (string->symbol (path->string (path-replace-extension (assert (file-name-from-path k) path?) #"")))
(get-static-url k)))))))
; https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload ; https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload
(: link-header String) (: link-header String)
(define link-header (define link-header
(let* ([with-t '(("main.css" "as=style"))] (let* ([with-t '(("main.css" "as=style")
[without-t '(("preact.js" "as=script") ("preact.js" "as=script"))]
("source-sans-pro-v21-vietnamese_latin-ext_latin_greek-ext_greek_cyrillic-ext_cyrillic-regular.woff2" "as=font" "crossorigin" "type=font/woff2"))] [without-t '(("source-sans-pro-v21-vietnamese_latin-ext_latin_greek-ext_greek_cyrillic-ext_cyrillic-regular.woff2" "as=font" "crossorigin" "type=font/woff2"))]
[with-t-full (map (λ ([path : (Listof String)]) (cons (get-static-url (car path)) (cdr path))) with-t)] [with-t-full (map (λ ([path : (Listof String)]) (cons (get-static-url (car path)) (cdr path))) with-t)]
[without-t-full (map (λ ([path : (Listof String)]) (cons (format "/static/~a" (car path)) (cdr path))) without-t)] [without-t-full (map (λ ([path : (Listof String)]) (cons (format "/static/~a" (car path)) (cdr path))) without-t)]
[all (append with-t-full without-t-full)] [all (append with-t-full without-t-full)]

View File

@@ -2,7 +2,7 @@
// sample: bandori/wiki/BanG_Dream!_Wikia // sample: bandori/wiki/BanG_Dream!_Wikia
// sample: ensemble-stars/wiki/The_English_Ensemble_Stars_Wiki // sample: ensemble-stars/wiki/The_English_Ensemble_Stars_Wiki
import {h, htm, render, signal, computed, effect} from "./preact.js" import {h, htm, render, signal, computed, effect} from "preact"
const html = htm.bind(h) const html = htm.bind(h)
const now = signal(Date.now()) const now = signal(Date.now())

View File

@@ -1,6 +1,11 @@
import {h, htm, render, signal, computed, effect} from "./preact.js" import {h, htm, render, signal, computed, effect} from "preact"
const html = htm.bind(h) const html = htm.bind(h)
// *** Status
const loaded = signal(false)
export {loaded}
// *** Loading indicator // *** Loading indicator
render(html`Loading, please wait...`, document.getElementById("loading")) render(html`Loading, please wait...`, document.getElementById("loading"))
@@ -69,6 +74,7 @@ async function cont() {
if (redirectTo) { if (redirectTo) {
redirectTo.click() redirectTo.click()
} }
loaded.value = true
}) })
xhr.open("POST", "/api/render/wiki") xhr.open("POST", "/api/render/wiki")

View File

@@ -1,4 +1,4 @@
import {h, htm, render, signal, computed, effect} from "./preact.js" import {h, htm, render, signal, computed, effect} from "preact"
const html = htm.bind(h) const html = htm.bind(h)
const classNames = classArr => classArr.filter(el => el).join(" ") const classNames = classArr => classArr.filter(el => el).join(" ")

View File

@@ -1,19 +1,22 @@
"use strict"; import {effect} from "preact"
import {loaded} from "jsonp"
const tabFromHash = location.hash.length > 1 ? location.hash.substring(1) : null const tabFromHash = location.hash.length > 1 ? location.hash.substring(1) : null
for (const tabber of document.body.querySelectorAll(".wds-tabber")) { function setUpAllTabs() {
for (const [tab, content] of getTabberTabs(tabber)) { for (const tabber of document.body.querySelectorAll(".wds-tabber")) {
// set up click listener on every tab for (const [tab, content] of getTabberTabs(tabber)) {
tab.addEventListener("click", e => { // set up click listener on every tab
setCurrentTab(tabber, tab, content) tab.addEventListener("click", e => {
e.preventDefault() setCurrentTab(tabber, tab, content)
}) e.preventDefault()
})
// re-open a specific tab on page load based on the URL hash // re-open a specific tab on page load based on the URL hash
if (tab.dataset.hash === tabFromHash) { if (tab.dataset.hash === tabFromHash) {
setCurrentTab(tabber, tab, content) setCurrentTab(tabber, tab, content)
tab.scrollIntoView() tab.scrollIntoView()
}
} }
} }
} }
@@ -37,4 +40,16 @@ function setCurrentTab(tabber, tab, content) {
} }
} }
if (!BWData.jsonp) {
setUpAllTabs()
} else if (loaded.value) {
setUpAllTabs()
} else {
effect(() => {
if (loaded.value) {
setUpAllTabs()
}
})
}
document.body.classList.remove("bw-tabs-nojs") document.body.classList.remove("bw-tabs-nojs")