Tailwind Docs Nav Switcher

使用左右箭头切换 Tailwind Docs 导航链接

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Tailwind Docs Nav Switcher
// @version      1.3.1
// @namespace    tianyw0
// @description  使用左右箭头切换 Tailwind Docs 导航链接
// @author       tianyw0
// @match        https://tailwindcss.com/docs/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  const nav = document.querySelector('nav');
  if (!nav) return;

  const links = Array.from(nav.querySelectorAll('a[href^="/docs/"]'));
  if (links.length === 0) return;

  function getCurrentIndex() {
    const current = location.href.split('#')[0];
    let idx = links.findIndex(a => a.href === current);
    if (idx === -1) idx = links.findIndex(a => current.startsWith(a.href));
    return idx >= 0 ? idx : 0;
  }

  let currentIndex = getCurrentIndex();

  nav.addEventListener('click', e => {
    const a = e.target.closest('a');
    if (!a) return;
    const idx = links.indexOf(a);
    if (idx >= 0) currentIndex = idx;
  });

  function tryExpand() {
    const button = Array.from(document.querySelectorAll('button'))
      .find(b => /show more/i.test(b.textContent.trim()));
    if (button) button.click();
  }

  function updateClassCount() {
    const table = document.querySelector('table');
    if (!table) return;
    const rows = table.querySelectorAll('tr');
    const count = Math.max(0, rows.length - 1);
    const h1 = document.querySelector('h1');
    if (h1 && !/class/.test(h1.textContent)) {
      h1.textContent += `(class:${count})`;
    }
  }

  window.addEventListener('keydown', e => {
    if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
      e.preventDefault();
      currentIndex =
        e.key === 'ArrowLeft'
          ? (currentIndex - 1 + links.length) % links.length
          : (currentIndex + 1) % links.length;

      const target = links[currentIndex];
      if (!target) return;
      target.click();

      const observer = new MutationObserver(() => {
        tryExpand();
        updateClassCount();
      });
      observer.observe(document.body, { childList: true, subtree: true });

      setTimeout(() => {
        tryExpand();
        updateClassCount();
        observer.disconnect();
      }, 800);
    }
  });
})();