Skip to content

Commit 05874cc

Browse files
author
Brian Vaughn
committed
Use exportFunction() to share clipboard copy with JS running in document/page context.
1 parent 0eac01a commit 05874cc

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
@@ -91,3 +91,19 @@ if (sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') {
9191
injectCode(
9292
';(' + installHook.toString() + '(window))' + saveNativeValues + detectReact,
9393
);
94+
95+
if (typeof exportFunction === 'function') {
96+
// eslint-disable-next-line no-undef
97+
exportFunction(
98+
text => {
99+
// Call clipboard.writeText from the extension content script
100+
// (as it has the clipboardWrite permission) and return a Promise
101+
// accessible to the webpage js code.
102+
return new window.Promise((resolve, reject) =>
103+
window.navigator.clipboard.writeText(text).then(resolve, reject),
104+
);
105+
},
106+
window.wrappedJSObject.__REACT_DEVTOOLS_GLOBAL_HOOK__,
107+
{defineAs: 'clipboardCopyText'},
108+
);
109+
}

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)