diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 69db556d5f..8c6e6f223b 100644 Binary files a/resources/compiled_coffeescript.zip and b/resources/compiled_coffeescript.zip differ diff --git a/src/calibre/ebooks/oeb/display/utils.coffee b/src/calibre/ebooks/oeb/display/utils.coffee index 5558b1ad0a..8ba0f9653e 100644 --- a/src/calibre/ebooks/oeb/display/utils.coffee +++ b/src/calibre/ebooks/oeb/display/utils.coffee @@ -103,6 +103,22 @@ class CalibreUtils return this.viewport_to_document(r.top, 0, elem.ownerDocument)[0] # }}} + word_at_point: (x, y) -> # {{{ + # Return the word at the specified point (in viewport co-ordinates) + range = if document.caretPositionFromPoint then document.caretPositionFromPoint(x, y) else document.caretRangeFromPoint(x, y) + if range == null + return null + node = range.startContainer + if node.nodeType != Node.TEXT_NODE + return null + offset = range.startOffset + range = document.createRange() + range.selectNodeContents(node) + range.setStart(node, offset) + range.setEnd(node, offset+1) + range.expand('word') + return range.toString() + # }}} if window? window.calibre_utils = new CalibreUtils() diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index ec804679f9..9413f12b7e 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -364,10 +364,7 @@ class Document(QWebPage): # {{{ return ans def javaScriptConsoleMessage(self, msg, lineno, msgid): - if self.debug_javascript: - prints(msg) - else: - return QWebPage.javaScriptConsoleMessage(self, msg, lineno, msgid) + prints(msg) def javaScriptAlert(self, frame, msg): if self.debug_javascript: @@ -681,6 +678,12 @@ class DocumentView(QWebView): # {{{ ac = getattr(self, '%s_action' % x) menu.addAction(ac.icon(), '%s [%s]' % (unicode(ac.text()), ','.join(self.shortcuts.get_shortcuts(sc))), ac.trigger) + if from_touch and self.manager is not None: + word = unicode(mf.evaluateJavaScript('window.calibre_utils.word_at_point(%f, %f)' % (ev.pos().x(), ev.pos().y())).toString()) + if word: + menu.addAction(self.dictionary_action.icon(), _('Lookup %s in the dictionary') % word, partial(self.manager.lookup, word)) + menu.addAction(self.search_online_action.icon(), _('Search for %s online') % word, partial(self.do_search_online, word)) + if not text and img.isNull(): menu.addSeparator() if self.manager.action_back.isEnabled(): @@ -748,8 +751,11 @@ class DocumentView(QWebView): # {{{ def search_online(self): t = unicode(self.selectedText()).strip() if t: - url = 'https://www.google.com/search?q=' + QUrl().toPercentEncoding(t) - open_url(QUrl.fromEncoded(url)) + self.do_search_online(t) + + def do_search_online(self, text): + url = 'https://www.google.com/search?q=' + QUrl().toPercentEncoding(text) + open_url(QUrl.fromEncoded(url)) def set_manager(self, manager): self.manager = manager