Skip to content

In Safari Clipboard.WriteText() does not work in a button on_click callback #561

Open
@gdworkman

Description

@gdworkman

Hello,

I am attempting to create a button that copies some text to the user's clipboard. I have included a simplified version of the relevant code below:

put_scope("example_scope")
with use_scope("example_scope):
    put_button('Copy', onclick=example_copy_callback)

def example_copy_callback() -> None:
    text_to_copy = "Hello World"
    run_js("""navigator.clipboard.writeText(out).then(
        () => {
            console.log('Content copied to clipboard');
        }, (err) => {
            console.error('Failed to copy: ', err);
        });""", out = text_to_copy
    )

This is working in both Chrome and Firefox, however with Safari I am getting the following error:
Failed to copy: – NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

I believe this has something to do with the fact that in Safari, clipboard.writeText() must be called within user gesture event handlers such as pointerdown or pointerup (source). Unfortunately, I am a noob when it comes to javascript and web development, and I am not clear on how this detail would interact with the underlying onclick callback and run_js() method of PyWebIO.

Any insight into why this is failing or viable workarounds would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions