-
-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathjavascript_errors.py
61 lines (53 loc) · 1.54 KB
/
javascript_errors.py
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
55
56
57
58
59
60
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()