Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui.code() can't copy in safari browser #2377

Closed
Wzhipeng opened this issue Jan 14, 2024 · 3 comments
Closed

ui.code() can't copy in safari browser #2377

Wzhipeng opened this issue Jan 14, 2024 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Wzhipeng
Copy link

Wzhipeng commented Jan 14, 2024

Description

image The ui.code component uses navigator.clipboard.writeText from the copy function, but an error will be reported in the safari browser and cannot be copied.

image

@falkoschindler
Copy link
Contributor

Thanks for reporting this issue, @Wzhipeng!

As I just learned from ChatGPT:

Modern browsers often require a user-initiated action, like a click or tap, to access the clipboard. Ensure your clipboard writing code is triggered by such an event.

I managed to fix it by writing to the clipboard directly on the client without making the detour over the server. This way Safari recognizes the clipboard access as user-initiated and doesn't raise an error.

@falkoschindler falkoschindler added this to the 1.4.11 milestone Jan 14, 2024
@falkoschindler falkoschindler added the bug Something isn't working label Jan 14, 2024
@falkoschindler falkoschindler self-assigned this Jan 14, 2024
@adrastaea
Copy link

adrastaea commented Feb 15, 2024

I managed to fix it by writing to the clipboard directly on the client without making the detour over the server. This way Safari recognizes the clipboard access as user-initiated and doesn't raise an error.

Could you show a code snippet? On Safari, I seem to still be getting Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. with the following code

@ui.page()
async def example_page(client: Client):
    async def copy_url():
        await client.connected()
        await client.run_javascript(f"navigator.clipboard.writeText('{BASE_URL}{app.url_path_for('example_page')}')")
    ui.icon("link").on("click", copy_url)

@rodja
Copy link
Member

rodja commented Feb 16, 2024

@adrastaea I think the trick is in this line

icon._props['onclick'] = f'navigator.clipboard.writeText(`{full_code}`)' # pylint: disable=protected-access

And it will get nicer in the next release with #2536. It allows registering of javascript code for events with .on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants