Skip to content

Commit 2dc683e

Browse files
committed
Nested data types are now allowed in javascript bindings.
Setting window properties implemented. You can also get/set properties during runtime using Frame.SetProperty() and Frame.GetProperty().
1 parent 0a117fa commit 2dc683e

17 files changed

+212
-715
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# If you want to ignore an already commited directory run:
66
# git rm --cached -r .idea
77
/.idea/
8+
/cefexample/debug.log
9+
/cefexample/error.log
810

911
# WingIDE project files
1012
/wingide.wpr

cefexample/cefadvanced.html

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,50 @@
2323

2424
Features coming next:
2525
<ul>
26-
<li>javascript bindings
27-
<li>javascript callbacks
2826
<li>popup and modal windows
2927
<li>loading content from zip (optionally encrypted)
3028
</ul>
3129

3230
<iframe src="cefadvanced.html"></iframe>
33-
<!--<iframe src="cefsimple_onloaderror.html"></iframe>-->
31+
<iframe src="cefsimple_onloaderror.html"></iframe>
3432

3533
<script>
3634
//frames["simple"].focus()
3735
</script>
3836

3937
<h3>Javascript bindings</h3>
4038

41-
<a href="javascript:alert('Return value from PyTest1: '+PyTest1(100))">window.PyTest1(100)</a><br>
42-
<a href="javascript:alert('Return value from PyTest2: '+PyTest2(100, 'This string was passed from javascript'))">window.PyTest2(100, 'This string was passed from javascript')</a>
39+
See output in console for these tests. (Note: "window." is optional, you can just call "PyTest1()")<br><br>
40+
41+
<a href="javascript:alert('Return value from PyTest1: '+PyTest1(100))">window.PyTest1(100)</a><br>
42+
<a href="javascript:alert('Return value from PyTest2: '+JSON.stringify(PyTest2(100, 'This string was passed from javascript')))">window.PyTest2(100, 'This string was passed from javascript')</a> - python PyTest2() should return: [1,2, [2.1, {'3': 3, '4': [5,6]}]]
43+
<br><br>
44+
45+
<script>var array = [1]; array[100] = 100.01;</script>
46+
<a href="javascript:PyTest1(array)">window.PyTest1(array)</a> (var array = [1]; array[100] = 100.01;) - the gap is filled with None values.<br>
47+
48+
<script>var object = {a: 1, '3': 33};</script>
49+
<a href="javascript:PyTest1(object)">window.PyTest1(object)</a> (var object = {a: 1, '3': 33};)<br>
50+
51+
<br>
52+
<script>var nested = [1,2, [3, 4, [5,6, [{7:7, 8: [9,10]}, 11]]]];</script>
53+
<a href="javascript:PyTest1(nested)">window.PyTest1(nested)</a> (var nested = [1,2, [3, 4, [5,6, [{7:7, 8: [9,10]}, 11]]]];) - dictionary keys will be in a different order in Python.<br>
54+
55+
<br>
56+
window.PyConfig =
57+
<script>document.write(JSON.stringify(window.PyConfig));</script>
58+
<br>
59+
Now change PyConfig: <a href="javascript:window.PyConfig={'option1': false, 'option2': 40}">window.PyConfig={'option1': False, 'option2': 40}</a><br>
60+
Call __browser.GetMainFrame().GetProperty("PyConfig"): <a href="javascript:PrintPyConfig()">PrintPyConfig()</a><br>
61+
Try setting PyConfig during runtime using __browser.GetMainFrame().SetProperty(): <a href="javascript:ChangePyConfig()">ChangePyConfig()</a> (then call PrintPyConfig())
62+
63+
<br><br>
64+
Test infinite recursion: <a href="javascript:PyTest1(window)">PyTest1(window)</a> - you will see a python exception in console (also in error.log), data structures can have maximum 10 levels of nesting.
65+
4366

4467
<h3>Popups</h3>
4568

46-
<a href="javascript:window.open('cefadvanced.html', '', 'width=600,height=400')">window.open('cefadvanced.html')</a>
69+
<a href="javascript:window.open('cefadvanced.html', '', 'width=600,height=400')">window.open('cefadvanced.html')</a>
4770

4871
</body>
4972
</html>

cefexample/cefadvanced.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import win32api
1010
import sys
1111

12+
__browser = None
13+
1214
def CloseApplication(windowID, msg, wparam, lparam):
1315
browser = cefpython.GetBrowserByWindowID(windowID)
1416
browser.CloseBrowser()
@@ -49,19 +51,30 @@ def CefAdvanced():
4951
bindings = cefpython.JavascriptBindings(bindToFrames=False, bindToPopups=False)
5052
bindings.SetFunction("PyTest1", PyTest1)
5153
bindings.SetFunction("PyTest2", PyTest2)
54+
55+
bindings.SetProperty("PyConfig", {"option1": True, "option2": 20})
56+
bindings.SetFunction("PrintPyConfig", PrintPyConfig)
57+
bindings.SetFunction("ChangePyConfig", ChangePyConfig)
5258

53-
browser = cefpython.CreateBrowser(windowID, browserSettings, "cefadvanced.html", handlers, bindings)
59+
global __browser
60+
__browser = cefpython.CreateBrowser(windowID, browserSettings, "cefadvanced.html", handlers, bindings)
5461

5562
cefpython.MessageLoop()
5663
cefpython.Shutdown()
5764

5865
def PyTest1(arg1):
5966
print "PyTest1(%s) called" % arg1
60-
return 123
67+
return "This string was returned from Python function PyTest1()"
6168

6269
def PyTest2(arg1, arg2):
6370
print "PyTest2(%s, %s) called" % (arg1, arg2)
64-
return "This string was returned from Python function PyTest2()"
71+
return [1,2, [2.1, {'3': 3, '4': [5,6]}]] # testing nested return values.
72+
73+
def PrintPyConfig():
74+
print "PrintPyConfig(): %s" % __browser.GetMainFrame().GetProperty("PyConfig")
75+
76+
def ChangePyConfig():
77+
__browser.GetMainFrame().SetProperty("PyConfig", "Changed in python during runtime in ChangePyConfig()")
6578

6679
def OnLoadStart(browser, frame):
6780
print "OnLoadStart(): frame URL: %s" % frame.GetURL()

0 commit comments

Comments
 (0)