def evaluate(script): result = self.webkit.windowScriptObject().evaluateWebScript_( 'JSON.stringify(eval("{0}"))'.format(_escape_string(script))) JSResult.result = None if result is WebKit.WebUndefined.undefined( ) or result == 'null' else json.loads(result) JSResult.result_semaphore.release()
def evaluate_js(self, script): def _evaluate_js(): self.webview.execute_script(code) self._js_result_semaphore.release() unique_id = uuid1().hex # Backup the doc title and store the result in it with a custom prefix code = 'oldTitle{0} = document.title; document.title = eval("{1}");'.format( unique_id, _escape_string(script)) self.load_event.wait() glib.idle_add(_evaluate_js) self._js_result_semaphore.acquire() if not gtk.main_level(): # Webview has been closed, don't proceed return None # Restore document title and return _js_result = self._parse_js_result(self.webview.get_title()) code = 'document.title = oldTitle{0};'.format(unique_id) glib.idle_add(self.webview.execute_script, code) return _js_result
def evaluate_js(self, script): def _evaluate_js(): self.webview.execute_script(code) result_semaphore.release() self.eval_js_lock.acquire() result_semaphore = Semaphore(0) self.js_result_semaphores.append(result_semaphore) # Backup the doc title and store the result in it with a custom prefix unique_id = uuid1().hex code = 'window.oldTitle{0} = document.title; document.title = JSON.stringify(eval("{1}"));'.format( unique_id, _escape_string(script)) self.load_event.wait() glib.idle_add(_evaluate_js) result_semaphore.acquire() if not gtk.main_level(): # Webview has been closed, don't proceed return None result = self.webview.get_title() result = None if result == 'undefined' or result == 'null' else result if result == '' else json.loads( result) # Restore document title and return code = 'document.title = window.oldTitle{0}'.format(unique_id) glib.idle_add(_evaluate_js) self.js_result_semaphores.remove(result_semaphore) self.eval_js_lock.release() return result
def _evaluate_js(): document = window.web_browser.Document result = document.InvokeScript( 'eval', ('JSON.stringify(eval("{0}"))'.format(_escape_string(script)), )) window.js_result = None if result is None or result is 'null' else json.loads( result) window.js_result_semaphore.release()
def _evaluate_js(): document = self.browser.web_browser.Document script_element = document.CreateElement('script') function_name = 'invoke' + uuid1().hex code = 'function {0}() {{return eval("{1}")}}'.format( function_name, _escape_string(script)) script_element.InnerText = code document.Body.AppendChild(script_element) self._js_result = document.InvokeScript(function_name) self._js_result_semaphor.release()
def on_status_change(self, webview, status): try: delim = '_' + self.js_bridge.uid + '_' except AttributeError: return # Check if status was updated by a JSBridge call if status.startswith(delim): _, func_name, param = status.split(delim) return_val = self.js_bridge.call(func_name, param) # Give back the return value to JS as a string code = 'pywebview._bridge.return_val = "{0}";'.format(_escape_string(str(return_val))) webview.execute_script(code)
def evaluate_js(self, script): def _evaluate_js(): self.webview.execute_script(code) self._js_result_semaphore.release() unique_id = uuid1().hex # Backup the doc title and store the result in it with a custom prefix code = 'oldTitle{0} = document.title; document.title = eval("{1}");'.format( unique_id, _escape_string(script)) glib.idle_add(_evaluate_js) self._js_result_semaphore.acquire() # Restore document title and return _js_result = self.webview.get_title() code = 'document.title = oldTitle{0};'.format(unique_id) glib.idle_add(self.webview.execute_script, code) return _js_result
def on_evaluate_js(self, script, uuid): def return_result(result): result = BrowserView._convert_string(result) uuid_ = BrowserView._convert_string(uuid) js_result = self._js_results[uuid_] js_result[ 'result'] = None if result is None or result == 'null' else result if result == '' else json.loads( result) js_result['semaphore'].release() escaped_script = 'JSON.stringify(eval("{0}"))'.format( _escape_string(script)) try: # PyQt4 result = self.view.page().mainFrame().evaluateJavaScript( escaped_script) return_result(result) except AttributeError: # PyQt5 self.view.page().runJavaScript(escaped_script, return_result)