FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cefpython/examples/snippets/javascript_bindings.py at cefpython66 · 2php/cefpython · GitHub
2php
cefpython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cefpython
/
examples
/
snippets
/
javascript_bindings.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
69 lines (57 loc) · 1.7 KB
Breadcrumbs
cefpython
/
examples
/
snippets
/
javascript_bindings.py
Copy path
File metadata and controls
69 lines (57 loc) · 1.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Communicate between Python and Javascript asynchronously using
inter-process messaging with the use of Javascript Bindings.
"""
from
cefpython3
import
cefpython
as
cef
g_htmlcode
=
"""
<!doctype html>
<html>
<head>
<style>
body, html {
font-family: Arial;
font-size: 11pt;
}
</style>
<script>
function print(msg) {
document.getElementById("console").innerHTML += msg+"<br>";
}
function js_function(value) {
print("Value sent from Python: <b>"+value+"</b>");
py_function("I am a Javascript string #1", js_callback);
}
function js_callback(value, py_callback) {
print("Value sent from Python: <b>"+value+"</b>");
py_callback("I am a Javascript string #2");
}
</script>
</head>
<body>
<h1>Javascript Bindings</h1>
<div id=console></div>
</body>
</html>
"""
def
main
():
cef
.
Initialize
()
browser
=
cef
.
CreateBrowserSync
(
url
=
cef
.
GetDataUrl
(
g_htmlcode
),
window_title
=
"Javascript Bindings"
)
browser
.
SetClientHandler
(
LoadHandler
())
bindings
=
cef
.
JavascriptBindings
()
bindings
.
SetFunction
(
"py_function"
,
py_function
)
bindings
.
SetFunction
(
"py_callback"
,
py_callback
)
browser
.
SetJavascriptBindings
(
bindings
)
cef
.
MessageLoop
()
del
browser
cef
.
Shutdown
()
def
py_function
(
value
,
js_callback
):
print
(
"Value sent from Javascript: "
+
value
)
js_callback
.
Call
(
"I am a Python string #2"
,
py_callback
)
def
py_callback
(
value
):
print
(
"Value sent from Javascript: "
+
value
)
class
LoadHandler
(
object
):
def
OnLoadEnd
(
self
,
browser
,
**
_
):
browser
.
ExecuteFunction
(
"js_function"
,
"I am a Python string #1"
)
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL