MCPcopy
hub / github.com/mitmproxy/mitmproxy / copyToClipboard

Function copyToClipboard

web/src/js/utils.ts:158–198  ·  view source on GitHub ↗
(
    textPromise: Promise<string>,
)

Source from the content-addressed store, hash-verified

156 * Never throws unless textPromise is rejected.
157 */
158export async function copyToClipboard(
159 textPromise: Promise<string>,
160): Promise<void> {
161 // Try the new clipboard APIs first. If that fails, use textarea fallback.
162 try {
163 await navigator.clipboard.write([
164 new ClipboardItem({
165 "text/plain": textPromise,
166 }),
167 ]);
168 return;
169 } catch (err) {
170 console.warn(err);
171 }
172
173 const text = await textPromise;
174
175 try {
176 await navigator.clipboard.writeText(text);
177 return;
178 } catch (err) {
179 console.warn(err);
180 }
181
182 const t = document.createElement("textarea");
183 t.value = text;
184 t.style.position = "absolute";
185 t.style.opacity = "0";
186 document.body.appendChild(t);
187 try {
188 t.focus();
189 t.select();
190 if (!document.execCommand("copy")) {
191 throw "failed to copy";
192 }
193 } catch {
194 alert(text);
195 } finally {
196 t.remove();
197 }
198}
199
200export async function copyViewContentDataToClipboard(
201 contentViewData: ContentViewData | undefined,

Callers 2

copyFunction · 0.90

Calls 5

warnMethod · 0.80
focusMethod · 0.80
writeMethod · 0.45
selectMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…