refactor(website): keep page text when a script message is missing

A page that omits a message attribute (a translation that drops one, or a
page without the shared footer) must not show empty or "undefined" text:

- Copy buttons still copy without #copy-status or its messages, keep their
  own label and leave the status empty.
- The star label is skipped when the header has no label for the count.
- The percent demo, dinosaur game and speed check leave the current text in
  place for a missing message; an unknown {placeholder} stays literal.

scripts/test-website-scripts.mjs runs main.js, home.js and dino-demo.js
against a small document model with node:test. `website.py check` runs it,
so the website CI job and the publisher's preparation job now set up the
repository's Node.js version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-11 07:09:16 +00:00
co-authored by Claude Opus 5.5
parent 3d34185171
commit dabcd783d5
8 changed files with 558 additions and 43 deletions
+33 -29
View File
@@ -12,8 +12,14 @@ function initializeDinoDemo(demo) {
const output = demo.querySelector("[data-dino-speed]");
const status = demo.querySelector("[data-dino-status]");
// The page owns the localized messages; {name} marks an inserted value.
// A missing message leaves the current text in place.
const message = (name, values = {}) =>
demo.dataset[name].replace(/\{(\w+)\}/g, (_, key) => values[key]);
demo.dataset[name]?.replace(/\{(\w+)\}/g, (match, key) =>
String(values[key] ?? match),
);
const show = (element, text) => {
if (text) element.textContent = text;
};
const ground = 148;
const dinosaurX = 52;
const dinosaur = new Path2D(
@@ -69,29 +75,23 @@ function initializeDinoDemo(demo) {
context.fillStyle = "#5b6570";
context.font = "13px monospace";
context.textAlign = "right";
context.fillText(
message("msgDistance", {
distance: String(Math.floor(distance / 100)).padStart(4, "0"),
}),
580,
28,
);
if (!running) {
const score = message("msgDistance", {
distance: String(Math.floor(distance / 100)).padStart(4, "0"),
});
if (score) context.fillText(score, 580, 28);
const hint = message(crashed ? "msgCanvasCrashed" : "msgCanvasReady");
if (!running && hint) {
context.textAlign = "center";
context.font = "15px system-ui";
context.fillText(
message(crashed ? "msgCanvasCrashed" : "msgCanvasReady"),
330,
85,
);
context.fillText(hint, 330, 85);
}
};
const stop = (notice) => {
running = false;
cancelAnimationFrame(frame);
start.textContent = message(crashed ? "msgPlayAgain" : "msgPlay");
if (notice) status.textContent = notice;
show(start, message(crashed ? "msgPlayAgain" : "msgPlay"));
show(status, notice);
draw();
};
@@ -104,8 +104,8 @@ function initializeDinoDemo(demo) {
obstacles = [{ x: 550, height: 38 }];
if (auto.checked) speed = SPEED_RULE.start;
elapsed = 0;
start.textContent = message("msgPlay");
status.textContent = message("msgReady");
show(start, message("msgPlay"));
show(status, message("msgReady"));
updateSpeedDisplay();
draw();
};
@@ -162,8 +162,8 @@ function initializeDinoDemo(demo) {
running = true;
lastTime = performance.now();
elapsed = 0;
start.textContent = message("msgPause");
status.textContent = message("msgRunning");
show(start, message("msgPause"));
show(status, message("msgRunning"));
frame = requestAnimationFrame(animate);
};
@@ -193,18 +193,23 @@ function initializeDinoDemo(demo) {
auto.checked = false;
speed = Number(slider.value);
updateSpeedDisplay();
status.textContent = message(
running ? "msgFixedRunning" : "msgFixedStopped",
{ speed: speed.toFixed(1) },
show(
status,
message(running ? "msgFixedRunning" : "msgFixedStopped", {
speed: speed.toFixed(1),
}),
);
draw();
});
auto.addEventListener("change", () => {
if (auto.checked) speed = SPEED_RULE.start;
updateSpeedDisplay();
status.textContent = auto.checked
? message("msgRule")
: message("msgFixed", { speed: speed.toFixed(1) });
show(
status,
auto.checked
? message("msgRule")
: message("msgFixed", { speed: speed.toFixed(1) }),
);
});
document.addEventListener("visibilitychange", () => {
if (document.hidden && running) stop(message("msgPaused"));
@@ -235,7 +240,6 @@ speedCheck?.addEventListener("click", () => {
matches && speed === cell.parentElement.children[1].textContent.trim();
});
const status = document.querySelector("[data-speed-check-status]");
status.textContent = matches
? status.dataset.msgMatch
: status.dataset.msgMismatch;
const result = matches ? status.dataset.msgMatch : status.dataset.msgMismatch;
if (result) status.textContent = result;
});
+11 -3
View File
@@ -6,8 +6,14 @@ function initializePercentDemo() {
const status = demo.querySelector("[data-percent-status]");
const equals = demo.querySelector('[data-percent-key="equals"]');
// The page owns the localized messages; {name} marks an inserted value.
// A missing message leaves the current text in place.
const message = (name, values = {}) =>
demo.dataset[name].replace(/\{(\w+)\}/g, (_, key) => values[key]);
demo.dataset[name]?.replace(/\{(\w+)\}/g, (match, key) =>
String(values[key] ?? match),
);
const show = (element, text) => {
if (text) element.textContent = text;
};
const operations = demo.querySelectorAll("[data-percent-operation]");
let operation = "add";
let stage = "input";
@@ -25,12 +31,14 @@ function initializePercentDemo() {
stage === "input" ? current : stage === "percent" ? percent : result,
);
equals.disabled = stage !== "percent";
status.textContent =
show(
status,
stage === "input"
? message("msgInput")
: stage === "percent"
? message(adding ? "msgPercentAdd" : "msgPercentMultiply")
: message("msgResult", { operator, percent, result });
: message("msgResult", { operator, percent, result }),
);
};
operations.forEach((button) => {
+10 -8
View File
@@ -1,11 +1,15 @@
function initializeCopyButtons() {
// The shared footer owns the localized copy messages.
// The shared footer owns the localized copy messages. Without one, a button
// still copies and keeps its own label.
const status = document.getElementById("copy-status");
if (status === null) return;
const messages = status.dataset;
const messages = status?.dataset ?? {};
document.querySelectorAll("[data-copy]").forEach((button) => {
const label = button.textContent;
const report = (buttonText, statusText) => {
if (buttonText) button.textContent = buttonText;
if (status !== null && statusText) status.textContent = statusText;
};
button.addEventListener("click", async () => {
const target = document.getElementById(button.getAttribute("data-copy"));
if (target === null) return;
@@ -17,8 +21,7 @@ function initializeCopyButtons() {
: content.replace(/\s+/g, " ");
try {
await navigator.clipboard.writeText(text);
button.textContent = messages.copied;
status.textContent = messages.statusCopied;
report(messages.copied, messages.statusCopied);
} catch {
const range = document.createRange();
range.selectNodeContents(target);
@@ -27,8 +30,7 @@ function initializeCopyButtons() {
selection.removeAllRanges();
selection.addRange(range);
}
button.textContent = messages.selected;
status.textContent = messages.statusSelected;
report(messages.selected, messages.statusSelected);
}
window.setTimeout(() => {
@@ -138,7 +140,7 @@ async function initializeGitHubStars() {
const template =
link.dataset[`label${category[0].toUpperCase()}${category.slice(1)}`] ??
link.dataset.labelOther;
if (template === undefined) return;
if (!template) return;
const formatted = new Intl.NumberFormat(lang).format(stars);
const label = template.replace("{count}", formatted);
count.textContent = formatted;