7171 </form >
7272
7373 <!-- Ko-fi 贊助按鈕 -->
74- <div class =" has-flex-center has-top-spaced" >
75- <div id =" kofi-widget-container" ></div >
74+ <div class =" has-flex-center has-top-spaced-large" >
75+ <div class =" ts-text is-secondary has-bottom-spaced" >如果您覺得我的內容對您有幫助,歡迎贊助支持我繼續創作!</div >
76+ <div id =" kofi-widget-container" ref =" kofiContainer" ></div >
7677 </div >
7778 </div >
7879 </div >
@@ -124,30 +125,71 @@ ${form.message}`
124125 }
125126}
126127
128+ // 模板引用
129+ const kofiContainer = ref (null )
130+
127131// 初始化 Ko-fi 按鈕
128- onMounted (async () => {
129- await nextTick ()
130-
131- // 確保 kofiwidget2 已經載入
132- if (typeof window .kofiwidget2 !== ' undefined' ) {
132+ const initKofiWidget = () => {
133+ if (typeof window .kofiwidget2 !== ' undefined' && kofiContainer .value ) {
133134 try {
135+ // 清空容器
136+ kofiContainer .value .innerHTML = ' '
137+
138+ // 初始化 Ko-fi widget
134139 window .kofiwidget2 .init (' Support Me on Ko-fi' , ' #606466' , ' P5P0KOCNI' )
135- window .kofiwidget2 .draw ()
140+
141+ // 將 widget 渲染到指定容器
142+ const widgetHTML = window .kofiwidget2 .getHTML ()
143+ if (widgetHTML) {
144+ kofiContainer .value .innerHTML = widgetHTML
145+ } else {
146+ // 如果 getHTML 不可用,使用傳統方法
147+ window .kofiwidget2 .draw ()
148+ // 尋找生成的 widget 並移動到我們的容器
149+ const widget = document .querySelector (' iframe[src*="ko-fi.com"]' )
150+ if (widget && widget .parentNode ) {
151+ kofiContainer .value .appendChild (widget .parentNode )
152+ }
153+ }
136154 } catch (error) {
137155 console .log (' Ko-fi widget initialization failed:' , error)
156+ // 提供備用的贊助連結
157+ kofiContainer .value .innerHTML = `
158+ <a href =" https://ko-fi.com/P5P0KOCNI" target =" _blank" class =" ts-button is-outlined" >
159+ ☕ Support Me on Ko-fi
160+ </a >
161+ `
138162 }
163+ }
164+ }
165+
166+ onMounted (async () => {
167+ await nextTick ()
168+
169+ // 嘗試初始化 Ko-fi widget
170+ if (typeof window .kofiwidget2 !== ' undefined' ) {
171+ initKofiWidget ()
139172 } else {
140- // 如果還沒載入,等待一下再試
141- setTimeout (() => {
173+ // 如果腳本還沒載入,等待載入完成
174+ let attempts = 0
175+ const maxAttempts = 10
176+ const checkInterval = setInterval (() => {
177+ attempts++
142178 if (typeof window .kofiwidget2 !== ' undefined' ) {
143- try {
144- window .kofiwidget2 .init (' Support Me on Ko-fi' , ' #606466' , ' P5P0KOCNI' )
145- window .kofiwidget2 .draw ()
146- } catch (error) {
147- console .log (' Ko-fi widget delayed initialization failed:' , error)
179+ clearInterval (checkInterval)
180+ initKofiWidget ()
181+ } else if (attempts >= maxAttempts) {
182+ clearInterval (checkInterval)
183+ // 如果腳本載入失敗,提供備用連結
184+ if (kofiContainer .value ) {
185+ kofiContainer .value .innerHTML = `
186+ <a href =" https://ko-fi.com/P5P0KOCNI" target =" _blank" class =" ts-button is-outlined" >
187+ ☕ Support Me on Ko-fi
188+ </a >
189+ `
148190 }
149191 }
150- }, 1000 )
192+ }, 500 )
151193 }
152194})
153195 </script >
0 commit comments