Skip to content

Instantly share code, notes, and snippets.

@Xifax
Last active September 15, 2015 08:22
Show Gist options
  • Save Xifax/aa5fcacebe480ebdd940 to your computer and use it in GitHub Desktop.
Save Xifax/aa5fcacebe480ebdd940 to your computer and use it in GitHub Desktop.
Anki addon to lookup selected character on click.
# coding: utf-8
"""
Modification at the request of François LEROY.
Tries to lookup single first character from selection on right click, using hsk.academy.
Author: Artiom Basenko <demi.log@gmail.com>
License: The MIT License (MIT)
"""
# Stdlib
import urllib
# Anki
from aqt.qt import *
from aqt.webview import AnkiWebView
from anki.hooks import addHook
# Qt
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class HSK:
"""hsk.academy lookuper
"""
# Lookup options
BASE_URL = 'http://www.hsk.academy/fr/'
CHARA_URL = 'characters/%s'
def get_selected(self, view):
"""Copy selected text"""
return view.page().selectedText()
def lookup_character_action(self, view, menu):
"""Lookup single character action"""
url = QUrl.fromEncoded(self.BASE_URL + self.CHARA_URL %
urllib.quote(
self.get_selected(view)[:1].encode('utf8', 'ignore')))
if self.get_selected(view):
QDesktopServices.openUrl(url)
# Add lookup actions to context menu
hsk = HSK()
addHook("AnkiWebView.contextMenuEvent", hsk.lookup_character_action)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment