アニソンイントロクイズ練習サイト

繰り返しランダム再生コード

1.下記のコードをコピーして、サイト(https://app.tockey.net/panelquiz/intro/2000/index.html)を開く。
※アニソンイントロクイズ前に公開するので、いつもは使用できません。

2.F12キーを押下して、開発者ツールを開く。

3.最下段に「コンソール」があるので、最終行に張り付けてEnter。

(() => {
  const sleep = (ms) => new Promise(r => setTimeout(r, ms));

  function getAudio() {
    return document.querySelector('audio#player, audio');
  }

  function findCandidates() {
    const rows = Array.from(document.querySelectorAll('tr'));
    const list = [];
    for (const r of rows) {
      const txt = (r.innerText || "").trim();
      if (!txt) continue;

      const parts = txt.split(/\s+/);
      const last = parts[parts.length - 1];
      if (last !== "1" && last !== "2") continue;

      // クリック対象の推定(button/a/role=button → なければtd先頭)
      const click =
        r.querySelector("button, a, [role='button']") ||
        r.querySelector("td") ||
        r;

      list.push({ row: r, click });
    }
    return list;
  }

  async function waitAudioEnd(maxWaitMs = 360000) {
    const start = Date.now();
    while (true) {
      const a = getAudio();
      if (a && Number.isFinite(a.duration) && a.duration > 0) {
        // ended or near end
        if (a.ended || a.currentTime >= a.duration - 0.05) return true;
      }
      if (Date.now() - start > maxWaitMs) return false;
      await sleep(250);
    }
  }

  let candidates = findCandidates();
  if (!candidates.length) {
    console.warn("レベル1/2候補が見つかりませんでした");
    return;
  }

  let lastIndex = -1;
  let stopFlag = false;
  window.__STOP_RANDOM_XE__ = () => { stopFlag = true; };

  (async () => {
    console.log("開始。止めるには __STOP_RANDOM_XE__() を実行");
    while (!stopFlag) {
      // DOMが変わる場合だけ再取得(軽く)
      const fresh = findCandidates();
      if (fresh.length) candidates = fresh;

      let idx = Math.floor(Math.random() * candidates.length);
      if (candidates.length > 1 && idx === lastIndex) idx = (idx + 1) % candidates.length;
      lastIndex = idx;

      const { row, click } = candidates[idx];
      const title = (row.innerText || "").split("/")[0].replace("再生", "").trim();
      console.log("クリック:", title);

      // 再生クリック
      click.scrollIntoView({ block: "center" });
      click.click();

      // 曲が終わるまで待機
      const ok = await waitAudioEnd();
      if (!ok) console.warn("タイムアウト。次へ");
      await sleep(200); // 次曲までの短いクッション
    }
    console.log("停止しました");
  })();
})();

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です