-
Notifications
You must be signed in to change notification settings - Fork 2
/
javascript_service.py
43 lines (31 loc) · 1.17 KB
/
javascript_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import logging
from streamlit_javascript import st_javascript
from testgen.ui.services.user_session_service import AUTH_TOKEN_COOKIE_NAME
LOG = logging.getLogger("testgen")
def copy_to_clipboard(text):
script = f"""await (async function () {{
window.parent.postMessage({{ type: 'TestgenCopyToClipboard', text: '{text}' }}, '*');
return 0;
}})()
"""
execute_javascript(script)
def clear_component_states():
execute_javascript(
f"""await (async function () {{
window.parent.postMessage({{ type: 'TestgenLogout', cookie: '{AUTH_TOKEN_COOKIE_NAME}' }}, '*');
return 0;
}})()
"""
)
def execute_javascript(script):
return_value = st_javascript(script)
if return_value != 0:
LOG.warning(f"execute_javascript returned with non zero value: {return_value}, script: {script}")
def get_browser_locale_timezone():
from streamlit_javascript import st_javascript
return st_javascript(
"""await (async () => {
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return userTimezone
})().then(returnValue => returnValue)"""
)