Repository navigation
Expand file tree
/
Copy pathtutorial.py
More file actions
199 lines (168 loc) · 7.41 KB
/
Copy pathtutorial.py
File metadata and controls
199 lines (168 loc) · 7.41 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Tutorial example. Doesn't depend on any third party GUI framework.
# Tested with CEF Python v57.0+
from cefpython3 import cefpython as cef
import base64
import platform
import sys
# HTML code. Browser will navigate to a Data uri created
# from this html code.
HTML_code = """
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body,html { font-family: Arial; font-size: 11pt; }
div.msg { margin: 0.2em; line-height: 1.5em; }
b { font-weight: bold; padding: 0.2em 0.3em; padding-bottom: 0.25em; }
b.Python { background: #174a8b; color: white; }
b.Javascript { background: #175c2e; color: white; }
b.event { }
i { padding: 0.1em 0.2em; background: #8f1d1d; color: white; font-weight: bold; }
</style>
</head>
<body>
<h1>Tutorial example</h1>
<div id="terminal"></div>
<script>
function js_print(lang, event, msg) {
if (event != "OnConsoleMessage") {
console.log(lang+": "+event+": "+msg);
}
event = event.replace(/</g, "<").replace(/>/g, ">");
msg = "<b class="+lang+">"+lang+":</b> <b class=event>"+event+":</b> "+msg;
var terminal = document.getElementById("terminal");
terminal.innerHTML += "<div class=msg>"+msg+"</div>";
}
js_print("Javascript", "<script>", "Executing");
function js_callback_1(ret) {
js_print("Javascript", "html_to_data_uri", ret);
}
function js_callback_2(msg, py_callback) {
js_print("Javascript", "js_callback", msg);
py_callback("String sent from Javascript");
}
function run_html_tests() {
js_print("Javascript", "run_html_tests", "Executing");
js_print("Javascript", "python_property", python_property);
js_print("Javascript", "navigator.userAgent", navigator.userAgent);
js_print("Javascript", "cefpython_version", cefpython_version.version);
html_to_data_uri("test", js_callback_1);
external.test_multiple_callbacks(js_callback_2);
setTimeout(function(){ throw_javascript_error_here(); }, 500);
}
</script>
</body>
</html>
"""
def main():
check_versions()
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
# To change the user agent use the "user_agent" option.
settings = {
# "user_agent": "MyAgent/20.00 MyProduct/10.00",
}
cef.Initialize(settings=settings)
set_global_handler()
browser = cef.CreateBrowserSync(url=html_to_data_uri(HTML_code),
window_title="Tutorial")
set_client_handlers(browser)
set_javascript_bindings(browser)
cef.MessageLoop()
cef.Shutdown()
def check_versions():
ver = cef.GetVersion()
print("[tutorial.py] CEF Python {ver}".format(ver=ver["version"]))
print("[tutorial.py] Chromium {ver}".format(ver=ver["chrome_version"]))
print("[tutorial.py] CEF {ver}".format(ver=ver["cef_version"]))
print("[tutorial.py] Python {ver} {arch}".format(
ver=platform.python_version(),
arch=platform.architecture()[0]))
assert int(cef.__version__.split(".")[0]) >= 57, "CEF Python v57.0+ required to run this"
def html_to_data_uri(html, js_callback=None):
# This function is called in two ways:
# 1. From Python: in this case value is returned
# 2. From Javascript: in this case value cannot be returned because
# inter-process messaging is asynchronous, so must return value
# by calling js_callback.
html = html.encode("utf-8", "replace")
b64 = base64.b64encode(html).decode("utf-8", "replace")
ret = "data:text/html;base64,{data}".format(data=b64)
if js_callback:
js_print(js_callback.GetFrame().GetBrowser(),
"Python", "html_to_data_uri",
"Called from Javascript. Will call Javascript callback now.")
js_callback.Call(ret)
else:
return ret
def set_global_handler():
# A global handler is a special handler for callbacks that
# must be set before Browser is created using
# SetGlobalClientCallback() method.
global_handler = GlobalHandler()
cef.SetGlobalClientCallback("OnAfterCreated",
global_handler.OnAfterCreated)
def set_client_handlers(browser):
client_handlers = [LoadHandler(), DisplayHandler()]
for handler in client_handlers:
browser.SetClientHandler(handler)
def set_javascript_bindings(browser):
js_print(browser, "Python", "set_javascript_bindings", "Setting javascript bindings at this moment")
external = External(browser)
bindings = cef.JavascriptBindings(
bindToFrames=False, bindToPopups=False)
bindings.SetProperty("python_property", "This property was set in Python")
bindings.SetProperty("cefpython_version", cef.GetVersion())
bindings.SetFunction("html_to_data_uri", html_to_data_uri)
bindings.SetObject("external", external)
browser.SetJavascriptBindings(bindings)
def js_print(browser, lang, event, msg):
# Execute Javascript function "js_print"
browser.ExecuteFunction("js_print", lang, event, msg)
class GlobalHandler(object):
def OnAfterCreated(self, browser, **_):
"""Called after a new browser is created."""
# Delay execution until the page's Javascript context is ready
args = [browser, "Python", "OnAfterCreated",
"(Delayed) Browser id="+str(browser.GetIdentifier())]
cef.PostDelayedTask(cef.TID_UI, 500, js_print, *args)
class LoadHandler(object):
def OnLoadingStateChange(self, browser, is_loading, **_):
"""Called when the loading state has changed."""
if not is_loading:
# Loading is complete. DOM is ready.
js_print(browser, "Python", "OnLoadingStateChange",
"Loading is complete, will execute run_html_tests")
browser.ExecuteFunction("run_html_tests")
class DisplayHandler(object):
def OnConsoleMessage(self, browser, message, **_):
"""Called to display a console message."""
# This will intercept js errors, e.g. the intentional error
# thrown by run_html_tests() in the page.
if "error" in message.lower() or "uncaught" in message.lower():
# Prevent infinite recurrence in case something went wrong
if "js_print is not defined" in message.lower():
if hasattr(self, "js_print_is_not_defined"):
print("Javascript error: " + message)
return
else:
self.js_print_is_not_defined = True
# Delay execution until the page's Javascript context is ready
args = [browser, "Python", "OnConsoleMessage",
"(Delayed) Intercepted Javascript error: <i>{error}</i>"
.format(error=message)]
cef.PostDelayedTask(cef.TID_UI, 500, js_print, *args)
else:
print("Javascript console: " + message)
class External(object):
def __init__(self, browser):
self.browser = browser
def test_multiple_callbacks(self, js_callback):
"""Test both javascript and python callbacks."""
print('in test_multiple_callbacks')
js_print(self.browser, "Python", "test_multiple_callbacks",
"Called from Javascript. Will call Javascript callback now.")
def py_callback(msg_from_js):
js_print(self.browser, "Python", "py_callback", msg_from_js)
js_callback.Call("String sent from Python", py_callback)
if __name__ == '__main__':
main()