1
1
import type { FC } from 'react' ;
2
- import React , { useRef , useEffect } from 'react' ;
2
+ import React , { useEffect , useRef } from 'react' ;
3
3
import { useDispatch } from 'react-redux' ;
4
4
import type { Dispatch } from 'redux' ;
5
5
@@ -99,7 +99,10 @@ export const ServerPane: FC<ServerPaneProps> = ({
99
99
if ( ! webview ) {
100
100
return ;
101
101
}
102
- const addEventListenerOnce = ( e : 'did-attach' , cb : ( ) => void ) : void => {
102
+ const addEventListenerOnce = (
103
+ e : 'did-attach' | 'new-window' ,
104
+ cb : ( ) => void
105
+ ) : void => {
103
106
const handler = ( ) => {
104
107
cb ( ) ;
105
108
webview . removeEventListener ( e , handler ) ;
@@ -119,10 +122,43 @@ export const ServerPane: FC<ServerPaneProps> = ({
119
122
} , 300 ) ;
120
123
} ;
121
124
125
+ const handleDidStartLoading = ( ) : void => {
126
+ webview . executeJavaScript ( `
127
+ document.addEventListener('click', function(event) {
128
+ const fileDownloadURL = 'https://open.rocket.chat/file-upload';
129
+ const isFileDownloadURL = event.target.href.startsWith(fileDownloadURL);
130
+ const isTargetBlank = event.target.target === '_blank';
131
+
132
+ const fileName = event.target.href.split('/').pop().split('?')[0];
133
+
134
+ if (isFileDownloadURL && isTargetBlank) {
135
+ event.preventDefault()
136
+
137
+ let downloadURL = event.target.href
138
+ if (!downloadURL.endsWith('?download')) downloadURL += '?download'
139
+
140
+ const dynamicAnchor = document.createElement('a');
141
+ dynamicAnchor.target = '_blank';
142
+ dynamicAnchor.download = fileName;
143
+ dynamicAnchor.href = downloadURL;
144
+
145
+ dynamicAnchor.addEventListener('click', function(e) {
146
+ e.stopPropagation();
147
+ });
148
+
149
+ document.body.appendChild(dynamicAnchor);
150
+ dynamicAnchor.click();
151
+ }
152
+ })
153
+ ` ) ;
154
+ } ;
155
+
122
156
addEventListenerOnce ( 'did-attach' , handleAttachReady ) ;
157
+ webview . addEventListener ( 'did-start-loading' , handleDidStartLoading ) ;
123
158
124
159
return ( ) => {
125
160
webview . removeEventListener ( 'did-attach' , handleAttachReady ) ;
161
+ webview . removeEventListener ( 'did-start-loading' , handleDidStartLoading ) ;
126
162
} ;
127
163
} , [ dispatch , serverUrl ] ) ;
128
164
0 commit comments