-
-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add javascript_erors.py and ondomready.py snippets (#403)
- Loading branch information
Showing
6 changed files
with
140 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Two ways for intercepting Javascript errors: | ||
1. window.onerror event in Javascript | ||
2. DisplayHandler.OnConsoleMessage in Python | ||
""" | ||
|
||
from cefpython3 import cefpython as cef | ||
|
||
g_htmlcode = """ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<style> | ||
body, html { | ||
font-family: Arial; | ||
font-size: 11pt; | ||
} | ||
</style> | ||
<script> | ||
function print(msg) { | ||
document.getElementById("console").innerHTML += msg+"<br>"; | ||
} | ||
window.onerror = function(message, source, lineno, colno, error) { | ||
print("[JS:window.onerror] "+error+" (line "+lineno+")"); | ||
// Return false so that default event handler is fired and | ||
// OnConsoleMessage can also intercept this error. | ||
return false; | ||
}; | ||
window.onload = function() { | ||
forceError(); | ||
}; | ||
</script> | ||
</head> | ||
<body> | ||
<h1>Javascript Errors</h1> | ||
<div id=console></div> | ||
</body> | ||
</html> | ||
""" | ||
|
||
|
||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters