Pass more useful information to userscripts.

Closes #379.
This commit is contained in:
Florian Bruhin 2015-01-04 20:16:15 +01:00
parent 013f906c3b
commit efe96462c9
3 changed files with 49 additions and 13 deletions

View File

@ -830,8 +830,28 @@ class CommandDispatcher:
args: Arguments to pass to the userscript.
"""
cmd = os.path.expanduser(cmd)
userscripts.run(cmd, *args, url=self._current_url(),
win_id=self._win_id)
env = {
'QUTE_MODE': 'command',
}
idx = self._current_index()
tabbed_browser = self._tabbed_browser()
if idx != -1:
env['QUTE_TITLE'] = tabbed_browser.tabText(idx)
webview = tabbed_browser.currentWidget()
if webview is not None and webview.hasSelection():
env['QUTE_SELECTED_TEXT'] = webview.selectedText()
env['QUTE_SELECTED_HTML'] = webview.selectedHtml()
try:
url = tabbed_browser.current_url()
except qtutils.QtValueError:
pass
else:
env['QUTE_URL'] = url.toString(QUrl.FullyEncoded)
userscripts.run(cmd, *args, win_id=self._win_id, env=env)
@cmdutils.register(instance='command-dispatcher', scope='window')
def quickmark_save(self):

View File

@ -443,16 +443,24 @@ class HintManager(QObject):
window=self._win_id)
download_manager.get(url, elem.webFrame().page())
def _call_userscript(self, url, context):
def _call_userscript(self, elem, context):
"""Call an userscript from a hint.
Args:
url: The URL to open as a QUrl.
elem: The QWebElement to use in the userscript.
context: The HintContext to use.
"""
cmd = context.args[0]
args = context.args[1:]
userscripts.run(cmd, *args, url=url, win_id=self._win_id)
env = {
'QUTE_MODE': 'hints',
'QUTE_SELECTED_TEXT': str(elem),
'QUTE_SELECTED_HTML': elem.toOuterXml(),
}
url = self._resolve_url(elem, context.baseurl)
if url is not None:
env['QUTE_URL'] = url.toString(QUrl.FullyEncoded)
userscripts.run(cmd, *args, win_id=self._win_id, env=env)
def _spawn(self, url, context):
"""Spawn a simple command from a hint.
@ -767,6 +775,7 @@ class HintManager(QObject):
Target.hover: self._click,
# _download needs a QWebElement to get the frame.
Target.download: self._download,
Target.userscript: self._call_userscript,
}
# Handlers which take a QUrl
url_handlers = {
@ -774,7 +783,6 @@ class HintManager(QObject):
Target.yank_primary: self._yank,
Target.run: self._run_cmd,
Target.fill: self._preset_cmd_text,
Target.userscript: self._call_userscript,
Target.spawn: self._spawn,
}
elem = self._context.elems[keystr].elem

View File

@ -24,10 +24,11 @@ import os.path
import tempfile
from PyQt5.QtCore import (pyqtSignal, pyqtSlot, QObject, QStandardPaths,
QSocketNotifier, QProcessEnvironment, QProcess, QUrl)
QSocketNotifier, QProcessEnvironment, QProcess)
from qutebrowser.utils import message, log, objreg, standarddir
from qutebrowser.commands import runners, cmdexc
from qutebrowser.config import config
class _QtFIFOReader(QObject):
@ -305,19 +306,26 @@ else:
UserscriptRunner = _DummyUserscriptRunner
def run(cmd, *args, url, win_id):
"""Convenience method to run an userscript."""
def run(cmd, *args, win_id, env):
"""Convenience method to run an userscript.
Args:
cmd: The userscript binary to run.
*args: The arguments to pass to the userscript.
win_id: The window id the userscript is executed in.
env: A dictionary of variables to add to the process environment.
"""
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
# We don't remove the password in the URL here, as it's probably safe to
# pass via env variable..
urlstr = url.toString(QUrl.FullyEncoded)
commandrunner = runners.CommandRunner(win_id, tabbed_browser)
runner = UserscriptRunner(win_id, tabbed_browser)
runner.got_cmd.connect(
lambda cmd: log.commands.debug("Got userscript command: {}".format(
cmd)))
runner.got_cmd.connect(commandrunner.run_safely)
runner.run(cmd, *args, env={'QUTE_URL': urlstr})
user_agent = config.get('network', 'user-agent')
if user_agent is not None:
env['QUTE_USER_AGENT'] = user_agent
runner.run(cmd, *args, env=env)
runner.finished.connect(commandrunner.deleteLater)
runner.finished.connect(runner.deleteLater)