Skip to content

Instantly share code, notes, and snippets.

@gilbertw1
Last active October 12, 2017 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilbertw1/d00914bea1cabff8f36de69d5a214198 to your computer and use it in GitHub Desktop.
Save gilbertw1/d00914bea1cabff8f36de69d5a214198 to your computer and use it in GitHub Desktop.
Externally edit text field in qutebrowser
c.aliases['edit-field'] = 'debug-pyeval -q objreg.get(\'tabbed-browser\', scope=\'window\', window=\'last-focused\').currentWidget()._widget.page().runJavaScript(\'document.activeElement.value\', lambda res: objreg.get(\'command-dispatcher\', scope=\'window\', window=\'last-focused\')._run_userscript(\'edit_field\', res, verbose=False))'
# Indented Command Python
objreg.get('tabbed-browser', scope='window', window='last-focused') \
.currentWidget()._widget.page() \
.runJavaScript('document.activeElement.value', lambda res:
objreg.get('command-dispatcher', scope='window', window='last-focused') \
._run_userscript('edit_field', res, verbose=False))
#!/bin/sh
# create a temp file to properly edit text
TEMP_FILE="$(mktemp --suffix '.txt')"
echo "$1" >> $TEMP_FILE
# editor command
EDITOR_COMMAND=${QB_EDITOR:-gvim}
# edit temp file
$EDITOR_COMMAND $TEMP_FILE
# if editing command failed then exit.
if [ $? -ne 0 ]; then
echo "Cancelled Text Edit..."
exit 1
fi
# retrieved edited text
EDIT_RESULT_BASE64="$(base64 -w 0 $TEMP_FILE)"
js() {
cat <<EOF
var decode = function(string) {
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var result = '';
var i = 0;
do {
var b1 = characters.indexOf( string.charAt(i++) );
var b2 = characters.indexOf( string.charAt(i++) );
var b3 = characters.indexOf( string.charAt(i++) );
var b4 = characters.indexOf( string.charAt(i++) );
var a = ( ( b1 & 0x3F ) << 2 ) | ( ( b2 >> 4 ) & 0x3 );
var b = ( ( b2 & 0xF ) << 4 ) | ( ( b3 >> 2 ) & 0xF );
var c = ( ( b3 & 0x3 ) << 6 ) | ( b4 & 0x3F );
result += String.fromCharCode(a) + (b?String.fromCharCode(b):'') + (c?String.fromCharCode(c):'');
} while( i < string.length );
return result;
};
var isTextBox = function(element) {
var tagName = element.tagName.toLowerCase();
if (tagName === 'textarea') return true;
if (tagName !== 'input') return false;
var type = element.getAttribute('type').toLowerCase();
var inputTypes = ['text', 'password', 'number', 'email', 'tel', 'url', 'search', 'date', 'datetime', 'datetime-local', 'time', 'month', 'week'];
return inputTypes.indexOf(type) >= 0;
};
var editedText = decode("$EDIT_RESULT_BASE64");
if (isTextBox(document.activeElement)) {
document.activeElement.value = editedText;
}
EOF
}
printjs() {
js | sed 's,//.*$,,' | tr '\n' ' '
}
echo "jseval -q $(printjs)" >> "$QUTE_FIFO"
echo "Editing complete, inserting text..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment