From 6bb2d4ed7c3d498f999d6ac0c042336da1c11b18 Mon Sep 17 00:00:00 2001 From: cztomczak Date: Mon, 20 Aug 2018 12:13:43 +0200 Subject: [PATCH] Add javascript_erors.py and ondomready.py snippets (#403) --- docs/Tutorial.md | 3 +- examples/README-examples.md | 41 +++++++++------- examples/snippets/javascript_bindings.py | 3 +- examples/snippets/javascript_errors.py | 61 ++++++++++++++++++++++++ examples/snippets/ondomready.py | 50 +++++++++++++++++++ src/cef_v59..v66_changes.txt | 2 + 6 files changed, 140 insertions(+), 20 deletions(-) create mode 100644 examples/snippets/javascript_errors.py create mode 100644 examples/snippets/ondomready.py diff --git a/docs/Tutorial.md b/docs/Tutorial.md index c6c0fcf7..ea32cb42 100644 --- a/docs/Tutorial.md +++ b/docs/Tutorial.md @@ -9,7 +9,8 @@ basics. This tutorial will discuss the three featured examples: [hello_world.py](../examples/hello_world.py), [tutorial.py](../examples/tutorial.py) and [screenshot.py](../examples/screenshot.py). There are many -more examples that you can find in the [README-examples.md](../examples/README-examples.md) +more examples that you can find in the +[README-examples.md](../examples/README-examples.md) file, but these examples are out of scope for this tutorial. diff --git a/examples/README-examples.md b/examples/README-examples.md index 0f0ef9a4..4ef36045 100644 --- a/examples/README-examples.md +++ b/examples/README-examples.md @@ -4,8 +4,8 @@ Table of contents: * [Hello World!](#hello-world) * [Supported examples](#supported-examples) * [Featured](#featured) - * [GUI frameworks](#gui-frameworks) * [Snippets](#snippets) + * [GUI frameworks](#gui-frameworks) * [Build executable with PyInstaller](#build-executable-with-pyinstaller) * [Unit tests](#unit-tests) * [Other examples](#other-examples) @@ -42,8 +42,31 @@ workarounds. discussed in great details in Tutorial in the [Off-screen rendering](../docs/Tutorial.md#off-screen-rendering) section. + +### Snippets + +See small code snippets that show various CEF features in the +[examples/snippets/](snippets/) directory: + +- [javascript_bindings.py](snippets/javascript_bindings.py) - Communicate + between Python and Javascript asynchronously using + inter-process messaging with the use of Javascript Bindings. +- [javascript_errors.py](snippets/javascript_errors.py) - Two ways for + intercepting Javascript errors. +- [mouse_clicks.py](snippets/mouse_clicks.py) - Perform mouse clicks + and mouse movements programmatically. +- [network_cookies.py](snippets/network_cookies.py) - Implement + interfaces to block or allow cookies over network requests. +- [onbeforeclose.py](snippets/onbeforeclose.py) - Implement interface + to execute custom code before browser window closes. +- [ondomready.py](snippets/ondomready.py) - Execute custom Python code + on a web page as soon as DOM is ready. + + ### GUI frameworks +Examples of embedding CEF browser using various GUI frameworks: + - [gtk2.py](gtk2.py): example for [PyGTK](http://www.pygtk.org/) library (GTK 2) - [gtk3.py](gtk3.py): example for [PyGObject / PyGI](https://wiki.gnome.org/Projects/PyGObject) @@ -64,22 +87,6 @@ workarounds. toolkit. This example implements High DPI support on Windows. -### Snippets - -See small code snippets that test various features in the -[examples/snippets/](snippets/) directory: - -- [javascript_bindings.py](snippets/javascript_bindings.py) - Communicate - between Python and Javascript asynchronously using - inter-process messaging with the use of Javascript Bindings. -- [mouse_clicks.py](snippets/mouse_clicks.py) - Perform mouse clicks - and mouse movements programmatically. -- [network_cookies.py](snippets/network_cookies.py) - Implement - interfaces to block or allow cookies over network requests. -- [onbeforeclose.py](snippets/onbeforeclose.py) - Implement interface - to execute custom code before browser window closes. - - ### Build executable with PyInstaller - [PyInstaller example](pyinstaller/README-pyinstaller.md): diff --git a/examples/snippets/javascript_bindings.py b/examples/snippets/javascript_bindings.py index 7c7dccc5..63135463 100644 --- a/examples/snippets/javascript_bindings.py +++ b/examples/snippets/javascript_bindings.py @@ -17,7 +17,6 @@ + + +

Javascript Errors

+
+ + +""" + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url=cef.GetDataUrl(g_htmlcode), + window_title="Javascript Errors") + browser.SetClientHandler(DisplayHandler()) + cef.MessageLoop() + cef.Shutdown() + + +class DisplayHandler(object): + def OnConsoleMessage(self, browser, message, line, **_): + if "error" in message.lower() or "uncaught" in message.lower(): + logmsg = "[Py:OnConsoleMessage] {message} (line {line})" \ + .format(message=message, line=line) + print(logmsg) + browser.ExecuteFunction("print", logmsg) + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/ondomready.py b/examples/snippets/ondomready.py new file mode 100644 index 00000000..caeacc02 --- /dev/null +++ b/examples/snippets/ondomready.py @@ -0,0 +1,50 @@ +""" +Execute custom Python code on a web page as soon as DOM is ready. +Implements a custom "_OnDomReady" event in the LifespanHandler object. +""" + +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url="https://www.google.com/", + window_title="_OnDomReady event") + lifespan_handler = LifespanHandler(browser) + browser.SetClientHandler(lifespan_handler) + bindings = cef.JavascriptBindings() + bindings.SetFunction("LifespanHandler_OnDomReady", + lifespan_handler["_OnDomReady"]) + browser.SetJavascriptBindings(bindings) + cef.MessageLoop() + del lifespan_handler + del browser + cef.Shutdown() + + +class LifespanHandler(object): + def __init__(self, browser): + self.browser = browser + + def __getitem__(self, key): + return getattr(self, key) + + def OnLoadStart(self, browser, **_): + browser.ExecuteJavascript(""" + if (document.readyState === "complete") { + LifespanHandler_OnDomReady(); + } else { + document.addEventListener("DOMContentLoaded", function() { + LifespanHandler_OnDomReady(); + }); + } + """) + + def _OnDomReady(self): + print("DOM is ready!") + self.browser.ExecuteFunction("alert", + "Message from Python: DOM is ready!") + + +if __name__ == '__main__': + main() diff --git a/src/cef_v59..v66_changes.txt b/src/cef_v59..v66_changes.txt index f78800d3..1024bdaf 100644 --- a/src/cef_v59..v66_changes.txt +++ b/src/cef_v59..v66_changes.txt @@ -83,6 +83,8 @@ NEW FEATURES + network_cookies.py + mouse_clicks.py + javascript_bindings.py + + javascript_errors.py + + ondomready.py + cef.GetDataUrl internal/cef_types.h