Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions extension/bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,19 @@ 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),
});
});
},

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),
});
});
},
Expand All @@ -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),
});
});
});
Expand All @@ -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 !== '') {
Expand Down Expand Up @@ -149,4 +141,5 @@ function main() {
addChromeListeners();
};

main();
main();

3 changes: 2 additions & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
"description": "go to last tab"
}
}
}
}

73 changes: 30 additions & 43 deletions host/main.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down
6 changes: 3 additions & 3 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
set -e

OS="$(uname -s)"
Expand All @@ -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
Expand Down