File tree Expand file tree Collapse file tree 4 files changed +61
-2
lines changed Expand file tree Collapse file tree 4 files changed +61
-2
lines changed Original file line number Diff line number Diff line change @@ -153,7 +153,9 @@ const en = {
153
153
version : 'Version' ,
154
154
vocabOnly : 'Vocab only' ,
155
155
writePromptToStart : 'Write a prompt to start a new session' ,
156
- you : 'You'
156
+ you : 'You' ,
157
+ copiedNotPrivate : 'Content copied, but your connection is not private' ,
158
+ notCopiedNotPrivate : "Couldn't copy content. Connection is not private"
157
159
} satisfies BaseTranslation ;
158
160
159
161
export default en ;
Original file line number Diff line number Diff line change @@ -620,6 +620,14 @@ type RootTranslation = {
620
620
* You
621
621
*/
622
622
you : string
623
+ /**
624
+ * Content copied, but your connection is not private
625
+ */
626
+ copiedNotPrivate : string
627
+ /**
628
+ * Couldn't copy content. Connection is not private
629
+ */
630
+ notCopiedNotPrivate : string
623
631
}
624
632
625
633
export type TranslationFunctions = {
@@ -1220,6 +1228,14 @@ The completion in progress will stop
1220
1228
* You
1221
1229
*/
1222
1230
you : ( ) => LocalizedString
1231
+ /**
1232
+ * Content copied, but your connection is not private
1233
+ */
1234
+ copiedNotPrivate : ( ) => LocalizedString
1235
+ /**
1236
+ * Couldn't copy content. Connection is not private
1237
+ */
1238
+ notCopiedNotPrivate : ( ) => LocalizedString
1223
1239
}
1224
1240
1225
1241
export type Formatters = { }
Original file line number Diff line number Diff line change 1
1
<script lang =" ts" >
2
2
import { Files } from ' lucide-svelte' ;
3
+ import { toast } from ' svelte-sonner' ;
3
4
4
5
import LL from ' $i18n/i18n-svelte' ;
5
6
8
9
export let content: string ;
9
10
10
11
function copyContent() {
11
- navigator .clipboard .writeText (content );
12
+ if (navigator .clipboard && window .isSecureContext ) {
13
+ navigator .clipboard .writeText (content );
14
+ } else {
15
+ // HACK
16
+ // This is a workaround to copy text content on HTTP connections.
17
+ // https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem
18
+ const textArea = document .createElement (' textarea' );
19
+ textArea .value = content ;
20
+ document .body .appendChild (textArea );
21
+ textArea .select ();
22
+ try {
23
+ document .execCommand (' copy' );
24
+ toast .warning ($LL .copiedNotPrivate ());
25
+ } catch (e ) {
26
+ console .error (e );
27
+ toast .error ($LL .notCopiedNotPrivate ());
28
+ }
29
+ document .body .removeChild (textArea );
30
+ }
12
31
}
13
32
</script >
14
33
Original file line number Diff line number Diff line change @@ -131,6 +131,28 @@ test.describe('Session interaction', () => {
131
131
) ;
132
132
} ) ;
133
133
134
+ test ( 'can copy text on an insecure connection' , async ( { page } ) => {
135
+ // Mock insecure context before navigating
136
+ await page . addInitScript ( ( ) => {
137
+ Object . defineProperty ( window , 'isSecureContext' , { value : false } ) ;
138
+ } ) ;
139
+
140
+ await page . goto ( '/' ) ;
141
+ await mockCompletionResponse ( page , MOCK_SESSION_1_RESPONSE_1 ) ;
142
+ await page . getByText ( 'Sessions' , { exact : true } ) . click ( ) ;
143
+ await page . getByTestId ( 'new-session' ) . click ( ) ;
144
+ await chooseModel ( page , MOCK_API_TAGS_RESPONSE . models [ 0 ] . name ) ;
145
+ await promptTextarea . fill ( 'Who would win in a fight between Emma Watson and Jessica Alba?' ) ;
146
+ await page . getByText ( 'Run' ) . click ( ) ;
147
+ await expect ( page . locator ( '.session__history' ) . getByTitle ( 'Copy' ) ) . toHaveCount ( 2 ) ;
148
+
149
+ const toastWarning = page . getByText ( 'Content copied, but your connection is not private' ) ;
150
+ await expect ( toastWarning ) . not . toBeVisible ( ) ;
151
+
152
+ await page . locator ( '.session__history' ) . getByTitle ( 'Copy' ) . first ( ) . click ( ) ;
153
+ await expect ( toastWarning ) . toBeVisible ( ) ;
154
+ } ) ;
155
+
134
156
test ( 'can copy the whole session content to clipboard' , async ( { page } ) => {
135
157
await page . goto ( '/' ) ;
136
158
await page . evaluate ( ( ) => navigator . clipboard . writeText ( '' ) ) ;
You can’t perform that action at this time.
0 commit comments