Skip to content

Commit f11fc3d

Browse files
committed
fix: where the player cannot start in a chat's frame or the browser, the play page offers a new tab or another browser, and a save with no word says the way by hand after 10 s (#116)
1 parent 26b6624 commit f11fc3d

4 files changed

Lines changed: 128 additions & 12 deletions

File tree

‎src/assets/site.css‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ html[data-standalone] .play { padding: 1rem; }
11181118
.play[data-drop] .play-stage { border-color: var(--accent); }
11191119
.play[data-drop] .play-panel { background: var(--tint); }
11201120

1121-
.play-loader, .play-ask, .play-error, .play-saved { display: grid; justify-items: center; gap: 0.75rem; width: 100%; min-width: 0; max-width: 26rem; }
1121+
.play-loader, .play-ask, .play-error, .play-saved, .play-elsewhere { display: grid; justify-items: center; gap: 0.75rem; width: 100%; min-width: 0; max-width: 26rem; }
11221122
.play-panel .button { white-space: normal; }
11231123
.play-label, .play-text, .play-note, .play-heading { margin: 0; }
11241124
.play-label { font-size: 0.9375rem; font-weight: 600; line-height: 1.4; }
@@ -1132,6 +1132,11 @@ html[data-standalone] .play { padding: 1rem; }
11321132
.play-error .play-icon { background: color-mix(in srgb, var(--t-tag) 14%, transparent); color: var(--t-tag); }
11331133
.play-saved-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem; margin: 0; }
11341134
.play-saved-actions:not(:has(> :not([hidden]))) { display: none; }
1135+
/* The address to copy where no tab opens: the page's own, long, so it scrolls in its box. */
1136+
.play-copy { display: grid; justify-items: center; gap: 0.4rem; width: 100%; }
1137+
.play-copy-row { display: flex; gap: 0.375rem; width: 100%; }
1138+
.play-copy-row input { flex: 1; min-width: 0; padding: 0.3rem 0.6rem; border: 1px solid var(--border); border-radius: var(--radius); background: transparent; color: var(--muted); font: 0.75rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; }
1139+
.play-copy-row input:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
11351140

11361141
/* Under the stage: what the activity is, and what to do with it. Until its name is known a bar
11371142
of its size stands there. */

‎src/h5p/build/build-source.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,11 @@ export function linksFromFragment(hash: string): { links: Record<string, unknown
167167
return { links, save: params.get('save') === '1', rest: rest ? `#${rest}` : '' }
168168
}
169169

170-
/** A fragment from `fragmentFromSource` with addresses and the wish to save beside it, as `linksFromFragment` reads them. */
171-
export const fragmentWithLinks = (fragment: string, links: Record<string, string>): string => `${fragment}&links=${toBase64Url(JSON.stringify(links))}&save=1`
170+
/**
171+
* A fragment from `fragmentFromSource` with addresses beside it, and the wish to save unless `save`
172+
* is false, as `linksFromFragment` reads them.
173+
*/
174+
export const fragmentWithLinks = (fragment: string, links: Record<string, string>, save = true): string => `${fragment}&links=${toBase64Url(JSON.stringify(links))}${save ? '&save=1' : ''}`
172175

