@@ -862,29 +862,88 @@ const hasUnavailableApis = computed(() => {
862
862
return apiUrls .value .some (api => ! api .available );
863
863
});
864
864
865
- // 添加复制功能函数
866
- const copyResult = () => {
867
- if (translationResult .value ) {
868
- navigator .clipboard .writeText (translationResult .value )
869
- .then (() => {
870
- ElMessage .success (' 复制成功' );
871
- })
872
- .catch (() => {
873
- ElMessage .error (' 复制失败' );
874
- });
865
+ // 修改复制功能函数
866
+ const copyResult = async () => {
867
+ if (! translationResult .value ) return ;
868
+
869
+ try {
870
+ // 首先检查是否在安全上下文中
871
+ if (navigator .clipboard && window .isSecureContext ) {
872
+ await navigator .clipboard .writeText (translationResult .value );
873
+ ElMessage .success (' 复制成功' );
874
+ } else {
875
+ // 不在安全上下文中,使用后备方案
876
+ console .log (' 使用后备复制方案' );
877
+ fallbackCopy (translationResult .value );
878
+ }
879
+ } catch (error ) {
880
+ console .error (' 复制失败:' , error );
881
+ // 捕获到错误时也尝试使用后备方案
882
+ console .log (' 尝试使用后备复制方案' );
883
+ fallbackCopy (translationResult .value );
875
884
}
876
885
};
877
886
878
- // 添加复制替代翻译的函数
879
- const copyAlternativeResult = () => {
880
- if (alternativeTranslationsText .value ) {
881
- navigator .clipboard .writeText (alternativeTranslationsText .value )
882
- .then (() => {
883
- ElMessage .success (' 复制成功' );
884
- })
885
- .catch (() => {
886
- ElMessage .error (' 复制失败' );
887
- });
887
+ // 添加后备复制方案
888
+ const fallbackCopy = (text : string ) => {
889
+ try {
890
+ const textArea = document .createElement (' textarea' );
891
+ textArea .value = text ;
892
+
893
+ // 确保文本区域在视口内但不可见
894
+ textArea .style .cssText = ' position:fixed;top:0;left:0;opacity:0;' ;
895
+
896
+ document .body .appendChild (textArea );
897
+
898
+ if (navigator .userAgent .match (/ ipad| iphone/ i )) {
899
+ // iOS 设备特殊处理
900
+ textArea .contentEditable = ' true' ;
901
+ textArea .readOnly = false ;
902
+
903
+ const range = document .createRange ();
904
+ range .selectNodeContents (textArea );
905
+
906
+ const selection = window .getSelection ();
907
+ if (selection ) {
908
+ selection .removeAllRanges ();
909
+ selection .addRange (range );
910
+ }
911
+ textArea .setSelectionRange (0 , 999999 );
912
+ } else {
913
+ // 其他设备
914
+ textArea .select ();
915
+ }
916
+
917
+ const successful = document .execCommand (' copy' );
918
+ document .body .removeChild (textArea );
919
+
920
+ if (successful ) {
921
+ ElMessage .success (' 复制成功' );
922
+ } else {
923
+ throw new Error (' execCommand 返回 false' );
924
+ }
925
+ } catch (err ) {
926
+ console .error (' 后备复制失败:' , err );
927
+ ElMessage .error (' 复制失败,请手动复制' );
928
+ }
929
+ };
930
+
931
+ // 同样修改替代翻译的复制函数
932
+ const copyAlternativeResult = async () => {
933
+ if (! alternativeTranslationsText .value ) return ;
934
+
935
+ try {
936
+ if (navigator .clipboard && window .isSecureContext ) {
937
+ await navigator .clipboard .writeText (alternativeTranslationsText .value );
938
+ ElMessage .success (' 复制成功' );
939
+ } else {
940
+ console .log (' 使用后备复制方案' );
941
+ fallbackCopy (alternativeTranslationsText .value );
942
+ }
943
+ } catch (error ) {
944
+ console .error (' 复制失败:' , error );
945
+ console .log (' 尝试使用后备复制方案' );
946
+ fallbackCopy (alternativeTranslationsText .value );
888
947
}
889
948
};
890
949
0 commit comments