|
10 | 10 | id = parts[toolsIndex + 1]; |
11 | 11 | } |
12 | 12 |
|
13 | | - if (!id) { |
| 13 | + if (id) { |
| 14 | + const payload = JSON.stringify({ id }); |
| 15 | + if (navigator.sendBeacon) { |
| 16 | + navigator.sendBeacon('/api/clicks', new Blob([payload], { type: 'application/json' })); |
| 17 | + } else { |
| 18 | + fetch('/api/clicks', { |
| 19 | + method: 'POST', |
| 20 | + headers: { 'Content-Type': 'application/json' }, |
| 21 | + body: payload, |
| 22 | + keepalive: true |
| 23 | + }); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + if (toolsIndex !== -1 && parts[toolsIndex + 1] && !path.endsWith('/app.html')) { |
| 28 | + const basePath = `/${parts.slice(0, toolsIndex + 2).join('/')}/`; |
| 29 | + const appUrl = `${basePath}app.html`; |
| 30 | + document.querySelectorAll('a[href]').forEach((link) => { |
| 31 | + const href = link.getAttribute('href'); |
| 32 | + if (!href || /^(https?:)?\/\//i.test(href) || href.startsWith('mailto:') || href.startsWith('#')) { |
| 33 | + return; |
| 34 | + } |
| 35 | + if (href === 'app.html' || href === './app.html' || href === 'app') { |
| 36 | + link.setAttribute('href', appUrl); |
| 37 | + } |
| 38 | + }); |
| 39 | + document.querySelectorAll('iframe[src]').forEach((frame) => { |
| 40 | + const src = frame.getAttribute('src'); |
| 41 | + if (src === 'app.html' || src === './app.html' || src === 'app') { |
| 42 | + frame.setAttribute('src', appUrl); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + if (!path.endsWith('/app.html') || toolsIndex === -1) { |
14 | 48 | return; |
15 | 49 | } |
16 | 50 |
|
17 | | - const payload = JSON.stringify({ id }); |
18 | | - if (navigator.sendBeacon) { |
19 | | - navigator.sendBeacon('/api/clicks', new Blob([payload], { type: 'application/json' })); |
| 51 | + if (document.querySelector('.tool-doc-link')) { |
20 | 52 | return; |
21 | 53 | } |
22 | | - fetch('/api/clicks', { |
23 | | - method: 'POST', |
24 | | - headers: { 'Content-Type': 'application/json' }, |
25 | | - body: payload, |
26 | | - keepalive: true |
| 54 | + |
| 55 | + const docLink = document.createElement('a'); |
| 56 | + docLink.href = path.replace(/app\.html$/, 'index.html'); |
| 57 | + docLink.className = 'tool-doc-link'; |
| 58 | + docLink.textContent = '\u8bf4\u660e'; |
| 59 | + docLink.setAttribute('aria-label', '\u67e5\u770b\u5de5\u5177\u8bf4\u660e'); |
| 60 | + docLink.style.cssText = [ |
| 61 | + 'position:fixed', |
| 62 | + 'top:18px', |
| 63 | + 'right:18px', |
| 64 | + 'padding:8px 14px', |
| 65 | + 'border-radius:999px', |
| 66 | + 'font-size:13px', |
| 67 | + 'font-weight:600', |
| 68 | + 'color:#0f172a', |
| 69 | + 'background:rgba(255,255,255,0.92)', |
| 70 | + 'text-decoration:none', |
| 71 | + 'box-shadow:0 10px 30px rgba(0,0,0,0.2)', |
| 72 | + 'backdrop-filter:blur(6px)', |
| 73 | + 'z-index:9999' |
| 74 | + ].join(';'); |
| 75 | + docLink.addEventListener('mouseenter', () => { |
| 76 | + docLink.style.transform = 'translateY(-1px)'; |
| 77 | + docLink.style.boxShadow = '0 12px 30px rgba(0,0,0,0.25)'; |
| 78 | + }); |
| 79 | + docLink.addEventListener('mouseleave', () => { |
| 80 | + docLink.style.transform = 'translateY(0)'; |
| 81 | + docLink.style.boxShadow = '0 10px 30px rgba(0,0,0,0.2)'; |
27 | 82 | }); |
| 83 | + |
| 84 | + document.body.appendChild(docLink); |
28 | 85 | })(); |
0 commit comments