@@ -4,18 +4,14 @@ import {convertImage} from '../utils.js';
4
4
5
5
const { i18n} = window . config ;
6
6
7
- async function doCopy ( content , btn ) {
8
- const success = await clippie ( content ) ;
9
- showTemporaryTooltip ( btn , success ? i18n . copy_success : i18n . copy_error ) ;
10
- }
11
-
12
7
export function initCopyContent ( ) {
13
8
const btn = document . getElementById ( 'copy-content' ) ;
14
9
if ( ! btn || btn . classList . contains ( 'disabled' ) ) return ;
15
10
16
11
btn . addEventListener ( 'click' , async ( ) => {
17
12
if ( btn . classList . contains ( 'is-loading' ) ) return ;
18
- let content , isImage ;
13
+ let content ;
14
+ let isRasterImage = false ;
19
15
const link = btn . getAttribute ( 'data-link' ) ;
20
16
21
17
// when data-link is present, we perform a fetch. this is either because
@@ -28,7 +24,7 @@ export function initCopyContent() {
28
24
const contentType = res . headers . get ( 'content-type' ) ;
29
25
30
26
if ( contentType . startsWith ( 'image/' ) && ! contentType . startsWith ( 'image/svg' ) ) {
31
- isImage = true ;
27
+ isRasterImage = true ;
32
28
content = await res . blob ( ) ;
33
29
} else {
34
30
content = await res . text ( ) ;
@@ -43,15 +39,14 @@ export function initCopyContent() {
43
39
content = Array . from ( lineEls ) . map ( ( el ) => el . textContent ) . join ( '' ) ;
44
40
}
45
41
46
- try {
47
- await doCopy ( content , btn ) ;
48
- } catch {
49
- if ( isImage ) { // convert image to png as last-resort as some browser only support png copy
50
- try {
51
- await doCopy ( await convertImage ( content , 'image/png' ) , btn ) ;
52
- } catch {
53
- showTemporaryTooltip ( btn , i18n . copy_error ) ;
54
- }
42
+ // try copy original first, if that fails and it's an image, convert it to png
43
+ const success = await clippie ( content ) ;
44
+ if ( success ) {
45
+ showTemporaryTooltip ( btn , i18n . copy_success ) ;
46
+ } else {
47
+ if ( isRasterImage ) {
48
+ const success = await clippie ( await convertImage ( content , 'image/png' ) ) ;
49
+ showTemporaryTooltip ( btn , success ? i18n . copy_success : i18n . copy_error ) ;
55
50
} else {
56
51
showTemporaryTooltip ( btn , i18n . copy_error ) ;
57
52
}
0 commit comments