@@ -104,35 +104,69 @@ <h4 style="font-size: 18px; margin: 0; color: #c0c0ff;">🤖 Forum AI: ChatGPT A
104
104
</ style >
105
105
106
106
< script >
107
- document . getElementById ( "askButton" ) . addEventListener ( "click" , async function ( ) {
108
- const query = document . getElementById ( "userQuery" ) . value ;
109
- if ( query . trim ( ) === "" ) {
110
- alert ( "Please ask a question!" ) ;
111
- return ;
107
+ // script.js
108
+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
109
+ const floatingButton = document . getElementById ( "floatingButton" ) ;
110
+ const widgetContainer = document . getElementById ( "widgetContainer" ) ;
111
+ const closeWidgetButton = document . getElementById ( "closeWidgetButton" ) ;
112
+ const askButton = document . getElementById ( "askButton" ) ;
113
+ const userQuery = document . getElementById ( "userQuery" ) ;
114
+ const responseBox = document . getElementById ( "responseBox" ) ;
115
+ const responseContent = document . getElementById ( "responseContent" ) ;
116
+
117
+ // 1) Open the widget
118
+ floatingButton . addEventListener ( "click" , ( ) => {
119
+ widgetContainer . style . display = "block" ;
120
+ userQuery . focus ( ) ;
121
+ } ) ;
122
+
123
+ // 2) Close the widget
124
+ closeWidgetButton . addEventListener ( "click" , ( ) => {
125
+ widgetContainer . style . display = "none" ;
126
+ } ) ;
127
+
128
+ // 3) Submit query (and disable the button until done)
129
+ askButton . addEventListener ( "click" , async ( ) => {
130
+ const query = userQuery . value . trim ( ) ;
131
+ if ( ! query ) {
132
+ alert ( "Please ask a question!" ) ;
133
+ return ;
112
134
}
113
135
114
- document . getElementById ( "responseBox" ) . style . display = "block" ;
115
- document . getElementById ( "responseContent" ) . textContent = "Processing..." ;
136
+ // disable & show loading state
137
+ askButton . disabled = true ;
138
+ askButton . textContent = "Waiting for response…" ;
139
+ responseBox . style . display = "block" ;
140
+ responseContent . textContent = "Processing… 🤖" ;
116
141
117
142
try {
118
- const res = await fetch ( "https://api.pollinations.ai/" , {
119
- method : "POST" ,
120
- headers : {
121
- "Content-Type" : "application/json" ,
122
- } ,
123
- body : JSON . stringify ( {
124
- model : "openai-large" , // or other available models if specified by Pollinations
125
- messages : [ { role : "user" , content : query } ] ,
126
- } ) ,
127
- } ) ;
128
-
129
- const data = await res . json ( ) ;
130
- const answer = data . choices [ 0 ] . message . content . trim ( ) ;
131
- document . getElementById ( "responseContent" ) . textContent = answer ;
143
+ const res = await fetch ( "https://api.pollinations.ai/" , {
144
+ method : "POST" ,
145
+ headers : { "Content-Type" : "application/json" } ,
146
+ body : JSON . stringify ( {
147
+ model : "openai-large" ,
148
+ messages : [ { role : "user" , content : query } ]
149
+ } )
150
+ } ) ;
151
+
152
+ if ( ! res . ok ) throw new Error ( `API returned ${ res . status } ` ) ;
153
+ const data = await res . json ( ) ;
154
+ const answer = data . choices ?. [ 0 ] ?. message ?. content ?. trim ( ) || "No response." ;
155
+ responseContent . textContent = answer ;
156
+
132
157
} catch ( err ) {
133
- console . error ( err ) ;
134
- document . getElementById ( "responseContent" ) . textContent = "Oops, something went wrong!" ;
158
+ console . error ( err ) ;
159
+ responseContent . textContent = "Oops, something went wrong!" ;
160
+ } finally {
161
+ // re‑enable
162
+ askButton . disabled = false ;
163
+ askButton . textContent = "Ask Forum AI" ;
135
164
}
136
- } ) ;
165
+ } ) ;
137
166
167
+ // 4) Allow Enter key to submit
168
+ userQuery . addEventListener ( "keydown" , e => {
169
+ if ( e . key === "Enter" ) askButton . click ( ) ;
170
+ } ) ;
171
+ } ) ;
138
172
</ script >
0 commit comments