173176
/** Whether a fragment has a `source=` at all, whatever it carries: a link of this page's kind. */
174177
export const linksSource = (hash: string): boolean => new URLSearchParams(hash.replace(/^#/, '')).has('source')

‎src/h5p/play/index.njk‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da
4343
<p class="play-text" id="error-text"></p>
4444
<a class="button button-solid button-large" id="fix" target="_blank" rel="noopener" hidden>Open in the build page</a>
4545
</div>
46+
{#- Where the player cannot start in this frame or this browser (#116): the way on, a tab of
47+
the page's own or another browser, with the address to copy where no tab opens. -#}
48+
<div class="play-elsewhere" id="elsewhere" hidden>
49+
<svg class="play-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M14 4h6v6M20 4l-9 9M19 14v4a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4"/></svg>
50+
<h1 class="play-heading" id="elsewhere-title"></h1>
51+
<p class="play-text" id="elsewhere-text"></p>
52+
<a class="button button-solid button-large" id="elsewhere-open" target="_blank" rel="noopener" hidden>Open in a new tab</a>
53+
<div class="play-copy">
54+
<p class="play-note" id="elsewhere-note"></p>
55+
<span class="play-copy-row"><input id="elsewhere-address" readonly aria-label="The activity's address"><button type="button" class="button" id="elsewhere-copy">Copy</button></span>
56+
</div>
57+
</div>
4658
{#- In a tab a link asked to save the package (&save=1): it is saved, and the tab closes itself. -#}
4759
<div class="play-saved" id="saved" hidden>
4860
<svg class="play-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12.5 10 17.5 19 7"/></svg>

‎src/h5p/play/play-page.ts‎

Lines changed: 105 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ import { baseName, compactSource, fragmentFromSource, fragmentWithLinks, kindOf,
5757
* the activity can then be played there. Such a tab builds from files only, never from an
5858
* address: what it fetched, or what the person chose in its place.
5959
*
60-
* What is seen is one stage, in one of six states (`data-state` on `main`): `building`, a loader
60+
* The player may not start where the page is, for the frame's sake or the browser's, not the
61+
* activity's (#116): a browser that blocks third-party cookies lets no framed page of another site
62+
* register a worker (Chrome's setting, which Incognito turns on, and Firefox's, 2026-11-25); WebKit
63+
* framed by another site could not read the archive the page had just built; a frame sandboxed
64+
* without an origin, or under a page on plain http, has no worker at all; nor has an app's built-in
65+
* browser. A tab of the page's own plays in each of the frame's cases: it is first-party, with its
66+
* own storage and worker, on https. So where the page knows from the browser that the player cannot
67+
* start, before anything is built, or the player says it could not register, read or store what the
68+
* page gave it, the page says so and offers the tab, or, in a tab already, another browser, with the
69+
* address to copy where no tab opens. Not a player without the worker: some activities would play
70+
* in part, which confuses more than it helps (the owner, 2026-11-25).
71+
*
72+
* What is seen is one stage, in one of seven states (`data-state` on `main`): `building`, a loader
6173
* over the place the player takes; `fetching`, the loader with how much of a file has come so far,
6274
* from the parent or from its address; `asking`, for the person's files; `error`, with the one
63-
* thing to do about it; `ready`, the player; `saved`, in a tab that only saves, the package saved
64-
* and the tab about to close. The parent is told each as a `status`, in words a screen reader is
65-
* told too.
75+
* thing to do about it; `elsewhere`, the way to a tab or another browser where the player cannot
76+
* start here; `ready`, the player; `saved`, in a tab that only saves, the package saved and the tab
77+
* about to close. The parent is told each as a `status`, in words a screen reader is told too.
6678
*/
6779

6880
const $ = <T extends HTMLElement>(id: string): T => document.getElementById(id) as T
@@ -84,6 +96,13 @@ const savedBox = $('saved')
8496
const savedText = $('saved-text')
8597
const savedStay = $<HTMLButtonElement>('saved-stay')
8698
const savedPlay = $<HTMLButtonElement>('saved-play')
99+
const elsewhereBox = $('elsewhere')
100+
const elsewhereTitle = $('elsewhere-title')
101+
const elsewhereText = $('elsewhere-text')
102+
const elsewhereOpen = $<HTMLAnchorElement>('elsewhere-open')
103+
const elsewhereNote = $('elsewhere-note')
104+
const elsewhereAddress = $<HTMLInputElement>('elsewhere-address')
105+
const elsewhereCopy = $<HTMLButtonElement>('elsewhere-copy')
87106
const errorTitle = $('error-title')
88107
const errorText = $('error-text')
89108
const fix = $<HTMLAnchorElement>('fix')
@@ -102,7 +121,7 @@ const edit = $<HTMLAnchorElement>('edit')
102121
const player = $('player') as HTMLElementTagNameMap['h5p-player']
103122
player.runtime = runtime
104123

105-
type State = 'building' | 'fetching' | 'asking' | 'ready' | 'error' | 'saved'
124+
type State = 'building' | 'fetching' | 'asking' | 'ready' | 'error' | 'elsewhere' | 'saved'
106125
type Opened = Awaited<ReturnType<typeof openPack>>
107126
/** The pack, opened at the first build and kept for the next, which a file chosen later starts. */
108127
let opened: Promise<Opened> | null = null
@@ -172,6 +191,7 @@ function show(state: State, text: string): void {
172191
loader.hidden = state !== 'building' && state !== 'fetching'
173192
filesBox.hidden = state !== 'asking'
174193
errorBox.hidden = state !== 'error'
194+
elsewhereBox.hidden = state !== 'elsewhere'
175195
savedBox.hidden = state !== 'saved'
176196
// The way to the build page is the problem's own button where it has one.
177197
edit.hidden = state === 'error' && !fix.hidden
@@ -200,6 +220,54 @@ function fail(heading: string, text: string, fixable = false): void {
200220
show('error', text ? `${heading}. ${text}` : `${heading}.`)
201221
}
202222

223+
/** Why the player cannot start where the page is: no secure context, or a browser that keeps it from starting. */
224+
type Why = 'insecure' | 'kept'
225+
226+
/**
227+
* Why the player cannot start here, as the browser shows it before anything is built: a page
228+
* outside a secure context, or with no origin, has no Service Worker, as one whose browser gives
229+
* none. `null` where it may start; the player says the rest.
230+
*/
231+
function cannotStart(): Why | null {
232+
if (!window.isSecureContext) return 'insecure'
233+
if (self.origin === 'null') return 'kept'
234+
try {
235+
return navigator.serviceWorker ? null : 'kept'
236+
} catch {
237+
return 'kept'
238+
}
239+
}
240+
241+
/**
242+
* The way on where the player cannot start here (#116). Framed: a tab of this page's own, which
243+
* the parent opens where it opens links (the click handler below hands it the address) and the
244+
* link opens otherwise, where the frame lets it. In a tab: another browser. The address is there
245+
* to copy in both, since whether a tab opened is not this page's to see. It carries the addresses
246+
* the parent gave for the person's files, so a tab plays them from there, and not the wish to
247+
* save. Download stays where a package was built: it is the person's even where it does not play.
248+
*/
249+
function elsewhere(why: Why): void {
250+
const given = Object.fromEntries(links)
251+
const address = `${location.origin}${location.pathname}${Object.keys(given).length ? fragmentWithLinks(ownFragment, given, false) : ownFragment}`
252+
elsewhereAddress.value = address
253+
elsewhereOpen.href = address
254+
elsewhereOpen.hidden = !framed
255+
elsewhereCopy.textContent = 'Copy'
256+
if (framed) {
257+
elsewhereTitle.textContent = 'Open it in a new tab'
258+
elsewhereText.textContent = why === 'insecure'
259+
? 'This chat’s page is not on a secure connection, and the player starts only on one. In a tab of its own it plays, still built in your browser.'
260+
: 'Your browser does not let the player start inside this chat’s reply, as where it blocks third-party cookies. In a tab of its own it plays, still built in your browser.'
261+
elsewhereNote.textContent = 'If no tab opens, copy this address into your browser.'
262+
} else {
263+
elsewhereTitle.textContent = 'Open it in another browser'
264+
elsewhereText.textContent = 'The player needs a Service Worker, which this browser does not give, as an app’s built-in browser often does not. Chrome, Edge, Firefox and Safari give one.'
265+
elsewhereNote.textContent = 'Copy this address into another browser.'
266+
}
267+
playerBox.hidden = true
268+
show('elsewhere', `${elsewhereTitle.textContent}. ${elsewhereText.textContent}`)
269+
}
270+
203271
/** What the activity is, under the stage: its title, and its type with the package's size once built. */
204272
function about(name: unknown, type: unknown, size?: number | string): void {
205273
if (typeof name === 'string' && name.trim()) title.textContent = name
@@ -401,16 +469,23 @@ function tooBig(size: number): void {
401469
saying(`This .h5p is ${sized(size)}, more than the chat saves with one press. ${BY_HAND}`)
402470
}
403471

404-
/** The package handed to a parent that saves files itself, which says how that went; no word in a minute is taken as no. */
472+
/**
473+
* The package handed to a parent that saves files itself, which says how that went. No word in a
474+
* few seconds is said with the way by hand (#116): a parent that never answers left the person
475+
* waiting a minute, and one that asks the person first still has its yes said when it comes.
476+
*/
405477
function handOver(file: File): void {
406478
handed = true
407479
saying('The chat is saving the .h5p…', { busy: true })
408480
toParent({ type: 'save', id: ++handedId, file })
409481
clearTimeout(handedTimer)
410-
handedTimer = setTimeout(() => saveDone(false), 60_000)
482+
handedTimer = setTimeout(() => {
483+
handed = false
484+
saying(`Nothing saved yet? ${BY_HAND_TOO}`)
485+
}, 10_000)
411486
}
412487

413-
/** How the parent's save of the last package handed went. A yes that comes after the minute is still said. */
488+
/** How the parent's save of the last package handed went. A yes that comes after the wait is still said. */
414489
function saveDone(ok: boolean): void {
415490
clearTimeout(handedTimer)
416491
handed = false
@@ -706,6 +781,9 @@ async function start(): Promise<void> {
706781
wanted = namedFiles(source)
707782
const written = source as { title?: unknown, type?: unknown }
708783
about(written.title, written.type)
784+
// Where the player cannot start here, nothing is built for it: the way on at once.
785+
const why = cannotStart()
786+
if (why) return elsewhere(why)
709787
// A tab that only saves fetches what the link gave addresses for first, and builds once.
710788
const { linked } = savingOnly ? linksFor(wanted) : { linked: {} }
711789
if (Object.keys(linked).length) return fetchThenBuild(linked)
@@ -723,9 +801,27 @@ player.addEventListener('ready', () => {
723801
})
724802
player.addEventListener('error', (event) => {
725803
if (player.state === 'ready') return
726-
fail('It was built, but does not play', (event as unknown as CustomEvent<{ message: string }>).detail.message, true)
804+
const { code, message } = (event as unknown as CustomEvent<{ code?: string, message: string }>).detail
805+
// Not the activity's: a worker that could not register is the browser's. Framed, so is an
806+
// archive the player could not read or store, which the page built a moment ago and which plays
807+
// in a tab (WebKit framed by another site, 2026-11-25).
808+
if (code === 'no-worker' || (framed && (code === 'bad-archive' || code === 'quota'))) return elsewhere('kept')
809+
fail('It was built, but does not play', message, true)
727810
})
728811

812+
// The address of the way on, copied, or left selected for the person's own keys where no clipboard is let.
813+
elsewhereAddress.addEventListener('focus', () => elsewhereAddress.select())
814+
elsewhereCopy.addEventListener('click', async () => {
815+
let copied = false
816+
try {
817+
await navigator.clipboard.writeText(elsewhereAddress.value)
818+
copied = true
819+
} catch {
820+
elsewhereAddress.select()
821+
try { copied = document.execCommand('copy') } catch { /* selected, at least */ }
822+
}
823+
if (copied) elsewhereCopy.textContent = 'Copied'
824+
})
729825
savedStay.addEventListener('click', stay)
730826
// The package that was saved, played in the tab that was kept open.
731827
savedPlay.addEventListener('click', () => {

0 commit comments

Comments
 (0)