From c4b6a8445e22c0fc10cb712b02ea02654a3c4bf0 Mon Sep 17 00:00:00 2001 From: luvi Date: Mon, 13 Feb 2023 08:25:28 +0000 Subject: [PATCH] [FIX] web: fix copying to clipboard no longer working Since commit (1), the write/writeText were no longer correctly called, because the browser native functions couldn't be called. Sometimes, it raised the "Illegal invocation..." error, or could have an unexpected behavior, preventing the value to be copied in the clipboard, which made the button pointless. No additional test was added, since it is difficult to test the clipboard API programmatically, and the current test coverage is all that we can do, without using the real clipboard object. (1): 9cdcd1c1f7219030386d2c90205ea083eb835a13 closes odoo/odoo#112843 X-original-commit: 767922d43334a6cbccdb4be64f207813240fd314 Signed-off-by: Aaron Bohy (aab) --- .../views/fields/copy_clipboard/copy_button.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/addons/web/static/src/views/fields/copy_clipboard/copy_button.js b/addons/web/static/src/views/fields/copy_clipboard/copy_button.js index 778a2a5b47e..7318da43fb2 100644 --- a/addons/web/static/src/views/fields/copy_clipboard/copy_button.js +++ b/addons/web/static/src/views/fields/copy_clipboard/copy_button.js @@ -28,15 +28,16 @@ export class CopyButton extends Component { // any kind of content can be copied into the clipboard using // the appropriate native methods if (typeof this.props.content === "string" || this.props.content instanceof String) { - write = browser.navigator.clipboard.writeText; + write = (value) => browser.navigator.clipboard.writeText(value); } else { - write = browser.navigator.clipboard.write; + write = (value) => browser.navigator.clipboard.write(value); } - write(this.props.content).then(() => { - this.showTooltip(); - }).catch((error) => { - browser.console.warn("This browser doesn't grant access to copy to clipboard"); - }); + try { + await write(this.props.content); + } catch(error) { + return browser.console.warn(error); + } + this.showTooltip(); } } CopyButton.template = "web.CopyButton";