# A score button you can keep on one phone / 手机上的计分按钮

Create an empty directory named `app`. Save the following two files inside it with UTF-8 encoding. The score remains in the current browser after a reload when storage is available. If storage is blocked, the button still works until the page closes and says that it could not save. Friends who receive the program file start with their own score. This example uses no external dependencies or Host APIs.

新建 `app` 目录，分别保存下面两个 UTF-8 文件。按按钮记一分；浏览器允许存储时，刷新后分数会保留。若存储被禁用，按钮在本页关闭前仍可使用，并会明确提示无法保存。把程序转交给朋友，各自的浏览器独立存分。这个示例只用标准 HTML 和 JavaScript。

## manifest.json

```json
{"id":"example.score-button","name":"Score button · 计分按钮","version":"1.0.0","entry":"index.html"}
```

## index.html

```html
<!doctype html>
<html lang="zh-CN">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>计分按钮 · Score button</title>
<style>
  * { box-sizing: border-box; }
  body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #fafaf8; color: #faf7ff; font: 1rem/1.5 system-ui, sans-serif; }
  main { width: min(22rem, calc(100% - 2rem)); padding: 1.7rem; background: #2d293e; border-radius: 1rem; box-shadow: 0 15px 32px -20px #33264a90; }
  h1 { margin: 0; padding-bottom: .6rem; border-bottom: 1px solid #61576e; font-size: 1.4rem; font-weight: 550; overflow-wrap: anywhere; }
  output { display: block; padding: 1rem 0; font-size: 4.5rem; line-height: 1.2; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
  .controls { display: grid; grid-template-columns: 1.6fr 1fr; gap: .55rem; }
  button { min-height: 44px; padding: .5rem; border: 1px solid #665c72; border-radius: 6px; background: #393247; color: #ebe5f4; font: inherit; cursor: pointer; }
  #point { background: #ded4ff; color: #332947; border-color: #ded4ff; }
  button:focus-visible { outline: 2px solid #c7b8ff; outline-offset: 3px; }
  #status { margin: .85rem 0 0; color: #c6bdd9; font-size: .78rem; }
</style>
<main>
  <h1>计分按钮 · Score button</h1>
  <output id="score" aria-label="Score" aria-live="polite">0</output>
  <div class="controls">
    <button type="button" id="point">加一分</button>
    <button type="button" id="reset">归零</button>
  </div>
  <p id="status"></p>
</main>
<script>
  const score = document.querySelector("#score")
  const status = document.querySelector("#status")
  const isEnglish = document.documentElement.lang === "en"
  score.setAttribute("aria-label", isEnglish ? "Score" : "当前分数")
  if (isEnglish) {
    document.querySelector("#point").textContent = "Add a point"
    document.querySelector("#reset").textContent = "Reset"
  }
  let points = 0
  let canSave = true
  try {
    const saved = Number(localStorage.getItem("score"))
    points = Number.isFinite(saved) && saved >= 0 ? Math.floor(saved) : 0
  } catch (error) {
    canSave = false
  }
  const render = () => {
    score.textContent = points
    status.textContent = canSave
      ? (isEnglish ? "Score saved on this device" : "分数保存在本机")
      : (isEnglish ? "Score lasts until this page closes" : "当前分数仅在本页打开时保留")
  }
  const save = () => {
    if (canSave) {
      try {
        localStorage.setItem("score", String(points))
      } catch (error) {
        canSave = false
      }
    }
  }
  document.querySelector("#point").addEventListener("click", () => {
    points += 1
    save()
    render()
  })
  document.querySelector("#reset").addEventListener("click", () => {
    points = 0
    save()
    render()
  })
  render()
</script>
</html>
```

Follow the [creation guide](start.md) to pack these files. Substitute `example.score-button` wherever the guide uses `friends.score`. 用[中文指南](start.zh.md)打包时，请将示例命令中的 ID 换成 `example.score-button`。给自己的新作品选一个新 ID。
