How to determine whether the current connection is secure #4236
-
QuestionHi erveyone. I use the function I tried to modify this part of the source code https://github.com/zauberzeug/nicegui/blob/main/nicegui/functions/clipboard.py#L15-L30 : async def read() -> str | None:
"""Read text from the clipboard.
Note: This function only works in secure contexts (HTTPS or localhost).
"""
result = await run_javascript('''
if (navigator.clipboard) {
return navigator.clipboard.readText()
}
else {
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).')
}
''')
if result is None:
log.warning('Clipboard API is only available in secure contexts (HTTPS or localhost).')
return result When result is None,return None instead of '' to distinguish the above two situations.When clipboard is empty,we will get '';When user not in Https or loaclhost,we will get None. However,This means I need to modify the package before use pyinstaller build my exe. Is there a better way to handle this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
By the way,this is my code to show specific notifications to users. async def on_cb_click():
data = await ui.clipboard.read()
print(data)
if type(data) == str and data:
own_input.set_value(data)
ui.notify(
"Succeed: ", position="bottom-right", type="positive"
)
elif data is None: # Unluckly we couldn't get None now.
ui.notify(
"Failed: Not in Https or localhost",
position="bottom-right",
type="warning",
)
else:
ui.notify(
"Failed: Cilpboard is empty",
position="bottom-right",
type="warning",
) |
Beta Was this translation helpful? Give feedback.
-
When I use |
Beta Was this translation helpful? Give feedback.
-
Good question, @weinibuliu! Maybe it would be better to raise an exception instead of printing a warning and returning an empty string. But because it would break the API, we would need to wait for version 3.0 before changing it. Your custom To check whether the clipboard is available, you can use a function like this: async def can_access_clipboard() -> bool:
return await ui.run_javascript('navigator.clipboard') is not None |
Beta Was this translation helpful? Give feedback.
Good question, @weinibuliu! Maybe it would be better to raise an exception instead of printing a warning and returning an empty string. But because it would break the API, we would need to wait for version 3.0 before changing it.
Your custom
read()
function seems like a good workaround. You can use it without modifying the library. This way you don't have to wait for 3.0.To check whether the clipboard is available, you can use a function like this: