Skip to content

Commit afcf694

Browse files
committed
Fix: change github start iframe to script
1 parent d545e12 commit afcf694

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

popup.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ <h3>Add New Problem</h3>
137137
</div>
138138

139139
<!-- 将 GitHub iframe 加载逻辑放在单独的脚本文件中 -->
140-
141-
<script async defer src="dist/loadGithubStar.js"></script>
142140

143141
<!-- 反馈按钮 -->
144142
<a href="https://github.com/xiaohajiayou/Leetcode-Mastery-Scheduler/issues"
@@ -486,5 +484,8 @@ <h5 class="problem-title mb-2">
486484
<script type="text/javascript" src="lib/bootstrap.bundle.min.js"></script>
487485
<script type="text/javascript" src="lib/fontawesome.js"></script>
488486
<script src="dist/popup.js"></script>
487+
488+
<!-- 直接引用外部脚本文件 -->
489+
<script src="dist/loadGithubStar.js"></script>
489490
</body>
490491
</html>

src/popup/script/loadGithubStar.js

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,78 @@
1+
// 设置超时时间
2+
const TIMEOUT_DURATION = 800;
13

2-
// 设置更短的超时时间
3-
const TIMEOUT_DURATION = 500 ;
4-
4+
// 主函数:加载GitHub Star按钮
55
function loadGithubStar() {
6+
console.log('开始加载GitHub Star按钮');
7+
68
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+
// 如果无法快速加载,直接显示静态按钮
1015
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);
2018
}, TIMEOUT_DURATION);
2119

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+
};
3541

36-
container.appendChild(iframe);
42+
container.appendChild(iframe);
43+
} catch (error) {
44+
console.error('加载GitHub Star时发生错误:', error);
45+
clearTimeout(timeout);
46+
addStaticButton(container);
47+
}
3748
}
3849

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+
}
3962

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

Comments
 (0)