sarsnake
sarsnake

Reputation: 27723

Reading the contents of Clipboard in FF

I am able to do that in IE, but FF doesn't allow to do something like:

$("#txtBox").bind('paste', function (e) {
        alert('pasting text!!!!');           
        alert(window.clipboardData.getData("Text"));
        window.event.returnValue = false;


    });

I am required to trap the clipboard contents onpaste, then populate a table with this content. We are allowing people to copy and paste from Excel.

What are some of the ways that are being used to achieve this in FF? Thanks

Upvotes: 2

Views: 354

Answers (1)

k0pernikus
k0pernikus

Reputation: 66787

For recent version of Firefox navigator.clipboard.writeText and navigator.clipboard.readText and avaiable.

Just keep in mind that you can't check the permissions via

await navigator.permissions.query({ name: 'clipboard-write' });

Yet this works for Firefox 133:

<html>
<head>
    <script type="application/javascript">
        const copy = async (text) => {
            if (!navigator.userAgent.includes('Firefox')) {
                await navigator.permissions.query({ name: 'clipboard-write' });
            }

            await navigator.clipboard.writeText(text);
            const result = await navigator.clipboard.readText();
            window.alert(result);
        }
    </script>
</head>
<body>
    <span onclick="copy('some text')" >
        Click here to copy 'some text'
    </span>
</body>
</html>

Upvotes: 0

Related Questions