diff --git a/extension/bg.js b/extension/bg.js index b02aacc..98bd2fb 100644 --- a/extension/bg.js +++ b/extension/bg.js @@ -37,12 +37,9 @@ const CMDS = { switchTab() { chrome.tabs.query({}, function (tabs) { state.port.postMessage({ - 'cmd': 'dmenu', 'info': 'switchTab', - 'param': { - 'rofi-opts': ['-i', '-p', 'tab'], - 'opts': tabs.map(e => (e.id) + ': ' + e.title + ' ::: ' + e.url), - } + 'rofi_flags': ['-i', '-p', 'tab'], + 'choices': tabs.map(e => (e.id) + ': ' + e.title + ' ::: ' + e.url), }); }); }, @@ -50,12 +47,9 @@ const CMDS = { openHistory() { refreshHistory(function (results) { state.port.postMessage({ - 'cmd': 'dmenu', 'info': 'openHistory', - 'param': { - 'rofi-opts': ['-matching', 'normal', '-i', '-p', 'history'], - 'opts': results.map(e => e.title + ' ::: ' + e.url), - } + 'rofi_flags': ['-matching', 'normal', '-i', '-p', 'history'], + 'choices': results.map(e => e.title + ' ::: ' + e.url), }); }); }, @@ -71,12 +65,9 @@ const CMDS = { refreshHistory(function (results) { state.port.postMessage({ - 'cmd': 'dmenu', 'info': 'changeToPage', - 'param': { - 'rofi-opts': ['-matching', 'normal', '-i', '-p', 'page'], - 'opts': results.filter(e => e.url.indexOf(pageOrigin) === 0).map(e => e.title + ' ::: ' + e.url), - } + 'rofi_flags': ['-matching', 'normal', '-i', '-p', 'page'], + 'choices': results.filter(e => e.url.indexOf(pageOrigin) === 0).map(e => e.title + ' ::: ' + e.url), }); }); }); @@ -86,6 +77,7 @@ const CMDS = { /*** listeners ***/ function onNativeMessage(message) { + console.log({ message }); if (message.info === 'switchTab' && message.result !== '') { goToTab(parseInt(message.result.split(': ')[0])); } else if (message.info === 'openHistory' && message.result !== '') { @@ -149,4 +141,5 @@ function main() { addChromeListeners(); }; -main(); \ No newline at end of file +main(); + diff --git a/extension/manifest.json b/extension/manifest.json index 55c3400..7309503 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -31,4 +31,5 @@ "description": "go to last tab" } } -} \ No newline at end of file +} + diff --git a/host/main.py b/host/main.py index aef4ef7..b893413 100755 --- a/host/main.py +++ b/host/main.py @@ -1,61 +1,48 @@ -#!/usr/bin/python2 +#!/usr/bin/env python3 -import struct import sys import json import subprocess +from typing import TypedDict, Any -def send_message(message): - sys.stdout.write(struct.pack('I', len(message))) - sys.stdout.write(message) - sys.stdout.flush() +class Params(TypedDict): + rofi_flags: list[str] + choices: list[str] + info: Any -def log(msg): - pass -def call_rofi(param): - options = param['opts'] - rofi_opts = ['rofi', '-dmenu'] - if 'rofi-opts' in param: - rofi_opts.extend(param['rofi-opts']) +def send_message(message: bytes): + _written = sys.stdout.buffer.write(len(message).to_bytes(4, byteorder='little')) + _written = sys.stdout.buffer.write(message) + _none = sys.stdout.flush() - sh = subprocess.Popen(rofi_opts, stdout=subprocess.PIPE, stdin=subprocess.PIPE) - sh.stdin.write('\n'.join(map(lambda x: x.encode('utf8'), options))) - sh.stdin.flush() - sh.stdin.close() - return sh.stdout.read().strip() +def call_rofi(param: Params): + rofi_cmd = ['rofi', '-dmenu'] + param['rofi_flags'] + choices = param['choices'] + + sh = subprocess.Popen(rofi_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) + stdout, _stderr = sh.communicate('\n'.join(choices).encode('raw_unicode_escape')) + + return stdout.decode('raw_unicode_escape') + def main(): - log('launched') while True: - data_length_bytes = sys.stdin.read(4) + data_length_bytes = sys.stdin.buffer.read(4).decode('raw_unicode_escape') if len(data_length_bytes) == 0: break - - data_length = struct.unpack('i', data_length_bytes)[0] - data = sys.stdin.read(data_length).decode('utf-8') - data = json.loads(data) - log(data) - - cmd = data['cmd'] - param = data['param'] - info = data['info'] - if cmd == 'dmenu': - output = { - 'cmd': 'dmenu', - 'result': call_rofi(param), - 'info': info - } - else: - output = { - 'result': 'unknow command: {}'.foramt(cmd) - } - send_message(json.dumps(output)) - - - sys.exit(0) + + data_length = int.from_bytes(data_length_bytes.encode('raw_unicode_escape'), byteorder='little') + data = sys.stdin.buffer.read(data_length).decode('raw_unicode_escape') + + params: Params = json.loads(data) + response = { + 'result': call_rofi(params), + 'info': params['info'] + } + send_message(json.dumps(response).encode('raw_unicode_escape')) if __name__ == '__main__': diff --git a/scripts/install.sh b/scripts/install.sh index 57a7ffc..8633dd6 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e OS="$(uname -s)" @@ -24,9 +24,9 @@ main() { print_horizontal_line # Dependencies - printf "${green}Checking Dependencies${none}: python2 rofi\n" + printf "${green}Checking Dependencies${none}: python3 rofi\n" - check_dependency python2 Python2 https://www.python.org + check_dependency python3 Python3 https://www.python.org check_dependency rofi rofi https://github.com/davatorium/rofi print_horizontal_line