How to paste text from clipboard into extension text field?

430 views
Skip to first unread message

SLAWI MAHDI

unread,
Nov 9, 2022, 9:57:28 AM11/9/22
to Chromium Extensions
I want to create a Paste button (id : paste) in my extension's popup.html window, which should paste the contents of the clipboard into a text field (id : inpt) in the same window

document.addEventListener('DOMContentLoaded', function() {
    var paste = document.getElementById('paste');
    paste.addEventListener('click', function() {

                  navigator.clipboard.readText()

      .then(txt => {
        document.getElementById("inpt").value = txt;
      })
    });                  
   });

I did the following code but without success

Juraj M.

unread,
Nov 9, 2022, 10:18:48 AM11/9/22
to Chromium Extensions, mahd...@gmail.com
I think you need a "clipboardRead" permission for that:

You can add it as Optional permission. Then request it with `browser.permissions.request({permissions: ['clipboardRead']})`.

SLAWI MAHDI

unread,
Nov 9, 2022, 10:46:34 AM11/9/22
to Chromium Extensions, juraj....@gmail.com, SLAWI MAHDI
Hi,

I added the "clipboardRead" in the manifest.json permissions, however I did not understand how I should request it with ` browser.permissions. request({ permissions: [ 'clipboardRead' ]})`?

Stefan Van Damme

unread,
Nov 9, 2022, 1:20:18 PM11/9/22
to Chromium Extensions, mahd...@gmail.com, juraj....@gmail.com
Hi Mahdi,


Thanks,

SLAWI MAHDI

unread,
Nov 10, 2022, 6:02:11 AM11/10/22
to Chromium Extensions, stefa...@gmail.com, SLAWI MAHDI, juraj....@gmail.com
Hi Stefan,

Thank you 

Stefan Van Damme

unread,
Nov 10, 2022, 6:30:55 AM11/10/22
to Chromium Extensions, mahd...@gmail.com, Stefan Van Damme, juraj....@gmail.com
Hi Mahdi,

You are welcome!

Thanks,
Stefan vd

Reply all
Reply to author
Forward
0 new messages