@@ -4,6 +4,7 @@ import { dragMoveListener, resizeMoveListener } from "./drag";
4
4
import interact from "interactjs" ;
5
5
import { getAIResponse } from "./api" ;
6
6
import { setAllowAutoScroll , updateAllowAutoScroll } from "./scrollControl" ;
7
+
7
8
export function createPopup ( text , rect ) {
8
9
const popup = document . createElement ( "div" ) ;
9
10
@@ -69,7 +70,7 @@ export function createPopup(text, rect) {
69
70
abortController . signal ,
70
71
ps ,
71
72
iconContainer ,
72
- aiResponseContainer ,
73
+ aiResponseContainer
73
74
) ;
74
75
75
76
refreshIcon . addEventListener ( "click" , ( event ) => {
@@ -83,7 +84,7 @@ export function createPopup(text, rect) {
83
84
abortController . signal ,
84
85
ps ,
85
86
iconContainer ,
86
- aiResponseContainer ,
87
+ aiResponseContainer
87
88
) ;
88
89
} ) ;
89
90
@@ -92,18 +93,6 @@ export function createPopup(text, rect) {
92
93
updateAllowAutoScroll ( aiResponseContainer ) ;
93
94
} ) ;
94
95
95
- const clickOutsideHandler = ( e ) => {
96
- if ( ! popup . contains ( e . target ) ) {
97
- document . body . removeChild ( popup ) ;
98
- document . removeEventListener ( "click" , clickOutsideHandler ) ;
99
- abortController . abort ( ) ;
100
- }
101
- } ;
102
-
103
- setTimeout ( ( ) => {
104
- document . addEventListener ( "click" , clickOutsideHandler ) ;
105
- } , 0 ) ;
106
-
107
96
const dragHandle = createDragHandle ( ) ;
108
97
popup . appendChild ( dragHandle ) ;
109
98
@@ -131,31 +120,34 @@ export function createPopup(text, rect) {
131
120
132
121
export function stylePopup ( popup , rect ) {
133
122
popup . id = "ai-popup" ;
134
- popup . style . position = "absolute" ;
135
- popup . style . width = "580px" ;
136
- popup . style . height = "380px" ;
137
- popup . style . paddingTop = "20px" ;
138
- popup . style . backgroundColor = "#f6f6f6a8" ;
139
- popup . style . boxShadow =
140
- "0 0 1px #0009,0 0 2px #0000000d,0 38px 90px #00000040" ;
141
- popup . style . backdropFilter = "blur(10px)" ;
142
- popup . style . borderRadius = "12px" ;
143
- popup . style . zIndex = "1000" ;
144
- popup . style . fontFamily = "Arial, sans-serif" ;
145
- popup . style . overflow = "auto" ;
123
+ Object . assign ( popup . style , {
124
+ position : "absolute" ,
125
+ width : "580px" ,
126
+ height : "380px" ,
127
+ paddingTop : "20px" ,
128
+ backgroundColor : "#f6f6f6a8" ,
129
+ boxShadow : "0 0 1px #0009, 0 0 2px #0000000d, 0 38px 90px #00000040" ,
130
+ backdropFilter : "blur(10px)" ,
131
+ borderRadius : "12px" ,
132
+ zIndex : "1000" ,
133
+ fontFamily : "Arial, sans-serif" ,
134
+ overflow : "auto" ,
135
+ } ) ;
146
136
147
137
const { adjustedX, adjustedY } = adjustPopupPosition ( rect , popup ) ;
148
138
popup . style . left = `${ adjustedX } px` ;
149
139
popup . style . top = `${ adjustedY } px` ;
150
140
}
151
141
152
142
export function styleResponseContainer ( container ) {
153
- container . style . position = "relative" ;
143
+ Object . assign ( container . style , {
144
+ position : "relative" ,
145
+ width : "100%" ,
146
+ height : "calc(100% - 40px)" ,
147
+ marginTop : "20px" ,
148
+ overflow : "auto" ,
149
+ } ) ;
154
150
container . id = "ai-response-container" ;
155
- container . style . width = "100%" ;
156
- container . style . height = "calc(100% - 40px)" ;
157
- container . style . marginTop = "20px" ;
158
- container . style . overflow = "auto" ;
159
151
}
160
152
161
153
function adjustPopupPosition ( rect , popup ) {
@@ -173,42 +165,101 @@ function adjustPopupPosition(rect, popup) {
173
165
adjustedY = rect . top + scrollY - popupHeight ;
174
166
}
175
167
176
- if ( adjustedX < scrollX ) adjustedX = scrollX ;
177
- if ( adjustedX + popupWidth > viewportWidth + scrollX )
178
- adjustedX = viewportWidth + scrollX - popupWidth ;
179
- if ( adjustedY < scrollY ) adjustedY = scrollY ;
180
- if ( adjustedY + popupHeight > viewportHeight + scrollY )
181
- adjustedY = viewportHeight + scrollY - popupHeight ;
168
+ adjustedX = Math . max (
169
+ scrollX ,
170
+ Math . min ( adjustedX , viewportWidth + scrollX - popupWidth )
171
+ ) ;
172
+ adjustedY = Math . max (
173
+ scrollY ,
174
+ Math . min ( adjustedY , viewportHeight + scrollY - popupHeight )
175
+ ) ;
182
176
183
177
return { adjustedX, adjustedY } ;
184
178
}
185
179
186
180
function createDragHandle ( ) {
187
181
const dragHandle = document . createElement ( "div" ) ;
188
- dragHandle . style . position = "absolute" ;
189
- dragHandle . style . top = "0" ;
190
- dragHandle . style . left = "0" ;
191
- dragHandle . style . width = "100%" ;
192
- dragHandle . style . height = "40px" ;
193
- dragHandle . style . backgroundColor = "#F2F2F7" ;
194
- dragHandle . style . cursor = "move" ;
195
- dragHandle . style . display = "flex" ;
196
- dragHandle . style . alignItems = "center" ;
197
- dragHandle . style . justifyContent = "center" ;
198
- dragHandle . style . marginBottom = "10px" ;
199
- dragHandle . style . fontWeight = "bold" ;
200
- dragHandle . style . color = "#007AFF" ;
201
- dragHandle . style . fontSize = "16px" ;
182
+ Object . assign ( dragHandle . style , {
183
+ position : "absolute" ,
184
+ top : "0" ,
185
+ left : "0" ,
186
+ width : "100%" ,
187
+ height : "40px" ,
188
+ backgroundColor : "#F2F2F7" ,
189
+ cursor : "move" ,
190
+ display : "flex" ,
191
+ alignItems : "center" ,
192
+ justifyContent : "center" ,
193
+ padding : "0 10px" ,
194
+ boxSizing : "border-box" ,
195
+ } ) ;
196
+
197
+ const titleContainer = document . createElement ( "div" ) ;
198
+ titleContainer . style . display = "flex" ;
199
+ titleContainer . style . alignItems = "center" ;
202
200
203
201
const logo = document . createElement ( "img" ) ;
204
202
logo . src = chrome . runtime . getURL ( "icons/icon24.png" ) ;
205
203
logo . style . height = "24px" ;
206
204
logo . style . marginRight = "10px" ;
207
205
208
- dragHandle . appendChild ( logo ) ;
209
-
210
206
const textNode = document . createTextNode ( "DeepSeek AI" ) ;
211
- dragHandle . appendChild ( textNode ) ;
207
+ titleContainer . appendChild ( logo ) ;
208
+ titleContainer . appendChild ( textNode ) ;
209
+
210
+ const closeButton = document . createElement ( "button" ) ;
211
+ Object . assign ( closeButton . style , {
212
+ display : "none" ,
213
+ background : "none" ,
214
+ border : "none" ,
215
+ cursor : "pointer" ,
216
+ padding : "0" ,
217
+ transition : "all 0.2s ease" ,
218
+ position : "absolute" ,
219
+ right : "10px" ,
220
+ } ) ;
221
+
222
+ const closeIcon = document . createElement ( "img" ) ;
223
+ closeIcon . src = chrome . runtime . getURL ( "icons/close.svg" ) ;
224
+ closeIcon . style . width = "20px" ;
225
+ closeIcon . style . height = "20px" ;
226
+
227
+ closeButton . appendChild ( closeIcon ) ;
228
+
229
+ dragHandle . appendChild ( titleContainer ) ;
230
+ dragHandle . appendChild ( closeButton ) ;
231
+
232
+ dragHandle . addEventListener ( "mouseenter" , ( ) => {
233
+ closeButton . style . display = "block" ;
234
+ } ) ;
235
+
236
+ dragHandle . addEventListener ( "mouseleave" , ( ) => {
237
+ closeButton . style . display = "none" ;
238
+ // 重置关闭按钮状态
239
+ closeIcon . src = chrome . runtime . getURL ( "icons/close.svg" ) ;
240
+ closeButton . style . transform = "scale(1)" ;
241
+ } ) ;
242
+
243
+ closeButton . addEventListener ( "mouseenter" , ( ) => {
244
+ closeIcon . src = chrome . runtime . getURL ( "icons/closeClicked.svg" ) ;
245
+ closeButton . style . transform = "scale(1.1)" ;
246
+ } ) ;
247
+
248
+ closeButton . addEventListener ( "mouseleave" , ( ) => {
249
+ closeIcon . src = chrome . runtime . getURL ( "icons/close.svg" ) ;
250
+ closeButton . style . transform = "scale(1)" ;
251
+ } ) ;
252
+
253
+ closeButton . addEventListener ( "click" , ( event ) => {
254
+ event . stopPropagation ( ) ;
255
+
256
+ setTimeout ( ( ) => {
257
+ const popup = document . getElementById ( "ai-popup" ) ;
258
+ if ( popup ) {
259
+ document . body . removeChild ( popup ) ;
260
+ }
261
+ } , 200 ) ;
262
+ } ) ;
212
263
213
264
return dragHandle ;
214
265
}
0 commit comments