mirror of
https://github.com/mitmproxy/mitmproxy.git
synced 2024-11-27 23:30:28 +00:00
Fix mitmweb export copy failed in non-secure domain (#5265)
* Fix mitmweb export copy failed in non-secure domain https://github.com/mitmproxy/mitmproxy/issues/5264 * Set the generic type of Promise to void * move clipboard copy to contrib, adjust for TypeScript Co-authored-by: Maximilian Hils <git@maximilianhils.com>
This commit is contained in:
parent
a8b6f48374
commit
d1d854f012
@ -22,7 +22,9 @@
|
||||
([#3965](https://github.com/mitmproxy/mitmproxy/issues/3965), @mhils)
|
||||
* Fix text truncation for full-width characters
|
||||
([#4278](https://github.com/mitmproxy/mitmproxy/issues/4278), @kjy00302)
|
||||
|
||||
* Fix mitmweb export copy failed in non-secure domain.
|
||||
([#5264](https://github.com/mitmproxy/mitmproxy/issues/5264), @Pactortester)
|
||||
|
||||
## 19 March 2022: mitmproxy 8.0.0
|
||||
|
||||
### Major Changes
|
||||
|
27
web/src/js/contrib/clipboard.tsx
Normal file
27
web/src/js/contrib/clipboard.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
// Adapted from https://stackoverflow.com/a/65996386/934719
|
||||
/**
|
||||
* `navigator.clipboard.writeText()`, but with an additional fallback for non-secure contexts.
|
||||
*/
|
||||
export function copyToClipboard(text: string): Promise<void> {
|
||||
// navigator clipboard requires a security context such as https
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
return navigator.clipboard.writeText(text);
|
||||
} else {
|
||||
let t = document.createElement("textarea");
|
||||
t.value = text;
|
||||
t.style.position = "absolute";
|
||||
t.style.opacity = "0";
|
||||
document.body.appendChild(t);
|
||||
try {
|
||||
t.focus();
|
||||
t.select();
|
||||
document.execCommand("copy");
|
||||
return Promise.resolve();
|
||||
} catch (err) {
|
||||
alert(text);
|
||||
return Promise.reject(err);
|
||||
} finally {
|
||||
t.remove();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
import {fetchApi, runCommand} from "../utils";
|
||||
import {runCommand} from "../utils";
|
||||
import {Flow} from "../flow";
|
||||
import {copyToClipboard} from "../contrib/clipboard";
|
||||
|
||||
|
||||
export const copy = async (flow: Flow, format: string): Promise<void> => {
|
||||
let ret = await runCommand("export", format, `@${flow.id}`);
|
||||
if(ret.value) {
|
||||
await navigator.clipboard.writeText(ret.value);
|
||||
} else if(ret.error) {
|
||||
if (ret.value) {
|
||||
await copyToClipboard(ret.value);
|
||||
} else if (ret.error) {
|
||||
alert(ret.error)
|
||||
} else {
|
||||
console.error(ret);
|
||||
|
Loading…
Reference in New Issue
Block a user