From 62e503f75363c2a5387fe1d3ad023a3824fad941 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 20 Nov 2026 15:50:32 +0200 Subject: [PATCH 1/2] feat(play): the play page asks for the files a source names (#88) An activity that uses a file of the person's own, a video beside the source, was sent to the build page. The page now names the files and takes them: chosen from the computer, or given by the frame's parent. One file named and one of the same kind chosen is a match whatever it is called. They are read in the browser and go nowhere. --- README.md | 2 +- src/assets/site.css | 2 + src/h5p/play/index.njk | 8 ++- src/h5p/play/play-page.ts | 128 +++++++++++++++++++++++++++++++++----- 4 files changed, 123 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index bcd5411..99fa774 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The site of the [missing-elements](https://github.com/missing-elements) web comp | `/` | The project list, the tools (the build page first, with its how-to), and a footer of links | [src/index.njk](src/index.njk); the lists are in [src/_data/site.js](src/_data/site.js) | | `/pdfjs-viewer/` | The docs of ``, with a live preview | [src/pdfjs-viewer/](src/pdfjs-viewer/) | | `/h5p/build/` | The build page, for people: what it makes, a sample that plays, and the build of a chat's source into a `.h5p`, in the visitor's browser | [src/h5p/build/](src/h5p/build/) | -| `/h5p/play/` | The play page: the activity in a link's `#source=`, built in the visitor's browser as the build page builds it, and played with nothing beside it but its download and a link to the build page, for a chat app to show in its reply (h5p-toolkit#210); no page links to it | [src/h5p/play/](src/h5p/play/) | +| `/h5p/play/` | The play page: the activity in a link's `#source=`, built in the visitor's browser as the build page builds it, with any file of their own it names chosen there, and played with nothing beside it but its download and a link to the build page, for a chat app to show in its reply (h5p-toolkit#210); no page links to it | [src/h5p/play/](src/h5p/play/) | | `/h5p/create-with-ai/` | The how-to, for people: installing the skill, asking a chat, the prompts to start from, the types, and what to do when something goes wrong | [src/h5p/create-with-ai/](src/h5p/create-with-ai/) | | `/h5p/skill/` | The skill as zips to install: `h5p-author.zip`, installed once in a chat's place for skills, or attached to the message in each new chat where it has none, and `h5p-author-chatgpt.zip`, the same skill laid out as a plugin; and a page that lists them and the bundles' addresses, each in full, which no page links to | [src/h5p/skill/](src/h5p/skill/); the zips written by [vite.config.ts](vite.config.ts) | | `/h5p/bundles/` | The skill's build tool and a library bundle per content type, for a chat that runs Node but cannot reach npm; and `h5p-author-build.tar.gz`, the tool and every library in one archive, for a person to attach to a chat whose code can download nothing | written by [vite.config.ts](vite.config.ts) | diff --git a/src/assets/site.css b/src/assets/site.css index 2412139..7dd24a2 100644 --- a/src/assets/site.css +++ b/src/assets/site.css @@ -1091,7 +1091,9 @@ pre.prompt { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.9375re .play { padding: 0.5rem; } .play .player-box { margin: 0; } .play-status { margin: 0 0 0.5rem; color: var(--muted); font-size: 0.9rem; } +.play-status[data-state="asking"] { color: inherit; } .play-status[data-state="error"] { color: inherit; font-weight: 600; } +.play-files { margin: 0 0 0.5rem; } .play-status[data-state="ready"] { display: none; } .play-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.5rem 0 0; } .play-actions [hidden] { display: none; } diff --git a/src/h5p/play/index.njk b/src/h5p/play/index.njk index bc9149d..c81236a 100644 --- a/src/h5p/play/index.njk +++ b/src/h5p/play/index.njk @@ -12,7 +12,13 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da so that a chat app can show the activity in its reply: play-page.ts builds it and plays it. -#}

Building the activity in your browser…

-
+ {#- For a source that names files of the person's own, a video beside it: chosen here, where + they stay in the browser. The field is hidden, and the button opens it. -#} + +

diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index 1752650..9980c3f 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -2,7 +2,7 @@ import '@missing-elements/h5p-offline-player' import { buildPackage, openPack, SourceError } from '@missing-elements/h5p-author/browser' import { pack as installPack } from '@missing-elements/h5p-libraries/install' import { runtime } from '@missing-elements/h5p-runtime' -import { namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source' +import { baseName, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source' /** * The play page, `/h5p/play/`: the activity in a link's `#source=` built in the browser, as the @@ -10,19 +10,42 @@ import { namedFiles, packageName, parseSource, sized, sourceFromFragment, type P * way to the build page. A chat app frames it to show the activity in a reply (h5p-toolkit#210). * Framed, it tells the frame's parent its height, and gives it the built file, which a parent that * may not let the frame download can save its own way. Nothing leaves the browser otherwise. + * + * A source that names files of the person's own, a video beside it, is asked for them here: the + * person chooses them from their computer, or the frame's parent gives them, and the activity is + * built once they are all here. A chat app was found to hand a card no file the person attached + * (ChatGPT, 2026-11-20: the tool got a piece of text in the file's place), so the page asks itself. + * A file chosen here goes nowhere: the build reads it in the browser. */ const $ = (id: string): T => document.getElementById(id) as T const status = $('status') +const filesBox = $('files-box') +const choose = $('choose') +const filesInput = $('files') +const playerBox = $('player-box') const download = $('download') const edit = $('edit') const player = $('player') as HTMLElementTagNameMap['h5p-player'] player.runtime = runtime +type Opened = Awaited> +/** The pack, opened at the first build and kept for the next, which a file chosen later starts. */ +let opened: Promise | null = null +/** The source the link carries, once read, and the files it names. */ +let source: unknown +let wanted: string[] = [] +/** The files given so far, by name. */ +const chosen = new Map() +/** The address of the package last built, given back when the next one replaces it. */ +let packageUrl: string | null = null +/** Counts builds, so that one overtaken by a newer one shows nothing. */ +let builds = 0 + const framed = window.parent !== window const toParent = (message: Record): void => { if (framed) window.parent.postMessage({ source: 'h5p-play', ...message }, '*') } -function say(text: string, state: 'building' | 'ready' | 'error'): void { +function say(text: string, state: 'building' | 'asking' | 'ready' | 'error'): void { status.textContent = text status.dataset.state = state toParent({ type: 'status', state, text }) @@ -34,41 +57,116 @@ edit.href = `/h5p/build/${location.hash}` // The page's height for the parent, which sizes its frame to it. if (framed) new ResizeObserver(() => toParent({ type: 'height', height: Math.ceil(document.documentElement.getBoundingClientRect().height) })).observe(document.body) -async function play(): Promise { - let text: string | null - try { - text = await sourceFromFragment(location.hash) - } catch { - return say('The link is damaged: its activity cannot be read. Ask the chat for it again.', 'error') +/** A file's kind, by the ending of its name. */ +const kind = (name: string): string => name.slice(name.lastIndexOf('.') + 1).toLowerCase() + +/** + * The given files for the paths the source names, matched by name as the build page matches them. + * Where it names one file and one of the same kind was given, that is the file whatever it is + * called: a chat may write the name its own way, and the source cannot be changed here. + */ +function filesFor(paths: string[]): { files: Record, missing: string[] } { + const given = [...chosen.values()] + const matched = matchFiles(paths, given) + const [path] = paths + const [file] = given + if (matched.missing.length && paths.length === 1 && given.length === 1 && path !== undefined && file !== undefined && kind(path) === kind(file.name)) { + return { files: { [path]: file }, missing: [] } } - if (text === null) return say('The link carries no activity. This page plays the one an AI chat put in its link.', 'error') - const parsed = parseSource(text) - if ('problem' in parsed) return say('The activity in the link cannot be read. Open it in the build page to see why.', 'error') - if (namedFiles(parsed.source).length) return say('The activity names files of your own: open it in the build page to choose them.', 'error') + return matched +} + +/** Asks for the files still missing, by their names, and tells the parent, which may have them. */ +function ask(missing: string[]): void { + const names = missing.map(baseName) + const listed = new Intl.ListFormat('en-GB', { type: 'conjunction' }).format(names) + const one = missing.length === 1 + choose.textContent = one ? 'Choose the file' : 'Choose the files' + filesInput.accept = [...new Set(missing.map((path) => `.${kind(path)}`))].join(',') + filesBox.hidden = false + say(chosen.size + ? `Still needed: ${listed}.` + : `This activity uses ${one ? 'a file' : 'files'} of yours: ${listed}. Choose ${one ? 'it' : 'them'} from your computer; ${one ? 'it stays' : 'they stay'} in your browser.`, 'asking') + toParent({ type: 'needs-files', names }) +} + +/** Files given, by the person or by the parent: the activity is built once none is missing. */ +function take(files: File[]): void { + if (!files.length || !wanted.length) return + // Where one file is named, a second choice replaces the first. + if (wanted.length === 1) chosen.clear() + for (const file of files) chosen.set(file.name, file) + void play() +} + +async function play(): Promise { + const build = ++builds + const { files, missing } = filesFor(wanted) + if (missing.length) return ask(missing) + filesBox.hidden = true + say('Building the activity in your browser…', 'building') try { - const result = await buildPackage(parsed.source, { libraries: await openPack(installPack), files: {} }) + opened ??= openPack(installPack) + const result = await buildPackage(source, { libraries: await opened, files }) + if (build !== builds) return const name = packageName(String(result.manifest.title ?? '')) - download.href = URL.createObjectURL(result.blob) + if (packageUrl) URL.revokeObjectURL(packageUrl) + packageUrl = URL.createObjectURL(result.blob) + download.href = packageUrl download.download = name download.hidden = false const file = new File([result.blob], name, { type: 'application/zip' }) toParent({ type: 'built', file, title: result.manifest.title, size: sized(result.blob.size) }) say(`“${String(result.manifest.title ?? name)}”, ${sized(result.blob.size)}. Starting it…`, 'building') + playerBox.hidden = false player.file = file } catch (error) { + if (build !== builds) return if (error instanceof SourceError) { const problems = (error as SourceError & { problems: Problem[] }).problems return say(`The activity has ${problems.length} problem${problems.length === 1 ? '' : 's'}: open it in the build page to see and fix ${problems.length === 1 ? 'it' : 'them'}.`, 'error') } + // A pack that could not open is opened again at the next build. + opened = null say(`It could not be built: ${error instanceof Error ? error.message : error}`, 'error') } } +async function start(): Promise { + let text: string | null + try { + text = await sourceFromFragment(location.hash) + } catch { + return say('The link is damaged: its activity cannot be read. Ask the chat for it again.', 'error') + } + if (text === null) return say('The link carries no activity. This page plays the one an AI chat put in its link.', 'error') + const parsed = parseSource(text) + if ('problem' in parsed) return say('The activity in the link cannot be read. Open it in the build page to see why.', 'error') + source = parsed.source + wanted = namedFiles(source) + await play() +} + player.addEventListener('ready', () => say(status.textContent!.replace(/ Starting it…$/, ''), 'ready')) player.addEventListener('error', (event) => { if (player.state === 'ready') return say(`It was built, but does not play: ${(event as unknown as CustomEvent<{ message: string }>).detail.message}`, 'error') }) -void play() +// The file field is hidden, and the button opens it. +choose.addEventListener('click', () => filesInput.click()) +filesInput.addEventListener('change', () => { + const files = [...(filesInput.files ?? [])] + filesInput.value = '' + take(files) +}) +// Files from the frame's parent, which may hold what the person attached in the chat. +window.addEventListener('message', (event) => { + if (!framed || event.source !== window.parent) return + const message = event.data as { source?: unknown, type?: unknown, files?: unknown } | null + if (message?.source !== 'h5p-play-host' || message.type !== 'files' || !Array.isArray(message.files)) return + take(message.files.filter((file): file is File => file instanceof File)) +}) + +void start() window.addEventListener('hashchange', () => location.reload()) From 85e73e1fae377699f8b1b7169319ac06a91380ff Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 20 Nov 2026 15:54:59 +0200 Subject: [PATCH 2/2] fix(play): the parent is told the missing files once per set of names (#88) A parent that answered each needs-files with the same files, some of them, was asked again with every answer, without end. --- src/h5p/play/play-page.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index 9980c3f..50fe251 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -41,6 +41,8 @@ const chosen = new Map() let packageUrl: string | null = null /** Counts builds, so that one overtaken by a newer one shows nothing. */ let builds = 0 +/** The missing names the parent was last told, so that it is told again only when they change. */ +let told = '' const framed = window.parent !== window const toParent = (message: Record): void => { if (framed) window.parent.postMessage({ source: 'h5p-play', ...message }, '*') } @@ -76,7 +78,11 @@ function filesFor(paths: string[]): { files: Record, missing: stri return matched } -/** Asks for the files still missing, by their names, and tells the parent, which may have them. */ +/** + * Asks for the files still missing, by their names, and tells the parent, which may have them. The + * parent is told once for a set of names: one that answers each time with the same files, some of + * them or none that match, would otherwise be asked again with every answer, without end. + */ function ask(missing: string[]): void { const names = missing.map(baseName) const listed = new Intl.ListFormat('en-GB', { type: 'conjunction' }).format(names) @@ -87,6 +93,9 @@ function ask(missing: string[]): void { say(chosen.size ? `Still needed: ${listed}.` : `This activity uses ${one ? 'a file' : 'files'} of yours: ${listed}. Choose ${one ? 'it' : 'them'} from your computer; ${one ? 'it stays' : 'they stay'} in your browser.`, 'asking') + const missingNow = names.join('\n') + if (missingNow === told) return + told = missingNow toParent({ type: 'needs-files', names }) }