@@ -42,6 +42,23 @@ function getDate() {
4242 return { year, month, day } ;
4343}
4444
45+ function updateResult ( status , message ) {
46+ const el = document . getElementById ( "result" ) ;
47+
48+ el . className =
49+ "text-sm whitespace-pre-wrap text-left w-full px-3 py-2 rounded-md border transition" ;
50+
51+ if ( status === "success" ) {
52+ el . classList . add ( "bg-green-50" , "text-green-700" , "border-green-300" ) ;
53+ } else if ( status === "error" ) {
54+ el . classList . add ( "bg-red-50" , "text-red-700" , "border-red-300" ) ;
55+ } else if ( status === "loading" ) {
56+ el . classList . add ( "bg-orange-50" , "text-orange-700" , "border-orange-300" , "loading-dots" ) ;
57+ }
58+
59+ el . textContent = message ;
60+ }
61+
4562function setupEventListeners ( ) {
4663 document . getElementById ( "exportBtn" ) . addEventListener ( "click" , async ( ) => {
4764 chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
@@ -76,22 +93,27 @@ function setupEventListeners() {
7693 chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
7794 chrome . tabs . sendMessage ( tabs [ 0 ] . id , { action : "get_sesskey" } , async ( res ) => {
7895 const sesskey = res ?. sesskey ;
96+ const resultEl = document . getElementById ( "result" ) ;
97+
7998 if ( ! sesskey ) {
8099 console . log ( "no touch sesskey" ) ;
81- document . getElementById ( "result" ) . textContent =
82- "無法獲取 sesskey,請先登入 Moodle 或者重新整理頁面" ;
100+ updateResult ( "error" , "無法獲取 sesskey,請先登入 Moodle 或重新整理頁面" ) ;
83101 return ;
84102 }
103+
85104 console . log ( "import" ) ;
86- document . getElementById ( "result" ) . textContent = "匯入作業進入行事曆中..." ;
105+ updateResult ( "loading" , "匯入作業進入行事曆中" ) ;
106+
87107 const { year, month, day } = getDate ( ) ;
108+
88109 chrome . runtime . sendMessage (
89110 { action : "import_events" , sesskey, year, month, day } ,
90111 ( res ) => {
112+ resultEl . classList . remove ( "loading-dots" ) ;
91113 if ( res ?. ok ) {
92- document . getElementById ( "result" ) . textContent = `成功匯入 ${ res . count } 個作業事件` ;
114+ updateResult ( "success" , `成功匯入 ${ res . count } 個作業事件` ) ;
93115 } else {
94- document . getElementById ( "result" ) . textContent = res ?. error || "發生錯誤,請稍後再試" ;
116+ updateResult ( "error" , res ?. error || "發生錯誤,請稍後再試" ) ;
95117 }
96118 }
97119 ) ;
0 commit comments