def ExceptHook(excType, excValue, traceObject): import traceback, os, time, codecs # This hook does the following: in case of exception write it to # the "error.log" file, display it to the console, shutdown CEF # and exit application immediately by ignoring "finally" (os._exit()). errorMsg = "\n".join( traceback.format_exception(excType, excValue, traceObject)) errorFile = GetApplicationPath("error.log") try: appEncoding = cefpython.g_applicationSettings["string_encoding"] except: appEncoding = "utf-8" if type(errorMsg) == bytes: errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace") try: with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp: fp.write("\n[%s] %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg)) except: print("cefpython: WARNING: failed writing to error file: %s" % (errorFile)) # Convert error message to ascii before printing, otherwise # you may get error like this: # | UnicodeEncodeError: 'charmap' codec can't encode characters errorMsg = errorMsg.encode("ascii", errors="replace") errorMsg = errorMsg.decode("ascii", errors="replace") print("\n" + errorMsg + "\n") cefpython.QuitMessageLoop() cefpython.Shutdown() os._exit(1)
def jsCallback(html): browser = cefpython.GetBrowserByWindowHandle(0) metadata = browser.GetUserData("metadata") metadata['final_url'] = browser.GetUrl() browser.SetUserData("metadata", metadata) browser.SetUserData("html", html) cefpython.QuitMessageLoop()
def exceptionHook(exceptionType, exceptionValue, traceObject): errorMessage = os.linesep.join(traceback.format_exception(exceptionType, exceptionValue, traceObject)) errorFile = getApplicationPath("error.log") try: applicationEncoding = cefpython.g_applicationSettings["string_encoding"] except: applicationEncoding = "utf-8" if type(errorMessage) == bytes: errorMessage = errorMessage.decode(encoding=applicationEncoding, errors="replace") try: with codecs.open(errorFile, mode='a', encoding=applicationEncoding) as fp: fp.write((os.linesep + '[%s] %s' + os.linesep) % (time.strftime("%d-%m-%Y %H:%M:%S"), errorMessage)) except: print("cloudburst: WARNING: failed writing to error file: %s" % errorFile) # Convert error message to ascii before printing to prevent errors like this: # UnicodeEncodeError: 'charmap' codec can't encode characters errorMessage = errorMessage.encode('ascii', errors='replace') errorMessage = errorMessage.decode('ascii', errors='replace') print os.linesep + errorMessage + os.linesep cefpython.QuitMessageLoop() cefpython.Shutdown() os._exit(1)
def ExceptHook(type, value, traceObject): import traceback, os, time # This hook does the following: in case of exception display it, # write to error.log, shutdown CEF and exit application. error = "\n".join(traceback.format_exception(type, value, traceObject)) with open(GetApplicationPath("error.log"), "a") as file: file.write("\n[%s] %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), error)) print("\n" + error + "\n") cefpython.QuitMessageLoop() cefpython.Shutdown() # So that "finally" does not execute. os._exit(1)
def OnLoadEnd(self, browser, frame, httpStatusCode): if self.isLoading or self.doneEnd: return self.doneEnd = True metadata = browser.GetUserData("metadata") if httpStatusCode != 200: metadata[ 'error'] = "Failed loading page got status code %s" % httpStatusCode cefpython.QuitMessageLoop() return logging.info("Finished loading page") metadata['loaded'] = 1 metadata['content'] = 1 metadata['content_time'] = int(time()) metadata['response_code'] = httpStatusCode browser.SetUserData("metadata", metadata) frame = browser.GetMainFrame() if 'size' in self.command and self.command['size'] != "screen": frame.ExecuteJavascript(''' scrollWidth = document.body.scrollWidth; scrollHeight = document.body.scrollHeight; clientWidth = document.body.clientWidth; clientHeight = document.body.clientHeight; width = (scrollWidth > clientWidth) ? scrollWidth + 10 : clientWidth; height = (scrollHeight > clientHeight) ? scrollHeight + 10 : clientHeight; setPageSize(width, height);''') if 'script' in self.command: logging.info("Executing user JS") frame.ExecuteJavascript(self.command['script']) frame.ExecuteJavascript(''' function flashOnPage() { objects = document.querySelectorAll("object"); for (var i=0; i < objects.length; i++) { if ("classid" in objects[i].attributes || objects[i].type == "application/x-shockwave-flash") return true; } return false; }; if (flashOnPage()) { timeout = flash_delay; log("Detected flash")} else { timeout = delay; log("Didn\'t detect flash")} log("Waiting " + timeout + "s"); setTimeout(function() { jsCallback(document.documentElement.innerHTML);}, timeout * 1000);''' )
def OnLoadError(self, browser, frame, errorCode, errorText, failedURL): metadata = browser.GetUserData("metadata") metadata['error'] = errorText browser.SetUserData("metadata", metadata) cefpython.QuitMessageLoop()