Skip to content

Commit 6df0d41

Browse files
author
Brian Vaughn
committed
Use exportFunction() to share clipboard copy with JS running in document/page context.
1 parent 2b903da commit 6df0d41

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

packages/react-devtools-extensions/firefox/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"scripts": ["build/background.js"]
4545
},
4646

47-
"permissions": ["file:///*", "http://*/*", "https://*/*"],
47+
"permissions": ["file:///*", "http://*/*", "https://*/*", "clipboardWrite"],
4848

4949
"content_scripts": [
5050
{

packages/react-devtools-extensions/src/injectGlobalHook.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,19 @@ if (document.contentType === 'text/html') {
9797
detectReact,
9898
);
9999
}
100+
101+
if (typeof exportFunction === 'function') {
102+
// eslint-disable-next-line no-undef
103+
exportFunction(
104+
text => {
105+
// Call clipboard.writeText from the extension content script
106+
// (as it has the clipboardWrite permission) and return a Promise
107+
// accessible to the webpage js code.
108+
return new window.Promise((resolve, reject) =>
109+
window.navigator.clipboard.writeText(text).then(resolve, reject),
110+
);
111+
},
112+
window.wrappedJSObject.__REACT_DEVTOOLS_GLOBAL_HOOK__,
113+
{defineAs: 'clipboardCopyText'},
114+
);
115+
}

packages/react-devtools-shared/src/backend/utils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,19 @@ export function cleanForBridge(
4040

4141
export function copyToClipboard(value: any): void {
4242
const safeToCopy = serializeToString(value);
43-
copy(safeToCopy === undefined ? 'undefined' : safeToCopy);
43+
const text = safeToCopy === undefined ? 'undefined' : safeToCopy;
44+
const {clipboardCopyText} = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
45+
46+
// On Firefox navigator.clipboard.writeText has to be called from
47+
// the content script js code (because it requires the clipboardWrite
48+
// permission to be allowed out of a "user handling" callback),
49+
// clipboardCopyText is an helper injected into the page from.
50+
// injectGlobalHook.
51+
if (typeof clipboardCopyText === 'function') {
52+
clipboardCopyText(text).catch(err => {});
53+
} else {
54+
copy(text);
55+
}
4456
}
4557

4658
export function copyWithSet(

0 commit comments

Comments
 (0)