|
| 1 | +// 设置超时时间 |
| 2 | +const TIMEOUT_DURATION = 800; |
1 | 3 |
|
2 | | -// 设置更短的超时时间 |
3 | | -const TIMEOUT_DURATION = 500 ; |
4 | | - |
| 4 | +// 主函数:加载GitHub Star按钮 |
5 | 5 | function loadGithubStar() { |
| 6 | + console.log('开始加载GitHub Star按钮'); |
| 7 | + |
6 | 8 | const container = document.getElementById('github-star-container'); |
7 | | - if (!container) return; |
8 | | - |
9 | | - // 如果浏览器且无法快速加载,直接显示静态按钮 |
| 9 | + if (!container) { |
| 10 | + console.error('找不到github-star-container元素'); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + // 如果无法快速加载,直接显示静态按钮 |
10 | 15 | const timeout = setTimeout(() => { |
11 | | - container.innerHTML = ` |
12 | | - <a href="https://github.com/xiaohajiayou/Leetcode-Mastery-Scheduler" |
13 | | - target="_blank" |
14 | | - style="text-decoration: none;"> |
15 | | - <button class="btn custom-btn" style="padding: 4px 12px;"> |
16 | | - <i class="fab fa-github"></i> Star |
17 | | - </button> |
18 | | - </a> |
19 | | - `; |
| 16 | + console.log('GitHub Star加载超时,显示静态按钮'); |
| 17 | + addStaticButton(container); |
20 | 18 | }, TIMEOUT_DURATION); |
21 | 19 |
|
22 | | - const iframe = document.createElement('iframe'); |
23 | | - iframe.src = "https://ghbtns.com/github-btn.html?user=xiaohajiayou&repo=Leetcode-Mastery-Scheduler&type=star&count=true"; |
24 | | - iframe.frameBorder = "0"; |
25 | | - iframe.scrolling = "0"; |
26 | | - iframe.width = "100"; |
27 | | - iframe.height = "20"; |
28 | | - |
29 | | - iframe.onload = () => { |
30 | | - clearTimeout(timeout); |
31 | | - requestAnimationFrame(() => { |
32 | | - iframe.style.opacity = "0.9"; |
33 | | - }); |
34 | | - }; |
| 20 | + try { |
| 21 | + const iframe = document.createElement('iframe'); |
| 22 | + iframe.src = "https://ghbtns.com/github-btn.html?user=xiaohajiayou&repo=Leetcode-Mastery-Scheduler&type=star&count=true"; |
| 23 | + iframe.frameBorder = "0"; |
| 24 | + iframe.scrolling = "0"; |
| 25 | + iframe.width = "100"; |
| 26 | + iframe.height = "20"; |
| 27 | + |
| 28 | + iframe.onload = () => { |
| 29 | + console.log('GitHub Star iframe加载成功'); |
| 30 | + clearTimeout(timeout); |
| 31 | + requestAnimationFrame(() => { |
| 32 | + iframe.style.opacity = "0.9"; |
| 33 | + }); |
| 34 | + }; |
| 35 | + |
| 36 | + iframe.onerror = () => { |
| 37 | + console.error('GitHub Star iframe加载失败'); |
| 38 | + clearTimeout(timeout); |
| 39 | + addStaticButton(container); |
| 40 | + }; |
35 | 41 |
|
36 | | - container.appendChild(iframe); |
| 42 | + container.appendChild(iframe); |
| 43 | + } catch (error) { |
| 44 | + console.error('加载GitHub Star时发生错误:', error); |
| 45 | + clearTimeout(timeout); |
| 46 | + addStaticButton(container); |
| 47 | + } |
37 | 48 | } |
38 | 49 |
|
| 50 | +// 添加静态按钮的辅助函数 |
| 51 | +function addStaticButton(container) { |
| 52 | + container.innerHTML = ` |
| 53 | + <a href="https://github.com/xiaohajiayou/Leetcode-Mastery-Scheduler" |
| 54 | + target="_blank" |
| 55 | + style="text-decoration: none;"> |
| 56 | + <button class="btn custom-btn" style="padding: 4px 12px;"> |
| 57 | + <i class="fab fa-github"></i> Star |
| 58 | + </button> |
| 59 | + </a> |
| 60 | + `; |
| 61 | +} |
39 | 62 |
|
40 | | -setTimeout(loadGithubStar, 100); |
| 63 | +// 在DOMContentLoaded事件触发时执行 |
| 64 | +document.addEventListener('DOMContentLoaded', function() { |
| 65 | + console.log('DOM已加载,准备加载GitHub Star按钮'); |
| 66 | + |
| 67 | + // 立即尝试加载 |
| 68 | + loadGithubStar(); |
| 69 | + |
| 70 | + // 添加备份检查,如果5秒后容器仍然为空,则使用静态按钮 |
| 71 | + setTimeout(function() { |
| 72 | + const container = document.getElementById('github-star-container'); |
| 73 | + if (container && container.children.length === 0) { |
| 74 | + console.log('5秒后容器仍为空,添加静态按钮'); |
| 75 | + addStaticButton(container); |
| 76 | + } |
| 77 | + }, 3000); |
| 78 | +}); |
0 commit comments