0

I'm trying to achieve a simple action on a Google Chrome extension: when user is on a tab, it could click on the extension icon, and something (like a string) will be copied into its clipboard.

The only thing that I don't know how to deal with in this process is the copy action to the clipboard. Can you help me on this?

What I've done so far:
- added the clipboardWrite permission to my manifest.json
- added the browser_action section to my manifest.json to handle click on the icon extension
- added something like this into my background.js file:

chrome.browserAction.onClicked.addListener((tab) => {
  console.log(tab.url);
  const string = "something";
  // Here I need to add code to copy `string` to clipboard, looking for a way to do it
});

Any help on this would be useful :)

Bardyl
  • 221
  • 3
  • 13
  • Use the standard approach, see [Using execCommand (Javascript) to copy hidden text to clipboard](https://stackoverflow.com/q/31593297) – wOxxOm May 31 '20 at 15:16
  • I'm not working in a context of basic webpage. I don't understand how this can be a solution. – Bardyl May 31 '20 at 16:52
  • Simply use it, it Just Works. The background script runs in a hidden background page which is a standard DOM page. – wOxxOm May 31 '20 at 17:32
  • No. That not works. I used another solution I will propose as answer. But your solution is not working. – Bardyl Jun 01 '20 at 06:35
  • It works for me and lots of other extensions so I guess you've used it incorrectly. Note that there are many answers in the topic I've linked. Here's a more generic one: [Copy to Clipboard in Chrome Extension](https://stackoverflow.com/a/18455088) – wOxxOm Jun 01 '20 at 07:03
  • Deleted my answer per wOxxOm's preference. Still, check this: https://stackoverflow.com/questions/54386279/how-can-i-use-navigator-clipboard-readtext-in-a-chrome-extension/57327762#57327762 – Ofek Shilon Jun 01 '20 at 14:04

0 Answers0