Prevent connection lost in mitmweb when pressing Download button. (#4849)

This commit is contained in:
Vinh Quang Tran 2021-10-09 23:32:07 +07:00 committed by GitHub
parent 9346002e0f
commit 0b48fdfc29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -80,6 +80,11 @@ export default function FlowMenu(): JSX.Element {
) )
} }
// Reference: https://stackoverflow.com/a/63627688/9921431
const openInNewTab = (url) => {
const newWindow = window.open(url, '_blank', 'noopener,noreferrer')
if (newWindow) newWindow.opener = null
}
function DownloadButton({flow}: { flow: Flow }) { function DownloadButton({flow}: { flow: Flow }) {
if (flow.type !== "http") if (flow.type !== "http")
@ -87,23 +92,23 @@ function DownloadButton({flow}: { flow: Flow }) {
if (flow.request.contentLength && !flow.response?.contentLength) { if (flow.request.contentLength && !flow.response?.contentLength) {
return <Button icon="fa-download" return <Button icon="fa-download"
onClick={() => window.location.href = MessageUtils.getContentURL(flow, flow.request)} onClick={() => openInNewTab(MessageUtils.getContentURL(flow, flow.request))}
>Download</Button> >Download</Button>
} }
if (flow.response) { if (flow.response) {
const response = flow.response; const response = flow.response;
if (!flow.request.contentLength && flow.response.contentLength) { if (!flow.request.contentLength && flow.response.contentLength) {
return <Button icon="fa-download" return <Button icon="fa-download"
onClick={() => window.location.href = MessageUtils.getContentURL(flow, response)} onClick={() => openInNewTab(MessageUtils.getContentURL(flow, response))}
>Download</Button> >Download</Button>
} }
if (flow.request.contentLength && flow.response.contentLength) { if (flow.request.contentLength && flow.response.contentLength) {
return <Dropdown text={ return <Dropdown text={
<Button icon="fa-download" onClick={() => 1}>Download</Button> <Button icon="fa-download" onClick={() => 1}>Download</Button>
} options={{"placement": "bottom-start"}}> } options={{"placement": "bottom-start"}}>
<MenuItem onClick={() => window.location.href = MessageUtils.getContentURL(flow, flow.request)}>Download <MenuItem onClick={() => openInNewTab(MessageUtils.getContentURL(flow, flow.request))}>Download
request</MenuItem> request</MenuItem>
<MenuItem onClick={() => window.location.href = MessageUtils.getContentURL(flow, response)}>Download <MenuItem onClick={() => openInNewTab(MessageUtils.getContentURL(flow, response))}>Download
response</MenuItem> response</MenuItem>
</Dropdown> </Dropdown>
} }