mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Word lookup now works
This commit is contained in:
parent
3772cfbfba
commit
ded9e3ef70
23
resources/lookup.js
Normal file
23
resources/lookup.js
Normal file
@ -0,0 +1,23 @@
|
||||
/* vim:fileencoding=utf-8
|
||||
*
|
||||
* Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPLv3 license
|
||||
*/
|
||||
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
function fix_google_markup() {
|
||||
var cc = document.getElementById('center_col');
|
||||
cc.style.marginLeft = '0';
|
||||
cc = document.getElementById('cnt');
|
||||
if (cc) cc.style.paddingTop = '0';
|
||||
['sfcnt', 'top_nav', 'before-appbar', 'appbar', 'searchform', 'easter-egg'].forEach(function(id) {
|
||||
var elem = document.getElementById(id);
|
||||
if (elem) elem.style.display = 'none';
|
||||
});
|
||||
}
|
||||
|
||||
if (window.location.hostname === 'www.google.com') fix_google_markup();
|
||||
})();
|
@ -5,6 +5,7 @@
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
from PyQt5.Qt import (
|
||||
QApplication, QComboBox, QHBoxLayout, QLabel, Qt, QTimer, QUrl, QVBoxLayout,
|
||||
@ -14,10 +15,10 @@ from PyQt5.QtWebEngineWidgets import (
|
||||
QWebEnginePage, QWebEngineProfile, QWebEngineView
|
||||
)
|
||||
|
||||
from calibre import random_user_agent
|
||||
from calibre import prints, random_user_agent
|
||||
from calibre.constants import cache_dir
|
||||
from calibre.gui2.viewer.web_view import vprefs
|
||||
from calibre.gui2.webengine import secure_webengine
|
||||
from calibre.gui2.webengine import create_script, insert_scripts, secure_webengine
|
||||
|
||||
vprefs.defaults['lookup_locations'] = [
|
||||
{
|
||||
@ -47,12 +48,24 @@ def create_profile():
|
||||
ans = QWebEngineProfile('viewer-lookup', QApplication.instance())
|
||||
ans.setHttpUserAgent(random_user_agent(allow_ie=False))
|
||||
ans.setCachePath(os.path.join(cache_dir(), 'ev2vl'))
|
||||
js = P('lookup.js', data=True, allow_user_override=False)
|
||||
insert_scripts(ans, create_script('lookup.js', js))
|
||||
s = ans.settings()
|
||||
s.setDefaultTextEncoding('utf-8')
|
||||
create_profile.ans = ans
|
||||
return ans
|
||||
|
||||
|
||||
class Page(QWebEnginePage):
|
||||
|
||||
def javaScriptConsoleMessage(self, level, msg, linenumber, source_id):
|
||||
prefix = {QWebEnginePage.InfoMessageLevel: 'INFO', QWebEnginePage.WarningMessageLevel: 'WARNING'}.get(
|
||||
level, 'ERROR')
|
||||
if source_id == 'userscript:lookup.js':
|
||||
prints('%s: %s:%s: %s' % (prefix, source_id, linenumber, msg), file=sys.stderr)
|
||||
sys.stderr.flush()
|
||||
|
||||
|
||||
class Lookup(QWidget):
|
||||
|
||||
def __init__(self, parent):
|
||||
@ -70,26 +83,38 @@ class Lookup(QWidget):
|
||||
self.label = la = QLabel(_('Lookup &in:'))
|
||||
h.addWidget(la), h.addWidget(sb), la.setBuddy(sb)
|
||||
self.view = QWebEngineView(self)
|
||||
self._page = QWebEnginePage(create_profile(), self.view)
|
||||
self._page = Page(create_profile(), self.view)
|
||||
secure_webengine(self._page, for_viewer=True)
|
||||
self.view.setPage(self._page)
|
||||
l.addWidget(self.view)
|
||||
self.populate_sources()
|
||||
self.source_box.currentIndexChanged.connect(self.update_query)
|
||||
self.source_box.currentIndexChanged.connect(self.source_changed)
|
||||
self.view.setHtml('<p>' + _('Double click on a word in the viewer window'
|
||||
' to look it up.'))
|
||||
|
||||
def source_changed(self):
|
||||
vprefs['lookup_location'] = self.source['name']
|
||||
self.update_query()
|
||||
|
||||
def populate_sources(self):
|
||||
sb = self.source_box
|
||||
sb.clear()
|
||||
for item in vprefs['lookup_locations']:
|
||||
sb.addItem(item['name'], item)
|
||||
idx = sb.findText(item['name'], Qt.MatchExactly)
|
||||
idx = sb.findText(vprefs['lookup_location'], Qt.MatchExactly)
|
||||
if idx > -1:
|
||||
self.setCurrentIndex(idx)
|
||||
sb.setCurrentIndex(idx)
|
||||
|
||||
def visibility_changed(self, is_visible):
|
||||
self.is_visible = is_visible
|
||||
self.update_query()
|
||||
|
||||
@property
|
||||
def source(self):
|
||||
idx = self.source_box.currentIndex()
|
||||
if idx > -1:
|
||||
return self.source_box.itemData(idx)
|
||||
|
||||
@property
|
||||
def url_template(self):
|
||||
idx = self.source_box.currentIndex()
|
||||
@ -101,7 +126,7 @@ class Lookup(QWidget):
|
||||
query = self.selected_text or self.current_query
|
||||
if self.current_query == query and self.current_source == self.url_template:
|
||||
return
|
||||
if not self.is_visible:
|
||||
if not self.is_visible or not query:
|
||||
return
|
||||
self.current_source = self.url_template
|
||||
url = self.current_source.format(word=query)
|
||||
|
Loading…
x
Reference in New Issue
Block a user