From a158ff142a25dd42c0ace7ef57f5d719abbebc3f Mon Sep 17 00:00:00 2001 From: RKest Date: Sat, 2 Nov 2024 06:08:52 +0100 Subject: [PATCH 1/3] Upgraded to python3, simplified code --- extension/bg.js | 21 +++++------- extension/manifest.json | 3 +- host/main.py | 73 +++++++++++++++++------------------------ scripts/install.sh | 6 ++-- 4 files changed, 43 insertions(+), 60 deletions(-) diff --git a/extension/bg.js b/extension/bg.js index b02aacc..c219209 100644 --- a/extension/bg.js +++ b/extension/bg.js @@ -39,10 +39,8 @@ const CMDS = { 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), }); }); }, @@ -52,10 +50,8 @@ const CMDS = { 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), }); }); }, @@ -73,10 +69,8 @@ const CMDS = { 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 +80,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 +144,4 @@ 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 From 830d4710712cd0317ffa90fd9285a29d646d3de8 Mon Sep 17 00:00:00 2001 From: RKest Date: Sat, 2 Nov 2024 06:17:09 +0100 Subject: [PATCH 2/3] Removed console.log --- extension/bg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/bg.js b/extension/bg.js index c219209..ce58a9d 100644 --- a/extension/bg.js +++ b/extension/bg.js @@ -80,7 +80,6 @@ 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 !== '') { @@ -145,3 +144,4 @@ function main() { }; main(); + From 2b22a0c6a02ef46386ecc5424b8f6343713bee3c Mon Sep 17 00:00:00 2001 From: RKest Date: Sat, 2 Nov 2024 06:20:03 +0100 Subject: [PATCH 3/3] Removed command filed --- extension/bg.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/extension/bg.js b/extension/bg.js index ce58a9d..98bd2fb 100644 --- a/extension/bg.js +++ b/extension/bg.js @@ -37,7 +37,6 @@ const CMDS = { switchTab() { chrome.tabs.query({}, function (tabs) { state.port.postMessage({ - 'cmd': 'dmenu', 'info': 'switchTab', 'rofi_flags': ['-i', '-p', 'tab'], 'choices': tabs.map(e => (e.id) + ': ' + e.title + ' ::: ' + e.url), @@ -48,7 +47,6 @@ const CMDS = { openHistory() { refreshHistory(function (results) { state.port.postMessage({ - 'cmd': 'dmenu', 'info': 'openHistory', 'rofi_flags': ['-matching', 'normal', '-i', '-p', 'history'], 'choices': results.map(e => e.title + ' ::: ' + e.url), @@ -67,7 +65,6 @@ const CMDS = { refreshHistory(function (results) { state.port.postMessage({ - 'cmd': 'dmenu', 'info': 'changeToPage', 'rofi_flags': ['-matching', 'normal', '-i', '-p', 'page'], 'choices': results.filter(e => e.url.indexOf(pageOrigin) === 0).map(e => e.title + ' ::: ' + e.url), @@ -80,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 !== '') {