Skip to content

Instantly share code, notes, and snippets.

@b1tdust
Created Sep 28, 2018
Embed
What would you like to do?
Google Translate Always on top
import gi
"""
Small script to use the mobile version of Google Translate with GTK3 and WebKit2 and Python 3.
It can get the input fromo the clipboard and paste it into the web app.
Copyleft: Brian Mayo
"""
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gtk, Gdk, WebKit2, GLib
url = "https://translate.google.com/m/translate?#es/en" # Languages ISO codes for translate spanish to english
window = Gtk.Window()
window.connect("destroy", Gtk.main_quit)
window.set_gravity(Gdk.Gravity.NORTH_WEST)
window.set_default_size(350, 400)
width, height = window.get_size()
window.move(Gdk.Screen.width() - width - 2, 100) # adjust it as you want
window.set_keep_above(True) # always on top
window.set_decorated(False) # minimalist
webview = WebKit2.WebView()
window.add(webview)
webview.load_uri(url)
class Runner:
def __init__(self):
self.last = ""
def __call__(self):
if webview.is_loading():
print('loading')
return True
text = ""
try:
clip = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
text = clip.wait_for_text()
except Exception as e:
print (str(e))
if text is not None and text != "" and text != self.last:
webview.run_javascript('document.querySelector("textarea#source").value = `{}`'.format(text))
self.last = text
return True
GLib.timeout_add_seconds(1, Runner()) # run every one second
window.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment