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:
Lijiawei 2022-04-21 02:00:31 +08:00 committed by GitHub
parent a8b6f48374
commit d1d854f012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 5 deletions

View File

@ -22,6 +22,8 @@
([#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

View 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();
}
}
}

View File

@ -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);