def _set_js_api(self): def _register_window_object(): frame.addToJavaScriptWindowObject('external', self.js_bridge) script = parse_api_js(self.js_bridge.api) if _qt_version >= [5, 5]: qwebchannel_js = QtCore.QFile('://qtwebchannel/qwebchannel.js') if qwebchannel_js.open(QtCore.QFile.ReadOnly): source = bytes(qwebchannel_js.readAll()).decode('utf-8') self.view.page().runJavaScript(source) self.channel.registerObject('external', self.js_bridge) qwebchannel_js.close() elif _qt_version >= [5, 0]: frame = self.view.page().mainFrame() _register_window_object() else: frame = self.view.page().mainFrame() _register_window_object() try: # PyQt4 self.view.page().mainFrame().evaluateJavaScript(script) except AttributeError: # PyQt5 self.view.page().runJavaScript(script) self.load_event.set()
def on_document_completed(self, sender, args): document = self.web_browser.Document document.InvokeScript('eval', (alert.src, )) if _debug: document.InvokeScript('eval', ( 'window.console = { log: function(msg) { window.external.console(JSON.stringify(msg)) }}', )) if self.first_load: self.web_browser.Visible = True self.first_load = False self.url = None if args.Url.AbsoluteUri == 'about:blank' else str( args.Url.AbsoluteUri) document.InvokeScript( 'eval', (parse_api_js(self.pywebview_window, 'mshtml'), )) if not self.pywebview_window.text_select: document.InvokeScript('eval', (disable_text_select, )) self.pywebview_window.loaded.set() if self.pywebview_window.easy_drag: document.MouseMove += self.on_mouse_move
def on_navigation_completed(self, _, args): try: if self.tmpdir and os.path.exists(self.tmpdir): shutil.rmtree(self.tmpdir) self.tmpdir = None except Exception as e: logger.exception('Failed deleting %s' % self.tmpdir) url = str(args.Uri) self.url = None if self.ishtml else url self.web_view.InvokeScript('eval', ( 'window.alert = (msg) => window.external.notify(JSON.stringify(["alert", msg+""]))', )) if _debug: self.web_view.InvokeScript('eval', ( 'window.console = { log: (msg) => window.external.notify(JSON.stringify(["console", msg+""]))}', )) self.web_view.InvokeScript( 'eval', (parse_api_js(self.pywebview_window.js_api, 'edgehtml'), )) if not self.pywebview_window.text_select: self.web_view.InvokeScript('eval', (disable_text_select, )) self.pywebview_window.loaded.set()
def on_navigation_completed(self, sender, args): try: if self.tmpdir and os.path.exists(self.tmpdir): shutil.rmtree(self.tmpdir) self.tmpdir = None except Exception as e: logger.exception('Failed deleting %s' % self.tmpdir) url = str(sender.Source) self.url = None if self.ishtml else url self.web_view.ExecuteScriptAsync( 'window.alert = (msg) => window.chrome.webview.postMessage(["alert", msg+"", ""])' ) if _debug: self.web_view.ExecuteScriptAsync( 'window.console = { log: (msg) => window.chrome.webview.postMessage(["console", msg+"", ""])}' ) self.web_view.ExecuteScriptAsync( parse_api_js(self.pywebview_window, 'chromium')) if not self.pywebview_window.text_select: self.web_view.ExecuteScriptAsync(disable_text_select) self.pywebview_window.loaded.set()
def on_download_complete(self, sender, args): document = self.web_browser.Document if self.js_bridge.api: document.InvokeScript('eval', (parse_api_js(self.js_bridge.api),)) if not self.text_select: document.InvokeScript('eval', (disable_text_select,))
def on_download_complete(self, sender, args): document = self.web_browser.Document if self.js_bridge.api: document.InvokeScript('eval', (parse_api_js(self.js_bridge.api),)) if not self.text_select: document.InvokeScript('eval', (disable_text_select,))
def on_navigation_completed(self, sender, args): url = str(sender.Source) self.url = None if self.ishtml else url self.web_view.ExecuteScriptAsync( parse_api_js(self.pywebview_window, 'chromium')) if not self.pywebview_window.text_select: self.web_view.ExecuteScriptAsync(disable_text_select) self.pywebview_window.loaded.set()
def initialize(self): if self.initialized: return if self.api: self.browser.ExecuteJavascript(parse_api_js(self.api)) if not self.text_select: self.browser.ExecuteJavascript(disable_text_select) self.initialized = True self.loaded.set()
def initialize(self): if self.initialized: return self.browser.GetJavascriptBindings().Rebind() self.browser.ExecuteJavascript(parse_api_js(self.js_api, 'cef')) if not self.text_select: self.browser.ExecuteJavascript(disable_text_select) self.browser.ExecuteJavascript(dom.src) self.initialized = True self.loaded.set()
def on_navigation_completed(self, sender, args): url = str(sender.Source) self.url = None if self.ishtml else url self.web_view.ExecuteScriptAsync('window.alert = (msg) => window.chrome.webview.postMessage(["alert", msg+"", ""])') if _debug['mode']: self.web_view.ExecuteScriptAsync('window.console = { log: (msg) => window.chrome.webview.postMessage(["console", msg+"", ""])}') self.web_view.ExecuteScriptAsync(parse_api_js(self.pywebview_window, 'chromium')) if not self.pywebview_window.text_select: self.web_view.ExecuteScriptAsync(disable_text_select) self.pywebview_window.loaded.set()
def initialize(self): if self.initialized: return self.browser.GetJavascriptBindings().Rebind() if self.api: self.browser.ExecuteJavascript(parse_api_js(self.api)) if not self.text_select: self.browser.ExecuteJavascript(disable_text_select) self.initialized = True self.loaded.set()
def create_bridge(): # Make the `call` method write the function name and param to the # `status` attribute of the JS window, delimited by a unique token. # The return value will be passed back to the `return_val` attribute # of the bridge by the on_status_change handler. code = """ window.pywebview._bridge.call = function(funcName, param) {{ document.title = JSON.stringify({{"type": "invoke", "uid": "{0}", "function": funcName, "param": param}}) return this.return_val; }};""".format(self.js_bridge.uid) # Create the `pywebview` JS api object self.webview.run_javascript(parse_api_js(self.js_bridge.api)) self.webview.run_javascript(code) self.load_event.set()
def create_bridge(): # Make the `call` method write the function name and param to the # `status` attribute of the JS window, delimited by a unique token. # The return value will be passed back to the `return_val` attribute # of the bridge by the on_status_change handler. code = """ window.pywebview._bridge.call = function(funcName, param) {{ window.status = "_{0}_" + funcName + "_{0}_" + param; return this.return_val; }};""".format(self.js_bridge.uid) # Create the `pywebview` JS api object self.webview.execute_script(parse_api_js(self.js_bridge.api)) self.webview.execute_script(code) self.load_event.set()
def initialize(self): if self.initialized: return self.browser.GetJavascriptBindings().Rebind() self.browser.ExecuteJavascript(parse_api_js(self.window, 'cef')) if not self.text_select: self.browser.ExecuteJavascript(disable_text_select) self.browser.ExecuteJavascript(dom.src) sleep(0.1) # wait for window.pywebview to load self.initialized = True self.loaded.set()
def create_bridge(): # Make the `call` method write the function name and param to the # `status` attribute of the JS window, delimited by a unique token. # The return value will be passed back to the `return_val` attribute # of the bridge by the on_status_change handler. code = """ window.pywebview._bridge.call = function(funcName, param) {{ document.title = JSON.stringify({{"type": "invoke", "uid": "{0}", "function": funcName, "param": param}}) return this.return_val; }};""".format(self.js_bridge.uid) # Create the `pywebview` JS api object self.webview.run_javascript(parse_api_js(self.js_bridge.api)) self.webview.run_javascript(code) self.load_event.set()
def create_bridge(): self.webview.run_javascript(parse_api_js(self.js_bridge.window.js_api, 'gtk')) if self.js_bridge.window.js_api: # Make the `call` method write the function name and param to the # window title. # The return value will be passed back to the `return_val` attribute # of the bridge by the on_title_change handler. code = """ window.pywebview._bridge.call = function(funcName, param, id) {{ document.title = JSON.stringify({{"type": "invoke", "uid": "{0}", "function": funcName, "param": param, "id": id}}) return this.return_val; }};""".format(self.js_bridge.uid) self.webview.run_javascript(code) self.loaded.set()
def on_document_completed(self, sender, args): self._initialize_js() if self.first_load: self.web_browser.Visible = True self.first_load = False document = self.web_browser.Document if self.js_bridge.api: document.InvokeScript('eval', (parse_api_js(self.js_bridge.api), )) if not self.text_select: document.InvokeScript('eval', (disable_text_select, )) self.load_event.set()
def webView_didFinishNavigation_(self, webview, nav): # Add the webview to the window if it's not yet the contentView i = BrowserView.get_instance('webkit', webview) if i: if not webview.window(): i.window.setContentView_(webview) i.window.makeFirstResponder_(webview) script = parse_api_js(i.js_bridge.window, 'cocoa') i.webkit.evaluateJavaScript_completionHandler_(script, lambda a,b: None) if not i.text_select: i.webkit.evaluateJavaScript_completionHandler_(disable_text_select, lambda a,b: None) print_hook = 'window.print = function() { window.webkit.messageHandlers.browserDelegate.postMessage("print") };' i.webkit.evaluateJavaScript_completionHandler_(print_hook, lambda a,b: None) i.loaded.set()
def webView_didFinishNavigation_(self, webview, nav): # Add the webview to the window if it's not yet the contentView i = BrowserView.get_instance('webkit', webview) if i: if not webview.window(): i.window.setContentView_(webview) i.window.makeFirstResponder_(webview) if i.js_bridge: script = parse_api_js(i.js_bridge.api) i.webkit.evaluateJavaScript_completionHandler_(script, lambda a,b: None) if not i.text_select: i.webkit.evaluateJavaScript_completionHandler_(disable_text_select, lambda a,b: None) print_hook = 'window.print = function() { window.webkit.messageHandlers.browserDelegate.postMessage("print") };' i.webkit.evaluateJavaScript_completionHandler_(print_hook, lambda a,b: None) i.loaded.set()
def _set_js_api(self): def _register_window_object(): frame.addToJavaScriptWindowObject('external', self.js_bridge) script = parse_api_js(self.js_bridge.api) if is_webengine: qwebchannel_js = QtCore.QFile('://qtwebchannel/qwebchannel.js') if qwebchannel_js.open(QtCore.QFile.ReadOnly): source = bytes(qwebchannel_js.readAll()).decode('utf-8') self.view.page().runJavaScript(source) self.channel.registerObject('external', self.js_bridge) qwebchannel_js.close() else: frame = self.view.page().mainFrame() _register_window_object() try: # < QT 5.6 self.view.page().mainFrame().evaluateJavaScript(script) except AttributeError: self.view.page().runJavaScript(script) self.load_event.set()
def _set_js_api(self): def _register_window_object(): frame.addToJavaScriptWindowObject('external', self.js_bridge) code = 'qtwebengine' if is_webengine else 'qtwebkit' script = parse_api_js(self.js_bridge.window.js_api, code) if is_webengine: qwebchannel_js = QtCore.QFile('://qtwebchannel/qwebchannel.js') if qwebchannel_js.open(QtCore.QFile.ReadOnly): source = bytes(qwebchannel_js.readAll()).decode('utf-8') self.view.page().runJavaScript(source) self.channel.registerObject('external', self.js_bridge) qwebchannel_js.close() else: frame = self.view.page().mainFrame() _register_window_object() try: # < QT 5.6 self.view.page().mainFrame().evaluateJavaScript(script) except AttributeError: self.view.page().runJavaScript(script) self.loaded.set()
def _set_js_api(self): script = parse_api_js(self.js_bridge.api) self.webkit.windowScriptObject().evaluateWebScript_(script) pwv_obj = self.webkit.windowScriptObject().valueForKey_('pywebview') pwv_obj.setValue_forKey_(self.js_bridge, '_bridge')
def create_bridge(): self.webview.run_javascript(parse_api_js(self.js_bridge.window, 'gtk', uid=self.js_bridge.uid)) self.loaded.set()