Skip to content

Commit 7448372

Browse files
committed
Add onpagecomplete.py snippet (cztomczak#403) and others.
Add tools/run_snippets.py.
1 parent 6bb2d4e commit 7448372

File tree

7 files changed

+105
-10
lines changed

7 files changed

+105
-10
lines changed

examples/README-examples.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ See small code snippets that show various CEF features in the
6161
to execute custom code before browser window closes.
6262
- [ondomready.py](snippets/ondomready.py) - Execute custom Python code
6363
on a web page as soon as DOM is ready.
64+
- [onpagecomplete.py](snippets/onpagecomplete.py) - Execute custom
65+
Python code on a web page when page loading is complete.
6466

6567

6668
### GUI frameworks

examples/snippets/ondomready.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Execute custom Python code on a web page as soon as DOM is ready.
3-
Implements a custom "_OnDomReady" event in the LifespanHandler object.
3+
Implements a custom "_OnDomReady" event in the LoadHandler object.
44
"""
55

66
from cefpython3 import cefpython as cef
@@ -10,19 +10,19 @@ def main():
1010
cef.Initialize()
1111
browser = cef.CreateBrowserSync(url="https://www.google.com/",
1212
window_title="_OnDomReady event")
13-
lifespan_handler = LifespanHandler(browser)
14-
browser.SetClientHandler(lifespan_handler)
13+
load_handler = LoadHandler(browser)
14+
browser.SetClientHandler(load_handler)
1515
bindings = cef.JavascriptBindings()
16-
bindings.SetFunction("LifespanHandler_OnDomReady",
17-
lifespan_handler["_OnDomReady"])
16+
bindings.SetFunction("LoadHandler_OnDomReady",
17+
load_handler["_OnDomReady"])
1818
browser.SetJavascriptBindings(bindings)
1919
cef.MessageLoop()
20-
del lifespan_handler
20+
del load_handler
2121
del browser
2222
cef.Shutdown()
2323

2424

25-
class LifespanHandler(object):
25+
class LoadHandler(object):
2626
def __init__(self, browser):
2727
self.browser = browser
2828

@@ -32,10 +32,10 @@ def __getitem__(self, key):
3232
def OnLoadStart(self, browser, **_):
3333
browser.ExecuteJavascript("""
3434
if (document.readyState === "complete") {
35-
LifespanHandler_OnDomReady();
35+
LoadHandler_OnDomReady();
3636
} else {
3737
document.addEventListener("DOMContentLoaded", function() {
38-
LifespanHandler_OnDomReady();
38+
LoadHandler_OnDomReady();
3939
});
4040
}
4141
""")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Execute custom Python code on a web page when page loading is complete.
3+
Implements a custom "_OnPageComplete" event in the LoadHandler object.
4+
"""
5+
6+
from cefpython3 import cefpython as cef
7+
8+
9+
def main():
10+
cef.Initialize()
11+
browser = cef.CreateBrowserSync(url="https://www.google.com/",
12+
window_title="_OnPageComplete event")
13+
browser.SetClientHandler(LoadHandler())
14+
cef.MessageLoop()
15+
del browser
16+
cef.Shutdown()
17+
18+
19+
class LoadHandler(object):
20+
def OnLoadingStateChange(self, browser, is_loading, **_):
21+
"""For detecting if page loading has ended it is recommended
22+
to use OnLoadingStateChange which is most reliable. The OnLoadEnd
23+
callback also available in LoadHandler can sometimes fail in
24+
some cases e.g. when image loading hangs."""
25+
if not is_loading:
26+
self._OnPageComplete(browser)
27+
28+
def _OnPageComplete(self, browser):
29+
print("Page loading is complete!")
30+
browser.ExecuteFunction("alert", "Message from Python: Page loading"
31+
" is complete!")
32+
33+
34+
if __name__ == '__main__':
35+
main()

src/cef_v59..v66_changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ NEW FEATURES
8585
+ javascript_bindings.py
8686
+ javascript_errors.py
8787
+ ondomready.py
88+
+ onpagecomplete
8889
+ cef.GetDataUrl
8990

9091
internal/cef_types.h

tools/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
# -- end build directories
159159

160160
EXAMPLES_DIR = os.path.join(ROOT_DIR, "examples")
161+
SNIPPETS_DIR = os.path.join(EXAMPLES_DIR, "snippets")
161162
SRC_DIR = os.path.join(ROOT_DIR, "src")
162163

163164
# Subdirectories in src/

tools/run_examples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Project website: https://github.com/cztomczak/cefpython
44

55
"""
6-
Run all examples that can be run on current configuration.
6+
Run all examples that can be run on current configuration
7+
and display a summary at the end.
78
89
Note on GTK 2 / GTK 3 on Windows:
910
Installing both PyGTK and PyGI on Windows will cause errors.

tools/run_snippets.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2018 CEF Python, see the Authors file.
2+
# All rights reserved. Licensed under BSD 3-clause license.
3+
# Project website: https://github.com/cztomczak/cefpython
4+
5+
"""
6+
Run all snippets from the examples/snippets/ directory
7+
and display a summary at the end.
8+
"""
9+
10+
from common import *
11+
12+
import glob
13+
import os
14+
import subprocess
15+
import sys
16+
17+
18+
def main():
19+
# Iterate over all snippets
20+
snippets_iter = glob.glob(os.path.join(SNIPPETS_DIR, "*.py"))
21+
succeeded = []
22+
failed = []
23+
for snippet in snippets_iter:
24+
print("[run_snippets.py] Running '{snippet}'..."
25+
.format(snippet=os.path.basename(snippet)))
26+
retcode = subprocess.call([sys.executable, snippet])
27+
if retcode == 0:
28+
succeeded.append(os.path.basename(snippet))
29+
else:
30+
print("[run_snippets.py] ERROR while running snippet: {snippet}"
31+
.format(snippet=snippet))
32+
failed.append(os.path.basename(snippet))
33+
34+
# Print summary
35+
summary = ""
36+
for snippet in succeeded:
37+
summary += " OK {snippet}{nl}"\
38+
.format(snippet=snippet, nl=os.linesep)
39+
for snippet in failed:
40+
summary += " ERROR {snippet}{nl}"\
41+
.format(snippet=snippet, nl=os.linesep)
42+
summary = summary[:-(len(os.linesep))]
43+
print("[run_snippets.py] SUMMARY:")
44+
print(summary.format())
45+
if len(failed):
46+
print("[run_snippets.py] ERRORS ({failed}) while running snippets"
47+
.format(failed=len(failed)))
48+
sys.exit(1)
49+
else:
50+
print("[run_snippets.py] OK ({succeeded})"
51+
.format(succeeded=len(succeeded)))
52+
53+
54+
if __name__ == "__main__":
55+
main()

0 commit comments

Comments
 (0)