| 
 | 1 | +document.addEventListener('DOMContentLoaded', () => {  | 
 | 2 | +  const containers = document.querySelectorAll('[data-today-in-history]');  | 
 | 3 | +  if (containers.length === 0) return;  | 
 | 4 | + | 
 | 5 | +  const apiUrl = 'https://60s.viki.moe/v2/today-in-history';  | 
 | 6 | + | 
 | 7 | +  // 渲染加载态  | 
 | 8 | +  containers.forEach(container => {  | 
 | 9 | +    container.innerHTML = `  | 
 | 10 | +      <div style="display:flex;align-items:center;justify-content:center;padding:20px;">  | 
 | 11 | +        <div style="width:28px;height:28px;border:3px solid #93c5fd;border-top-color:transparent;border-radius:50%;animation:spin 0.8s linear infinite"></div>  | 
 | 12 | +        <span style="margin-left:10px;color:#666;">正在加载历史上的今天...</span>  | 
 | 13 | +      </div>  | 
 | 14 | +      <style>@keyframes spin{to{transform:rotate(360deg)}}</style>  | 
 | 15 | +    `;  | 
 | 16 | +  });  | 
 | 17 | + | 
 | 18 | +  fetch(apiUrl)  | 
 | 19 | +    .then(r => {  | 
 | 20 | +      if (!r.ok) throw new Error(`HTTP 错误: ${r.status}`);  | 
 | 21 | +      return r.json();  | 
 | 22 | +    })  | 
 | 23 | +    .then(json => {  | 
 | 24 | +      if (!json || json.code !== 200 || !json.data) throw new Error('接口返回异常');  | 
 | 25 | + | 
 | 26 | +      const { date, items = [] } = json.data;  | 
 | 27 | +      const titleText = `历史上的今天 - ${date || ''}`;  | 
 | 28 | + | 
 | 29 | +      // 事件类型不再显示标签,颜色映射移除  | 
 | 30 | + | 
 | 31 | +      const escapeHtml = s => String(s || '')  | 
 | 32 | +        .replace(/&/g, '&')  | 
 | 33 | +        .replace(/</g, '<')  | 
 | 34 | +        .replace(/>/g, '>')  | 
 | 35 | +        .replace(/"/g, '"')  | 
 | 36 | +        .replace(/'/g, ''');  | 
 | 37 | + | 
 | 38 | +      const buildItem = item => {  | 
 | 39 | +        const year = escapeHtml(item.year);  | 
 | 40 | +        const title = escapeHtml(item.title);  | 
 | 41 | +        const desc = escapeHtml(item.description || '');  | 
 | 42 | +        const link = item.link || '#';  | 
 | 43 | +        // 不展示事件类型标签  | 
 | 44 | + | 
 | 45 | +        return `  | 
 | 46 | +          <li style="display:flex;gap:12px;padding:12px 0;border-bottom:1px solid #eee;">  | 
 | 47 | +            <div style="flex:0 0 auto;min-width:64px;height:28px;background:#f3f4f6;border-radius:14px;display:flex;align-items:center;justify-content:center;font-weight:700;color:#374151;">${year}</div>  | 
 | 48 | +            <div style="flex:1;min-width:0;">  | 
 | 49 | +              <div style="display:flex;align-items:flex-start;gap:10px;flex-wrap:wrap;">  | 
 | 50 | +                <a class="tih-link" href="${link}" target="_blank" rel="noopener" title="点击打开链接">${title}</a>  | 
 | 51 | +              </div>  | 
 | 52 | +              ${desc ? `<div style="margin-top:6px;color:#4b5563;line-height:1.75;white-space:normal;overflow:visible;text-overflow:initial;word-break:break-word;overflow-wrap:anywhere;">${desc}</div>` : ''}  | 
 | 53 | +            </div>  | 
 | 54 | +          </li>  | 
 | 55 | +        `;  | 
 | 56 | +      };  | 
 | 57 | + | 
 | 58 | +      const listHtml = items.length  | 
 | 59 | +        ? items.map(buildItem).join('')  | 
 | 60 | +        : '<div style="padding:16px;color:#6b7280;">今天暂无记录</div>';  | 
 | 61 | + | 
 | 62 | +      const cardHtml = `  | 
 | 63 | +        <style>  | 
 | 64 | +          .tih-link{  | 
 | 65 | +            display:inline-block;  | 
 | 66 | +            font-size:1.05rem;  | 
 | 67 | +            color:#1d4ed8; /* 蓝色强调,突出可点击 */  | 
 | 68 | +            text-decoration:none;  | 
 | 69 | +            background:rgba(37,99,235,0.08);  | 
 | 70 | +            border:1px solid rgba(37,99,235,0.15);  | 
 | 71 | +            padding:2px 8px;  | 
 | 72 | +            border-radius:8px;  | 
 | 73 | +            line-height:1.4;  | 
 | 74 | +            white-space:normal;  | 
 | 75 | +            overflow:visible;  | 
 | 76 | +            text-overflow:initial;  | 
 | 77 | +            word-break:break-word;  | 
 | 78 | +            overflow-wrap:anywhere;  | 
 | 79 | +            transition:background .2s ease, box-shadow .2s ease, color .2s ease;  | 
 | 80 | +          }  | 
 | 81 | +          .tih-link:hover{  | 
 | 82 | +            background:rgba(37,99,235,0.14);  | 
 | 83 | +            box-shadow:0 2px 10px rgba(37,99,235,0.18);  | 
 | 84 | +            text-decoration:underline;  | 
 | 85 | +          }  | 
 | 86 | +          .tih-link:focus-visible{  | 
 | 87 | +            outline:none;  | 
 | 88 | +            box-shadow:0 0 0 3px rgba(37,99,235,0.28);  | 
 | 89 | +          }  | 
 | 90 | +        </style>  | 
 | 91 | +        <div style="max-width:900px;margin:16px auto;padding:18px 20px;background:#fff;border-radius:12px;box-shadow:0 8px 24px rgba(149,157,165,0.2);">  | 
 | 92 | +          <div style="display:flex;align-items:center;justify-content:flex-start;margin-bottom:10px;">  | 
 | 93 | +            <h2 style="margin:0;font-size:20px;color:#111827;">${titleText}</h2>  | 
 | 94 | +          </div>  | 
 | 95 | +          <ol style="list-style:none;padding:0;margin:0;">${listHtml}</ol>  | 
 | 96 | +        </div>  | 
 | 97 | +      `;  | 
 | 98 | + | 
 | 99 | +      containers.forEach(c => c.innerHTML = cardHtml);  | 
 | 100 | +    })  | 
 | 101 | +    .catch(err => {  | 
 | 102 | +      const errorHtml = `  | 
 | 103 | +        <div style="color:#b91c1c;padding:15px;background:#fee2e2;border-left:4px solid #ef4444;border-radius:8px;">  | 
 | 104 | +          加载历史上的今天失败: ${err.message}  | 
 | 105 | +        </div>  | 
 | 106 | +      `;  | 
 | 107 | +      containers.forEach(c => c.innerHTML = errorHtml);  | 
 | 108 | +    });  | 
 | 109 | +});  | 
 | 110 | + | 
 | 111 | + | 
0 commit comments