Copy in popup script doesn't work inspite of including "clipboardWrite" permission

Hi,

I am developing a webextension for a security product which manage passwords. We have a feature which will allow our users to copy a password, which will be cleared from clipboard after their preffered timeout. I had to move the code for coping the string to the popup script, since firefox does not allow an element to be focussed nor selected. This does not work in google chrome for us, the timer placed in popup script works only when the popup is open and it doesn’t clear the copied text in firefox for some unknown reason(could be the same as in chrome). Please Advice.

What code are you using to modify the clipboard?

If you use a timer - make sure that the page is still visible (i.e. that the popup is not closed).
If you use the clipboard API (i.e. adding a listener to the “copy” event and then trigger the event via document.execCommand("copy")), don’t use just clear the data, but also set some data, e.g. event.clipboardData.setData("text/plain", " ");.

And note that you are able to copy data to the clipboard from the background page when out-of-process WebExtensions are enabled. Eventually this is going to be enabled by default, but until then, visit about:config and set extensions.webextensions.remote to true.

2 Likes

I am using a timer right now and it does not work when popup is closed. I am not aware of the clipboard api please share a document.

My need is to clear the copied text after a particular time.

Maybe this will help:

I have used this method and this does not work in Firefox since firefox wouldn’t allow an element to be focussed nor selected in background script.

Thank you @rob for helping me with a workaround for the issue i was facing with copy to clipboard.

I have now changed my implementation based on the sample given in this link

function to copy the text is now implemented in popup script and the function to clear the clipboard is implemented in content script.