0

I execute the following code

document.evaluate("/html/body/div/section/div/div/div[2]/div/div/button", document.body, null, 9, null). singleNodeValue.click();

on a "special" web page, moz-extension://653e6c0d-1f66-4b94-bc7c-78b55048de66/dashboard/html/browser.html

This click should insert something on the clipboard but instead, I get this error message:

UNHANDLED PROMISE REJECTION: NotAllowedError: Clipboard write was blocked due to lack of user activation.

I have read How to copy to clipboard on Firefox addon popup script? which discusses the same error message, but that situation is different from mine.

What do I need to do to insert something on the clipboard this way?

d-b
  • 695
  • 3
  • 14
  • 43
  • I fear you will need some user interaction and the page needs to have focus, otherwise you cannot copy things to the clipboard, or access anything clipboard related (just imagine any ad on any website just spamming your clipboard, that's what this tries to avoid). – somethinghere Jul 06 '23 at 11:57
  • @somethinghere I open the URL (see above) and then open the web inspector for that page so the page has focus and I have interacted with it. I think it is the "special URL" that is the problem? Can I somehow add it to "trusted sites" or something like that? – d-b Jul 06 '23 at 13:01
  • Once you open de web inspector, the page loses focus and the clipboard api is not longer accessible :) I have had this before, and it will just error out because the page isn't in focus, the web inspector at a local URL is. – somethinghere Jul 06 '23 at 13:30
  • @somethinghere That is good and bad news. The bad news first: could you set the focus to the page using JS, e.g., I found this question https://stackoverflow.com/questions/6976486/is-there-any-way-in-javascript-to-focus-the-document-content-area which seems similar enough, but I couldn't get it to work. Any ideas? – d-b Jul 06 '23 at 13:48
  • @somethinghere The good news is that my plan is to execute a JS using Applescript to script Chrome. It could be that JS executed that way doesn't change the focus/scope from the page. – d-b Jul 06 '23 at 13:50
  • That could indeed be the case, I am not sure. I am also pretty sure that if you programatically set the focus, it won't work - again, browsers don't want to hand the webpage the opportunity to focus itself, then execute things it could only do if the user interacted and was focused. It's an avenue ripe for abuse, and a hole I'm pretty sure browsers would close if you found it... It's annoying, but inevitable I fear. – somethinghere Jul 07 '23 at 07:14

1 Answers1

0

You can use the Clipboard API.

navigator.clipboard.writeText('Text to be copied')
  .then(() => {
    console.log('Text copied to clipboard');
  })
  .catch((error) => {
    console.error('Error copying text to clipboard:', error);
  });
  • 1
    I don't send the command to insert text into the clipboard, that is done by an browser extension when I send a click to a certain button in its UI. I need to allow the extension to write to the clipboard in the normal way (that is, by clicking manually) it works fine, it is just when I do it using Javascript I get an error. – d-b Jul 06 '23 at 11:44