From de67da8ed7af8a769c6c5ef850dd2975720590dd Mon Sep 17 00:00:00 2001 From: Kwaadpepper Date: Wed, 23 Sep 2015 08:05:20 +0200 Subject: [PATCH] Linux Fix and improvements Fix the deadlock issue caused by --sync (removed --sync since it is useless) Fix the Chromebased since it needs focus before send input Fix Firefox bug with xdotool since invisible window is returned by xdotool --visible-only Added Opera support Added Chromium support Added Firefox Nightly builds support (should be the same on mac, linux and windows) Fix Regex Application title Fix to only catch application name not openned webpages sudo apt-get install xdotool wmctrl Fixed regex to match no titled window (Firefox only, or Firefox Developper Edition with ) Added Missing opera for linux in Readme Added Missing chromium in readme --- BrowserRefresh.py | 3 ++ README.md | 6 ++-- linux/__init__.py | 81 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 70 insertions(+), 20 deletions(-) diff --git a/BrowserRefresh.py b/BrowserRefresh.py index 9fee847..cda0185 100644 --- a/BrowserRefresh.py +++ b/BrowserRefresh.py @@ -65,6 +65,9 @@ def run(self, args, activate=True, if 'firefox' in browsers: refresher.firefox() + if 'nightly' in browsers: + refresher.nightly() + if 'firefoxdev' in browsers and _os == 'Darwin': refresher.firefox_dev() diff --git a/README.md b/README.md index da48739..58aff57 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ git clone https://github.com/gcollazo/BrowserRefresh-Sublime.git "Browser Refres Install xdotool: ``` -sudo apt-get install xdotool +sudo apt-get install xdotool wmctrl ``` ### 2. Configure Key Bindings @@ -64,11 +64,13 @@ Specify which browsers to refresh on command. The default is `chrome` which will |---------------------------|--------------|-----------------| | Google Chrome | `chrome` | Mac, Win, Linux | | Google Chrome Canary | `canary` | Mac, Win | +| Chromium | `chromium | Linux | | Safari | `safari` | Mac, Win | | WebKit | `webkit` | Mac | | Firefox | `firefox` | Mac, Win, Linux | | Firefox Developer Edition | `firefoxdev` | Mac | -| Opera | `opera` | Mac, Win | +| Firefox Nightly | `nightly` | Mac, Win, Linux | +| Opera | `opera` | Mac, Win, Linux | | Internet Explorer | `ie` | Win | | SRWare Iron | `iron` | Win | | Yandex | `yandex` | Mac | diff --git a/linux/__init__.py b/linux/__init__.py index eaf4908..4529f47 100644 --- a/linux/__init__.py +++ b/linux/__init__.py @@ -1,15 +1,15 @@ import sublime -from subprocess import call - +from subprocess import call, Popen, PIPE, STDOUT class LinuxBrowserRefresh: def __init__(self, activate_browser): - # activate_browser is always true on Windows since you can't - # send keys to an inactive window programmatically. We ignore it. self.activate_browser = activate_browser def chrome(self): - self.SendKeysToAllWindows('google-chrome', 'F5') + self.SendKeyToAllWebkitBasedNavigators('google-chrome', 'ctrl+F5') + + def chromium(self): + self.SendKeyToAllWebkitBasedNavigators('chromium-browser', 'ctrl+F5') def iron(self): pass @@ -26,28 +26,73 @@ def safari64(self): def firefox(self): self.SendKeysToAllWindows('firefox', 'F5') + def nightly(self): + self.SendKeysToAllWindows('nightly', 'F5') + def opera(self): - pass - # except NotImplemented("Opera support not implemented yet.") + self.SendKeyToAllWebkitBasedNavigators('opera', 'ctrl+F5') def ie(self): pass # except NotImplemented("IE support not implemented yet.") + def SendKeyToAllWebkitBasedNavigators(self, cls, key): + cmd = ['xdotool', 'search', '--onlyvisible', '--class', cls, 'windowactivate', 'key', key] + + if self.activate_browser: + cmd += ['windowactivate'] + else: + # trick to bring sublime text back to front + # this avoids the webkit bug where you have + # to activatewindow before inputing key + call(['subl']) + + try: + call(cmd) + except Exception: + self.print_error(cmd) + def SendKeysToAllWindows(self, cls, key): "Sends the keystroke to all windows whose title matches the regex" - cmd = ['xdotool', 'search', '--sync', '--onlyvisible', '--class', cls, 'windowfocus', 'key', key] + cmd = ['wmctrl', '-l'] + try: + process = Popen(cmd, stdout=PIPE) + out, err = process.communicate() + except Exception: + self.print_error(cmd) + return - if self.activate_browser: - cmd += ['windowactivate'] + process = Popen(['grep', '-ie', '[-]\?[a-z0-9 ]*%s[a-z0-9 ]*$' % cls], stdout=PIPE, stdin=PIPE) + process.stdin.write(out) + out, err = process.communicate() - status_code = call(cmd) + wID = "" + try: + wID = out.split()[0].decode(encoding='UTF-8') + except Exception: + # no window found + return + + cmd = ['xdotool', 'key', '--window', wID, key] + try: + call(cmd) + except Exception: + self.print_error(cmd) + return + + if self.activate_browser: + cmd = ['xdotool', 'windowactivate', wID] + try: + call(cmd) + except Exception: + self.print_error(cmd) + return - if status_code != 0: - sublime.error_message( - 'Browser Refresh cannot execute the specified program.\n\n' - '%s\n\n' - 'If program \'xdotool\' is currently not installed ' - 'you can install it by typing:\n\n' - 'sudo apt-get install xdotool' % " ".join(cmd)) + def print_error(prog, cmd): + sublime.error_message( + 'Browser Refresh cannot execute the specified program.\n\n' + '%s\n\n' + 'If program \'%s\' is currently not installed ' + 'you can install it by typing:\n\n' + 'sudo apt-get install %s' % (' '.join(cmd), cmd[0], cmd[0]))