0

I need a Chrome extension like this:

  • Click on extension icon should create an unique ID
  • Copy it to clipboard
  • Write it in console

manifest.json

{
  "manifest_version": 3,
  "name": "uniqID",
  "version": "1.0",
  "description": "Generate uniqID",
  "icons": {
    "48": "icon.png"
  },
  "action": {
    "default_icon": "icon.png"
  },
  "background": {
    "service_worker": "sw.js"
  },
  "permissions": [
    "activeTab",
    "clipboardWrite"
  ]
}

sw.js

chrome.action.onClicked.addListener(function (tab) {
    console.log('test');
});

There is no error, but clicking on extension's icon - nothing happens.
Im expecting test in console

Also, I see service worker (inactive) a short time after loading extension

Please help

provance
  • 877
  • 6
  • 10
  • 1) Replace `browser_action` with `action`. 2) Due to the bug in Chrome you can't use navigator.clipboard in a service worker. Use document.execCommand in the offline document as shown in [the official example](https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples/cookbook.offscreen-clipboard-write). – wOxxOm Sep 02 '23 at 10:54
  • @wOxxOm - I replace `browser_action` with `action`. I also max simplify `sw.js` so just `console.log('test')` exists. Now there is no error but nothing happes, console i empty. See my post update, pls. Also service_worker becomes inactive, a short time after loading extension – provance Sep 02 '23 at 13:09
  • 1) [How to see background.js console?](/a/10258029), 2) Service worker isn't meant to be persistent, so it becoming inactive is the correct behavior. – wOxxOm Sep 02 '23 at 13:41

0 Answers0