forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathonpagecomplete.py
More file actions
54 lines (44 loc) · 1.58 KB
/
Copy pathonpagecomplete.py
File metadata and controls
54 lines (44 loc) · 1.58 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
"""
Execute custom Python code on a web page when all visible content is loaded.
Implements a custom "_OnPageComplete" event that fires after window.load and
a browser paint frame, ensuring content is fully rendered before notifying.
"""
from cefpython3 import cefpython as cef
def main():
cef.Initialize()
browser = cef.CreateBrowserSync(url="https://www.google.com/",
window_title="_OnPageComplete event")
handler = PageCompleteHandler(browser)
browser.SetClientHandler(handler)
bindings = cef.JavascriptBindings()
bindings.SetFunction("LoadHandler_OnPageComplete",
handler["_OnPageComplete"])
browser.SetJavascriptBindings(bindings)
cef.MessageLoop()
del handler
del browser
cef.Shutdown()
class PageCompleteHandler(object):
def __init__(self, browser):
self.browser = browser
def __getitem__(self, key):
return getattr(self, key)
def OnContextCreated(self, browser, frame, **_):
if not frame.IsMain():
return
browser.ExecuteJavascript("""
window.addEventListener("load", function() {
requestAnimationFrame(function() {
LoadHandler_OnPageComplete();
});
});
""")
def _OnPageComplete(self):
print("Page loading is complete!")
self.browser.ExecuteJavascript(
'setTimeout(function(){'
' alert("Message from Python: Page loading is complete!");'
' }, 0);'
)
if __name__ == '__main__':
main()