1- console . log ( "\n %c Post-Abstract-AI 开源博客文章摘要AI生成工具 %c https://github.com/zhheo/Post-Abstract-AI \n" , "color: #fadfa3; background: #030307; padding:5px 0;" , "background: #fadfa3; padding:5px 0;" )
1+ console . log ( "\n %c Post-Abstract-AI 博客文章摘要AI生成工具 %c https://github.com/zhheo/Post-Abstract-AI \n" , "color: #fadfa3; background: #030307; padding:5px 0;" , "background: #fadfa3; padding:5px 0;" )
22
33function tianliGPT ( usePjax ) {
44 var tianliGPTIsRunning = false ;
@@ -37,13 +37,21 @@ function tianliGPT(usePjax) {
3737
3838 const aiTitleTextDiv = document . createElement ( 'div' ) ;
3939 aiTitleTextDiv . className = 'tianliGPT-title-text' ;
40- aiTitleTextDiv . textContent = 'AI摘要' ;
40+ if ( typeof tianliGPT_Title === "undefined" ) {
41+ aiTitleTextDiv . textContent = 'AI摘要' ;
42+ } else {
43+ aiTitleTextDiv . textContent = tianliGPT_Title ;
44+ }
4145 aiTitleDiv . appendChild ( aiTitleTextDiv ) ;
4246
4347 const aiTagDiv = document . createElement ( 'div' ) ;
4448 aiTagDiv . className = 'tianliGPT-tag' ;
4549 aiTagDiv . id = 'tianliGPT-tag' ;
46- aiTagDiv . textContent = 'TianliGPT' ;
50+ if ( typeof tianliGPT_Name === "undefined" ) {
51+ aiTagDiv . textContent = 'TianliGPT' ;
52+ } else {
53+ aiTagDiv . textContent = tianliGPT_Name ;
54+ }
4755 aiTitleDiv . appendChild ( aiTagDiv ) ;
4856
4957 const aiExplanationDiv = document . createElement ( 'div' ) ;
@@ -123,27 +131,64 @@ function tianliGPT(usePjax) {
123131 const controller = new AbortController ( ) ;
124132 const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeout ) ;
125133 const response = await fetch ( apiUrl , { signal : controller . signal } ) ;
134+ clearTimeout ( timeoutId ) ; // 清除定时器,避免不必要的abort
135+
126136 if ( response . ok ) {
127137 const data = await response . json ( ) ;
128138 return {
129- summary : data . summary ,
130- audioId : data . id // 获取音频的 ID
139+ summary : data . summary
131140 } ;
132141 } else {
133- if ( response . status === 402 ) {
134- document . querySelectorAll ( '.post-TianliGPT' ) . forEach ( el => {
135- el . style . display = 'none' ;
136- } ) ;
137- let info = "Key错误或余额不足,请充值后请求新的文章"
138- throw new Error ( 'TianliGPT:' + info ) ;
142+ let info = ""
143+ const errorData = await response . json ( ) ; // 假设错误响应也是JSON格式
144+
145+ if ( response . status === 514 ) {
146+ info = "TianliGPT is only available in mainland China, and is not yet open to overseas users, so stay tuned!" ;
147+ tianliGPT . aiShowAnimation ( info ) ;
148+ return info ;
139149 }
150+
151+ // 根据403错误处理
140152 if ( response . status === 403 ) {
141- let info = "请求来源于Key不匹配,可能因为这个Key没有在summary.zhheo.com进行域名绑定。"
142- tianliGPT . aiShowAnimation ( info )
143- return info
144- }
145- if ( response . status === 405 ) {
146- let info = "你的网站设置了Referrer-Policy为same-origin,这会导致Tianli无法验证你的请求来源。TianliGPT依赖refer进行来源判断,特别是meta标签的referrer属性需要修改,至少为origin。例如:<meta name=\"referrer\" content=\"origin\">"
153+ switch ( errorData . err_code ) {
154+ case 1 :
155+ // 你的具体错误处理逻辑,例如显示某个特定的错误信息
156+ info = "你的网站设置了Referrer-Policy为same-origin,这会导致Tianli无法验证你的请求来源。TianliGPT依赖refer进行来源判断,特别是meta标签的referrer属性需要修改,至少为origin。例如:<meta name=\"referrer\" content=\"origin\">" ;
157+ tianliGPT . aiShowAnimation ( info ) ;
158+ return info ;
159+ case 2 :
160+ // 你的具体错误处理逻辑,例如显示某个特定的错误信息
161+ info = "你正在使用的tianliGPT_key已经被其他网站绑定或不存在,请检查当前网站地址是否在summary.zhheo.com中已绑定。" ;
162+ tianliGPT . aiShowAnimation ( info ) ;
163+ return info ;
164+ // 这里可以添加更多的err_code判断分支
165+ case 3 :
166+ info = "参数缺失,请检查是否正确配置tianliGPT_key"
167+ tianliGPT . aiShowAnimation ( info ) ;
168+ return info ;
169+ case 4 :
170+ document . querySelectorAll ( '.post-TianliGPT' ) . forEach ( el => {
171+ el . style . display = 'none' ;
172+ } ) ;
173+ info = "Key错误或余额不足,请充值后请求新的文章"
174+ throw new Error ( 'TianliGPT:' + info ) ;
175+ case 5 :
176+ info = errorData . err_msg
177+ tianliGPT . aiShowAnimation ( info ) ;
178+ return info ;
179+ case 6 :
180+ info = errorData . err_msg
181+ tianliGPT . aiShowAnimation ( info ) ;
182+ return info ;
183+ case 7 :
184+ info = errorData . err_msg
185+ tianliGPT . aiShowAnimation ( info ) ;
186+ return info ;
187+ default :
188+ // 默认的处理逻辑
189+ tianliGPT . aiShowAnimation ( "未知错误,请检查API文档" ) ;
190+ return "未知错误,请检查API文档" ;
191+ }
147192 }
148193 }
149194 } catch ( error ) {
@@ -250,9 +295,6 @@ function tianliGPT(usePjax) {
250295 }
251296 tianliGPT . fetchTianliGPT ( content ) . then ( data => {
252297 const summary = data . summary ;
253- const audioId = data . audioId ;
254- const buttonDiv = document . querySelector ( '.tianliGPT-tag' ) ;
255- buttonDiv . dataset . audioId = audioId ; // 将音频的 ID 存储在按钮的 dataset 属性中
256298 tianliGPT . aiShowAnimation ( summary ) ;
257299 } )
258300 }
@@ -298,24 +340,4 @@ tianliGPT(false);
298340
299341document . addEventListener ( 'pjax:complete' , function ( ) {
300342 tianliGPT ( true ) ;
301- } )
302-
303- document . addEventListener ( 'click' , function ( event ) {
304- const target = event . target ;
305- if ( target . classList . contains ( 'tianliGPT-tag' ) ) {
306- playAudio ( ) ;
307- }
308- } ) ;
309-
310- document . addEventListener ( 'click' , function ( event ) {
311- const target = event . target ;
312- const buttonDiv = document . querySelector ( '.tianliGPT-tag' ) ;
313-
314- buttonDiv . addEventListener ( 'click' , function ( ) {
315- buttonDiv . classList . toggle ( 'playing' ) ;
316- } ) ;
317-
318- if ( target === buttonDiv || buttonDiv . contains ( target ) ) {
319- playAudio ( ) ;
320- }
321- } ) ;
343+ } )
0 commit comments