wxt icon indicating copy to clipboard operation
wxt copied to clipboard

Use `navigator.clipboard` api but an error was reported "Cannot read properties of undefined"

Open lapse12 opened this issue 1 year ago • 1 comments

Describe the bug

I tried the navigator. clipboard.writeText and navigator. clipboard.write api, which work well in the IDE, but I encountered error during testing them

TypeError: Cannot read properties of undefined (reading 'writeText')
image image

Reproduction

minimal.zip

Steps to reproduce

pnpm i wxt

System Info

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 78.95 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.2 - ~/.nvm/versions/node/v18.20.2/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v18.20.2/bin/npm
    pnpm: 8.15.4 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 127.0.6533.89
    Safari: 17.4.1
  npmPackages:
    wxt: ^0.19.1 => 0.19.2

Used Package Manager

pnpm

Validations

lapse12 avatar Aug 02 '24 06:08 lapse12

@lapse12 Can't use clipboard API on the background script now. navigator.clipboard API doesn't exist.

So…you can exec on the browser(popup or content-scripts) or offscreen API or sendMessage hacks.

Some reference URLs are listed below.

  • Cannot read clipboard from service worker in a MV3 chrome extension
    • https://issues.chromium.org/issues/40738001
  • MDN Clipboard API security considerations
    • https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#security_considerations
  • Hack through Offscreen API example
    • https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples/cookbook.offscreen-clipboard-write

1natsu172 avatar Aug 02 '24 07:08 1natsu172

I'm gonna close this as it's not an issue with WXT, but a question about how to use the clipboard API in general.

aklinker1 avatar Aug 15 '24 22:08 aklinker1