|
| 1 | +function copyText(text, btn) { |
| 2 | + var ta = document.createElement('textarea'); |
| 3 | + ta.value = text; |
| 4 | + ta.style.cssText = 'position:fixed;left:-9999px;top:-9999px'; |
| 5 | + document.body.appendChild(ta); |
| 6 | + ta.select(); |
| 7 | + try { |
| 8 | + document.execCommand('copy'); |
| 9 | + btn.textContent = '已复制'; |
| 10 | + btn.classList.add('copied'); |
| 11 | + setTimeout(function() { btn.textContent = '复制'; btn.classList.remove('copied'); }, 1500); |
| 12 | + } catch(e) { |
| 13 | + btn.textContent = '失败'; |
| 14 | + } |
| 15 | + document.body.removeChild(ta); |
| 16 | +} |
| 17 | + |
| 18 | +document.addEventListener('click', function(e) { |
| 19 | + var btn = e.target.closest('.copy-btn'); |
| 20 | + if (!btn) return; |
| 21 | + copyText(btn.getAttribute('data-copy'), btn); |
| 22 | +}); |
| 23 | + |
| 24 | +var modal = document.getElementById('apiModal'); |
| 25 | +var modalBody = document.getElementById('modalBody'); |
| 26 | +var modalTitle = document.getElementById('modalTitle'); |
| 27 | + |
| 28 | +document.addEventListener('click', function(e) { |
| 29 | + var btn = e.target.closest('.api-btn'); |
| 30 | + if (!btn) return; |
| 31 | + var g = btn.getAttribute('data-gallery'); |
| 32 | + var base = window.location.origin; |
| 33 | + modalTitle.textContent = g + ' — API 调用'; |
| 34 | + var items = [ |
| 35 | + { label: '随机图(图片)', path: '/image/api/random?gallery=' + g }, |
| 36 | + { label: '随机图(JSON)', path: '/image/api/random?gallery=' + g + '&type=json' }, |
| 37 | + { label: '随机图(跳转)', path: '/image/api/random?gallery=' + g + '&type=redirect' }, |
| 38 | + { label: 'PC 随机图', path: '/image/api/random?gallery=' + g + '&device=pc' }, |
| 39 | + { label: 'Mobile 随机图', path: '/image/api/random?gallery=' + g + '&device=mobile' }, |
| 40 | + { label: 'PC JSON', path: '/image/api/random?gallery=' + g + '&device=pc&type=json' }, |
| 41 | + { label: 'Mobile JSON', path: '/image/api/random?gallery=' + g + '&device=mobile&type=json' } |
| 42 | + ]; |
| 43 | + var html = ''; |
| 44 | + for (var i = 0; i < items.length; i++) { |
| 45 | + var full = base + items[i].path; |
| 46 | + html += '<div class="api-row">' + |
| 47 | + '<span class="api-label">' + items[i].label + '</span>' + |
| 48 | + '<code class="api-url">' + full + '</code>' + |
| 49 | + '<button type="button" class="copy-btn" data-copy="' + full + '">复制</button>' + |
| 50 | + '</div>'; |
| 51 | + } |
| 52 | + modalBody.innerHTML = html; |
| 53 | + modal.hidden = false; |
| 54 | +}); |
| 55 | + |
| 56 | +document.getElementById('modalClose').addEventListener('click', function() { |
| 57 | + modal.hidden = true; |
| 58 | +}); |
| 59 | + |
| 60 | +modal.addEventListener('click', function(e) { |
| 61 | + if (e.target === modal) modal.hidden = true; |
| 62 | +}); |
| 63 | + |
| 64 | +document.addEventListener('keydown', function(e) { |
| 65 | + if (e.key === 'Escape' && !modal.hidden) modal.hidden = true; |
| 66 | +}); |
0 commit comments