Add run_js_eval and get :jseval to run

This commit is contained in:
Florian Bruhin 2016-06-14 18:47:26 +02:00
parent cd95f94ac8
commit edb65ecf50
4 changed files with 41 additions and 19 deletions

View File

@ -1107,7 +1107,7 @@ class CommandDispatcher:
QWebSettings.JavascriptEnabled): QWebSettings.JavascriptEnabled):
if tab: if tab:
page.open_target = usertypes.ClickTarget.tab page.open_target = usertypes.ClickTarget.tab
page.currentFrame().evaluateJavaScript( widget.run_js_async(
'window.getSelection().anchorNode.parentNode.click()') 'window.getSelection().anchorNode.parentNode.click()')
else: else:
try: try:
@ -1698,27 +1698,30 @@ class CommandDispatcher:
js_code: The string to evaluate. js_code: The string to evaluate.
quiet: Don't show resulting JS object. quiet: Don't show resulting JS object.
""" """
frame = self._current_widget().page().mainFrame()
out = frame.evaluateJavaScript(js_code)
if quiet: if quiet:
return jseval_cb = None
else:
def jseval_cb(out):
if out is None: if out is None:
# Getting the actual error (if any) seems to be difficult. The # Getting the actual error (if any) seems to be difficult.
# error does end up in BrowserPage.javaScriptConsoleMessage(), but # The error does end up in
# distinguishing between :jseval errors and errors from the webpage # BrowserPage.javaScriptConsoleMessage(), but distinguishing
# is not trivial... # between :jseval errors and errors from the webpage is not
# trivial...
message.info(self._win_id, 'No output or error') message.info(self._win_id, 'No output or error')
else: else:
# The output can be a string, number, dict, array, etc. But *don't* # The output can be a string, number, dict, array, etc. But
# output too much data, as this will make qutebrowser hang # *don't* output too much data, as this will make
# qutebrowser hang
out = str(out) out = str(out)
if len(out) > 5000: if len(out) > 5000:
message.info(self._win_id, out[:5000] + ' [...trimmed...]') message.info(self._win_id, out[:5000] + ' [...trimmed...]')
else: else:
message.info(self._win_id, out) message.info(self._win_id, out)
self._current_widget().run_js_async(js_code, callback=jseval_cb)
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
def fake_key(self, keystring, global_=False): def fake_key(self, keystring, global_=False):
"""Send a fake keypress or key string to the website or qutebrowser. """Send a fake keypress or key string to the website or qutebrowser.

View File

@ -303,6 +303,14 @@ class AbstractTab(QWidget):
""" """
raise NotImplementedError raise NotImplementedError
def run_js_async(self, code, callback=None):
"""Run javascript async.
The given callback will be called with the result when running JS is
complete.
"""
raise NotImplementedError
def shutdown(self): def shutdown(self):
raise NotImplementedError raise NotImplementedError

View File

@ -114,6 +114,12 @@ class WebEngineViewTab(tab.AbstractTab):
else: else:
self._widget.page().toHtml(callback) self._widget.page().toHtml(callback)
def run_js_async(self, code, callback=None):
if callback is None:
self._widget.page().runJavaScript(code)
else:
self._widget.page().runJavaScript(code, callback)
def shutdown(self): def shutdown(self):
# TODO # TODO
pass pass

View File

@ -378,6 +378,11 @@ class WebViewTab(tab.AbstractTab):
else: else:
callback(frame.toHtml()) callback(frame.toHtml())
def run_js_async(self, code, callback=None):
result = self._widget.page().mainFrame().evaluateJavaScript(code)
if callback is not None:
callback(result)
def icon(self): def icon(self):
return self._widget.icon() return self._widget.icon()