├── LICENSE ├── README.md ├── chrome-switch-tabs └── chrome-switch-tabs └── firefox-switch-tabs ├── firefox-switch-tabs └── mozrepl /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Kevin Morio 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Rofi switch browser tabs 2 | =================== 3 | 4 | These scripts allow to use [rofi] to switch between tabs in Firefox and Google Chrome / Chromium. 5 | 6 | 7 | Firefox 8 | --------- 9 | 10 | #### Dependencies 11 | - Python 12 | - jq 13 | 14 | #### Installation 15 | 1. Install [MozRepl] 16 | 2. Press Alt and go to Tools->MozRepl->Activate on startup 17 | 3. Copy `firefox-switch-tabs` and `mozrepl` to a location which is in your path 18 | 4. Invoke rofi with `rofi -modi 'firefox:firefox-switch-tabs' -show firefox` 19 | 20 | Chrome 21 | ---------- 22 | 23 | #### Dependencies 24 | - Nodejs 25 | - jq 26 | 27 | #### Installation 28 | 1. `npm install -g chrome-remote-interface` 29 | 2. `sudo $EDITOR /usr/bin/chrome-remote-interface` and change `Line 190` from `console.log(display(tabs))` to `console.log(display(JSON.stringify(tabs)))` (this step can be skipped in newer versions of `chrome-remote-interface`) 30 | 3. Copy `chrome-switch-tabs` (the file, not the folder) to a location which is in your path 31 | 3. Run `google-chrome/chromium --remote-debugging-port=9222` 32 | 4. Invoke rofi with `rofi -modi 'chrome:chrome-switch-tabs' -show chrome` 33 | 34 | Known bugs 35 | --------------- 36 | 37 | #### Firefox 38 | Firefox does not load all opened tabs at startup. Unfortunately only loaded tabs can be shown. 39 | 40 | [rofi]: https://github.com/davatorium/rofi 41 | [MozRepl]: https://github.com/bard/mozrepl 42 | -------------------------------------------------------------------------------- /chrome-switch-tabs/chrome-switch-tabs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | TABS_JSON=$(chrome-remote-interface list | sed -e "s/^'//" -e "s/'$//" | jq -r 'map(select(.type == "page") | {id: .id, title: .title})') 4 | 5 | if [[ -z $@ ]]; then 6 | TAB_NAMES=$(echo "$TABS_JSON" | jq -r 'map(.title) | .[]') 7 | 8 | echo "$TAB_NAMES" 9 | else 10 | TAB=$* 11 | 12 | TAB_ID=$(echo "$TABS_JSON" | jq -r "map(select(.title | contains (\"${TAB//\"/\\\"}\")) | .id) | .[]") 13 | 14 | chrome-remote-interface activate "$TAB_ID" >/dev/null 15 | fi 16 | -------------------------------------------------------------------------------- /firefox-switch-tabs/firefox-switch-tabs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | TABS_JSON=$(mozrepl | sed -e 's/^"//' -e 's/"$//') 4 | 5 | if [[ -z $@ ]]; then 6 | TAB_NAMES=$(echo "$TABS_JSON" | jq -r 'to_entries | map(select(.value!="") | .value) | .[]') 7 | 8 | echo "$TAB_NAMES" 9 | else 10 | TAB=$* 11 | 12 | TAB_NUM=$(echo "$TABS_JSON" | jq -r "to_entries | map(select(.value | contains (\"${TAB//\"/\\\"}\")) | .key) | .[]") 13 | 14 | mozrepl "$TAB_NUM" >/dev/null 15 | fi 16 | -------------------------------------------------------------------------------- /firefox-switch-tabs/mozrepl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys, re 4 | from telnetlib import Telnet 5 | 6 | class Mozrepl(object): 7 | def __init__(self, ip="127.0.0.1", port=4242): 8 | self.ip = ip 9 | self.port = port 10 | self.prompt = b"repl>" 11 | 12 | def __enter__(self): 13 | self.t = Telnet(self.ip, self.port) 14 | intro = self.t.read_until(self.prompt, 1) 15 | if not intro.endswith(self.prompt): 16 | self.prompt = re.search(br"repl\d+>", intro).group(0) 17 | print("Waited due to nonstandard prompt:", self.prompt.decode()) 18 | return self 19 | 20 | def __exit__(self, type, value, traceback): 21 | self.t.close() 22 | del self.t 23 | 24 | def js(self, command): 25 | self.t.write(command.encode() + b"\n") 26 | return self.t.read_until(self.prompt).decode().replace((b"\n" + self.prompt).decode(),'').strip() 27 | 28 | gettabs = """ 29 | function getTabs() { 30 | var tabs = {}; 31 | for (i = 0; i < gBrowser.browsers.length; i++) { 32 | tabs[i] = gBrowser.browsers[i].contentTitle 33 | } 34 | return tabs; 35 | } 36 | JSON.stringify(getTabs()); 37 | """ 38 | 39 | switchtotab = """ 40 | gBrowser.tabContainer.selectedIndex= 41 | """ 42 | 43 | with Mozrepl() as mozrepl: 44 | if len(sys.argv) > 1: 45 | print(mozrepl.js(switchtotab + sys.argv[1])) 46 | else: 47 | print(mozrepl.js(gettabs)) 48 | 49 | --------------------------------------------------------------------------------