Last active
April 1, 2017 14:37
-
-
Save logcat/31ed7799b57363bcbe536aa55a64e292 to your computer and use it in GitHub Desktop.
Sends word from Sublime Text 3 to open redfoxsanakirja tab in chrome (works on OSX, depends on https://github.com/prasmussen/chrome-cli/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
import subprocess | |
import re | |
class RedfoxCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
redfox(word(self.view)) | |
def word(view): | |
selection = view.sel() | |
region = view.sel()[0] | |
word_region = view.word(region) | |
return view.substr(word_region) | |
def tabs_raw(): | |
return subprocess.check_output(['chrome-cli', 'list', 'tabs']).decode('utf-8').rstrip('\n') | |
def tabs(): | |
return tabs_raw().split('\n') | |
def get_id(tab): | |
return re.search('([0-9]*)]', tab).group(1) | |
def tab_ids(): | |
return [get_id(tab) for tab in tabs()] | |
def tab_details(tab_id): | |
return subprocess.check_output(['chrome-cli', 'info', '-t', tab_id]).decode('utf-8').rstrip('\n') | |
def is_website(tab_id, url): | |
return url in tab_details(tab_id) | |
def find_tab(predicate): | |
return next((tab_id for tab_id in tab_ids() if predicate(tab_id)), None) | |
def activate_tab(tab_id): | |
subprocess.check_output(['chrome-cli', 'activate', '-t', tab_id]) | |
def is_redfox(tab_id): | |
return is_website(tab_id, 'redfoxsanakirja.fi') | |
def redfox_javascript(word): | |
return 'document.getElementById(\"_redfoxportaalisanakirja_WAR_redfoxportaalisanakirja_:dictionary-form:query_word\").value = \"' + word + "\";" + "document.getElementById(\"_redfoxportaalisanakirja_WAR_redfoxportaalisanakirja_:dictionary-form:submitBtn\").click()" | |
def redfox(word): | |
tab_id = find_tab(is_redfox) | |
activate_tab(tab_id) | |
subprocess.check_output(['chrome-cli', 'execute', redfox_javascript(word), '-t', tab_id]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment