コード例 #1
0
    def on_title_change(self, webview, title):
        title = webview.get_title()

        try:
            js_data = json.loads(title)

            if 'type' not in js_data:
                return

            elif js_data['type'] == 'eval' and old_webkit:  # return result of evaluate_js
                unique_id = js_data['uid']
                result = js_data['result'] if 'result' in js_data else None

                js = self.js_results[unique_id]
                js['result'] = result
                js['semaphore'].release()

            elif js_data['type'] == 'invoke':  # invoke js api's function
                func_name = js_data['function']
                value_id = js_data['id']
                param = js_data['param'] if 'param' in js_data else None
                return_val = self.js_bridge.call(func_name, param, value_id)

                # Give back the return value to JS as a string
                code = 'pywebview._bridge.return_val = "{0}";'.format(escape_string(str(return_val)))
                webview.run_javascript(code)

        except ValueError: # Python 2
            logger.debug('GTK: JSON decode failed:\n %s' % title)
        except json.JSONDecodeError: # Python 3
            logger.debug('GTK: JSON decode failed:\n %s' % title)
コード例 #2
0
ファイル: gtk.py プロジェクト: r0x0r/pywebview
    def on_title_change(self, webview, title):
        title = webview.get_title()

        try:
            js_data = json.loads(title)

            if 'type' not in js_data:
                return

            elif js_data['type'] == 'eval':  # return result of evaluate_js
                unique_id = js_data['uid']
                result = js_data['result'] if 'result' in js_data else None

                js = self.js_results[unique_id]
                js['result'] = result
                js['semaphore'].release()

            elif js_data['type'] == 'invoke':  # invoke js api's function
                func_name = js_data['function']
                param = js_data['param'] if 'param' in js_data else None
                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.run_javascript(code)

        except ValueError: # Python 2
            pass
        except json.JSONDecodeError: # Python 3
            pass
コード例 #3
0
ファイル: qt.py プロジェクト: siddht4/pywebview
    def on_load_finished(self):
        if self.js_bridge.api:
            self._set_js_api()
        else:
            self.load_event.set()

        if not self.text_select:
            self.evaluate_js(escape_string(disable_text_select))
コード例 #4
0
ファイル: winforms.py プロジェクト: Nablast/pywebview
 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()
コード例 #5
0
ファイル: gtk.py プロジェクト: siddht4/pywebview
    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)
コード例 #6
0
ファイル: gtk.py プロジェクト: Nablast/pywebview
    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