Skip to content

Commit 35edce7

Browse files
committed
Added ExecuteFunction and ExecuteJavascript methods also to the Browser
object. Browser.ExecuteFunction is the same as Browser.GetMainFrame().ExecuteFunction.
1 parent 78d5f83 commit 35edce7

File tree

2 files changed

+34
-57
lines changed

2 files changed

+34
-57
lines changed

cefpython/browser.pyx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
IF CEF_VERSION == 1:
66
# In CEF 1 there are both KT_KEYDOWN and KEYEVENT_KEYDOWN, and
7-
# these are different constants, making a bit of confusion.
7+
# these are different constants, making a bit of confusion.
88
# In CEF 1 KT_ is for SendKeyEvent, KEYEVENT_ is for OnKeyEvent().
99
# In CEF 3 there are only KEYEVENT_* constants.
1010
KEYTYPE_KEYDOWN = cef_types.KT_KEYDOWN
@@ -179,7 +179,7 @@ cdef class PyBrowser:
179179
# -------------------------------------------------------------------------
180180
# CEF 1
181181
# -------------------------------------------------------------------------
182-
cpdef py_void SetClientCallback_CEF1(self,
182+
cpdef py_void SetClientCallback_CEF1(self,
183183
py_string name, object callback):
184184
if not self.allowedClientCallbacks:
185185
# CefLoadHandler.
@@ -196,7 +196,7 @@ cdef class PyBrowser:
196196
"OnResourceResponse", "OnProtocolExecution",
197197
"GetDownloadHandler", "GetAuthCredentials",
198198
"GetCookieManager"]
199-
199+
200200
# CefDisplayHandler.
201201
self.allowedClientCallbacks += ["OnAddressChange",
202202
"OnConsoleMessage", "OnContentsSizeChange",
@@ -219,7 +219,7 @@ cdef class PyBrowser:
219219
# -------------------------------------------------------------------------
220220
# CEF 3
221221
# -------------------------------------------------------------------------
222-
cpdef py_void SetClientCallback_CEF3(self,
222+
cpdef py_void SetClientCallback_CEF3(self,
223223
py_string name, object callback):
224224
if not self.allowedClientCallbacks:
225225
# DisplayHandler
@@ -229,19 +229,19 @@ cdef class PyBrowser:
229229
# KeyboardHandler
230230
self.allowedClientCallbacks += ["OnPreKeyEvent", "OnKeyEvent"];
231231
# RequestHandler
232-
# NOTE: OnCertificateError and OnBeforePluginLoad are not
233-
# included as they must be set using
232+
# NOTE: OnCertificateError and OnBeforePluginLoad are not
233+
# included as they must be set using
234234
# cefpython.SetGlobalClientCallback().
235235
self.allowedClientCallbacks += ["OnBeforeResourceLoad",
236236
"OnResourceRedirect", "GetAuthCredentials",
237-
"OnQuotaRequest", "OnProtocolExecution",
237+
"OnQuotaRequest", "OnProtocolExecution",
238238
"GetResourceHandler",
239239
"OnBeforeBrowse", "OnRendererProcessTerminated",
240240
"OnPluginCrashed"]
241241
# RequestContextHandler
242242
self.allowedClientCallbacks += ["GetCookieManager"]
243243
# LoadHandler
244-
self.allowedClientCallbacks += ["OnLoadingStateChange",
244+
self.allowedClientCallbacks += ["OnLoadingStateChange",
245245
"OnLoadStart", "OnLoadEnd", "OnLoadError"]
246246
# LifespanHandler
247247
# NOTE: OnAfterCreated not included as it must be set using
@@ -335,18 +335,25 @@ cdef class PyBrowser:
335335
if len(g_pyBrowsers) == 1:
336336
# This is the last browser remaining.
337337
if g_sharedRequestContext.get():
338-
# A similar release is done in Shutdown
338+
# A similar release is done in Shutdown
339339
# and RemovePyBrowser.
340340
Debug("CloseBrowser: releasing shared request context")
341341
g_sharedRequestContext.Assign(NULL)
342342
Debug("CefBrowser::CloseBrowser(%s)" % forceClose)
343343
self.GetCefBrowserHost().get().CloseBrowser(bool(forceClose))
344-
344+
345345
IF CEF_VERSION == 1:
346346

347347
cpdef py_void CloseDevTools(self):
348348
self.GetCefBrowser().get().CloseDevTools()
349349

350+
def ExecuteFunction(self, *args):
351+
self.GetMainFrame().ExecuteFunction(*args)
352+
353+
cpdef py_void ExecuteJavascript(self, py_string jsCode,
354+
py_string scriptUrl="", int startLine=0):
355+
self.GetMainFrame().ExecuteJavascript(jsCode, scriptUrl, startLine)
356+
350357
cpdef py_void Find(self, int searchId, py_string searchText,
351358
py_bool forward, py_bool matchCase,
352359
py_bool findNext):
@@ -708,7 +715,7 @@ cdef class PyBrowser:
708715

709716
cpdef py_void SendCaptureLostEvent(self):
710717
self.GetCefBrowser().get().SendCaptureLostEvent()
711-
718+
712719
ELIF CEF_VERSION == 3:
713720
cpdef py_void SendKeyEvent(self, dict pyEvent):
714721
cdef CefKeyEvent cefEvent
@@ -731,7 +738,7 @@ cdef class PyBrowser:
731738
cefEvent.focus_on_editable_field = \
732739
bool(pyEvent["focus_on_editable_field"])
733740
self.GetCefBrowserHost().get().SendKeyEvent(cefEvent)
734-
741+
735742
cpdef py_void SendMouseClickEvent(self, x, y,
736743
cef_types.cef_mouse_button_type_t mouseButtonType,
737744
py_bool mouseUp, int clickCount):
@@ -779,7 +786,7 @@ cdef class PyBrowser:
779786
mouseEvent.modifiers = 0
780787
self.GetCefBrowserHost().get().SendMouseWheelEvent(mouseEvent,
781788
deltaX, deltaY)
782-
789+
783790
cpdef py_void SendFocusEvent(self, py_bool setFocus):
784791
self.GetCefBrowserHost().get().SendFocusEvent(bool(setFocus))
785792

@@ -812,7 +819,7 @@ cdef class PyBrowser:
812819
# virtual void HandleKeyEventBeforeTextInputClient(CefEventHandle keyEvent) =0;
813820
# virtual void HandleKeyEventAfterTextInputClient(CefEventHandle keyEvent) =0;
814821

815-
cdef void SendProcessMessage(self, cef_process_id_t targetProcess,
822+
cdef void SendProcessMessage(self, cef_process_id_t targetProcess,
816823
object frameId, py_string messageName, list pyArguments
817824
) except *:
818825
cdef CefRefPtr[CefProcessMessage] message = \
@@ -826,7 +833,7 @@ cdef class PyBrowser:
826833
PyListToExistingCefListValue(self.GetIdentifier(), frameId,
827834
pyArguments, messageArguments)
828835
Debug("SendProcessMessage(): message=%s, arguments size=%d" % (
829-
messageName,
836+
messageName,
830837
message.get().GetArgumentList().get().GetSize()))
831838
cdef cpp_bool success = \
832839
self.GetCefBrowser().get().SendProcessMessage(

cefpython/frame.pyx

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -84,48 +84,10 @@ cdef class PyFrame:
8484
return True
8585
return False
8686

87-
def ExecuteFunction(self, *args):
88-
# No need to enter V8 context as we're calling javascript
89-
# asynchronously using ExecuteJavascript() function.
90-
funcName = args[0]
91-
code = funcName+"("
92-
for i in range(1, len(args)):
93-
if i != 1:
94-
code += ", "
95-
code += json.dumps(args[i])
96-
code += ")"
97-
self.ExecuteJavascript(code)
98-
9987
def CallFunction(self, *args):
10088
# DEPRECATED - keep for backwards compatibility.
10189
self.ExecuteFunction(*args)
10290

103-
# Synchronous javascript calls won't be supported as CEF 3
104-
# does not support it, so supporting it in CEF 1 is probably
105-
# not a good idea - porting to CEF 3 would be problematic.
106-
107-
# cpdef object CallFunctionSync(self, funcName, ...):
108-
# CefV8 Objects, Arrays and Functions can be created only inside V8 context,
109-
# you need to call CefV8Context::Enter() and CefV8Context::Exit():
110-
# http://code.google.com/p/chromiumembedded/issues/detail?id=203
111-
# Entering context should be done for Frame::CallFunction().
112-
# You must check current context and Enter it if not same, before calling
113-
# PyToV8Value().
114-
# TODO: call Frame->GetV8Context?()->GetGlobal?() you get a window object,
115-
# now iterate through its all properties and compare to funcName, you get a
116-
# real javascript object which you can call and be able to get return value, also
117-
# you can pass python callbacks this way.'''
118-
# Example:
119-
# cdef CefRefPtr[CefV8Context] v8Context = (
120-
# self.GetCefFrame().get().GetV8Context())
121-
# sameContext = v8Context.get().IsSame(cef_v8_static.GetCurrentContext())
122-
# if not sameContext:
123-
# assert v8Context.get().Enter(), "v8Context.Enter() failed"
124-
# ... execute function here ...
125-
# if not sameContext:
126-
# assert v8Context.get().Exit(), "v8Context.Exit() failed"
127-
# pass
128-
12991
cpdef py_void Copy(self):
13092
self.GetCefFrame().get().Copy()
13193

@@ -135,15 +97,23 @@ cdef class PyFrame:
13597
cpdef py_void Delete(self):
13698
self.GetCefFrame().get().Delete()
13799

100+
def ExecuteFunction(self, *args):
101+
# No need to enter V8 context as we're calling javascript
102+
# asynchronously using ExecuteJavascript() function.
103+
funcName = args[0]
104+
code = funcName+"("
105+
for i in range(1, len(args)):
106+
if i != 1:
107+
code += ", "
108+
code += json.dumps(args[i])
109+
code += ")"
110+
self.ExecuteJavascript(code)
111+
138112
cpdef py_void ExecuteJavascript(self, py_string jsCode,
139113
py_string scriptUrl="", int startLine=0):
140114
self.GetCefFrame().get().ExecuteJavaScript(PyToCefStringValue(jsCode),
141115
PyToCefStringValue(scriptUrl), startLine)
142116

143-
cpdef object EvalJavascript(self):
144-
# TODO: CefV8Context > Eval
145-
pass
146-
147117
cpdef object GetIdentifier(self):
148118
# It is better to save browser and frame identifiers during
149119
# browser instantiation. When freeing PyBrowser and PyFrame

0 commit comments

Comments
 (0)