qutebrowser: Slightly refactor translate & codegrab
Code grabbing has been slightly refactored to always check for the existence of the qutebrowser fifo before sending anything. Translate has been fixed up a little to use lingva for selection translations, only falling back to google translate for complete page translations since I do not think any other services support this for now.
This commit is contained in:
parent
f7304b8941
commit
ae58c1c4ef
2 changed files with 11 additions and 10 deletions
|
@ -14,16 +14,17 @@ else:
|
||||||
PYPERCLIP = True
|
PYPERCLIP = True
|
||||||
|
|
||||||
|
|
||||||
def parse_text_content(element):
|
def parse_text_content(element) -> str:
|
||||||
root = ET.fromstring(element)
|
root = ET.fromstring(element)
|
||||||
text = ET.tostring(root, encoding="unicode", method="text")
|
text = ET.tostring(root, encoding="unicode", method="text")
|
||||||
text = html.unescape(text)
|
text = html.unescape(text)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def send_command_to_qute(command):
|
def send_command_to_qute(command) -> None:
|
||||||
with open(os.environ.get("QUTE_FIFO"), "w") as f:
|
if fifo := os.environ.get("QUTE_FIFO"):
|
||||||
f.write(command)
|
with open(fifo, "w") as f:
|
||||||
|
f.write(command)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -32,7 +33,7 @@ def main():
|
||||||
# https://github.com/qutebrowser/qutebrowser/blob/master/doc/userscripts.asciidoc
|
# https://github.com/qutebrowser/qutebrowser/blob/master/doc/userscripts.asciidoc
|
||||||
element = os.environ.get("QUTE_SELECTED_HTML")
|
element = os.environ.get("QUTE_SELECTED_HTML")
|
||||||
code_text = parse_text_content(element)
|
code_text = parse_text_content(element)
|
||||||
if PYPERCLIP:
|
if pyperclip and PYPERCLIP:
|
||||||
pyperclip.copy(code_text)
|
pyperclip.copy(code_text)
|
||||||
send_command_to_qute(
|
send_command_to_qute(
|
||||||
"message-info 'copied to clipboard: {info}{suffix}'".format(
|
"message-info 'copied to clipboard: {info}{suffix}'".format(
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
# Translate the page, or if `--text` argument is given the current selection, with google translate.
|
# Translate the page, or if `--text` argument is given the current selection, with google translate.
|
||||||
#
|
#
|
||||||
# Adapted code from https://github.com/AckslD/Qute-Translate, with much gratitude.
|
# Adapted code from https://github.com/AckslD/Qute-Translate, with much gratitude.
|
||||||
|
PAGE="https://lingva.garudalinux.org/SOURCELANGUAGE/TARGETLANGUAGE/TRANSLATETEXT"
|
||||||
|
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
case $1 in
|
case $1 in
|
||||||
|
@ -38,14 +39,13 @@ fi
|
||||||
|
|
||||||
if [ "$QUTE_TRANS_URL" = "false" ]; then
|
if [ "$QUTE_TRANS_URL" = "false" ]; then
|
||||||
# Translate selected text
|
# Translate selected text
|
||||||
PAGE="https://translate.google.com/#view=home&op=translate&"
|
PAGE=$(echo "$PAGE" | sed -e "s/SOURCELANGUAGE/$QUTE_TRANS_SOURCE/" -e "s/TARGETLANGUAGE/$QUTE_TRANS_TARGET/" -e "s/TRANSLATETEXT/$QUTE_SELECTED_TEXT/")
|
||||||
CONT_KEY="text"
|
echo "open -t ${PAGE}" >>"$QUTE_FIFO"
|
||||||
CONTENT=$QUTE_SELECTED_TEXT
|
|
||||||
else
|
else
|
||||||
# Default translate URL
|
# Default translate URL
|
||||||
PAGE="https://translate.google.com/translate?"
|
PAGE="https://translate.google.com/translate?"
|
||||||
CONT_KEY="u"
|
CONT_KEY="u"
|
||||||
CONTENT=$QUTE_URL
|
CONTENT="$QUTE_URL"
|
||||||
|
echo "open -t ${PAGE}sl=${QUTE_TRANS_SOURCE}&tl=${QUTE_TRANS_TARGET}&${CONT_KEY}=\"${CONTENT}\"" >>"$QUTE_FIFO"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "open -t ${PAGE}sl=$QUTE_TRANS_SOURCE&tl=$QUTE_TRANS_TARGET&$CONT_KEY=$CONTENT" >>"$QUTE_FIFO"
|
|
||||||
|
|
Loading…
Reference in a new issue