From a6dd65bea4f495d3b4d64037480c818b2ccf518f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 24 Jul 2020 19:38:57 +0530 Subject: [PATCH] Add a button to selection bar to search net --- src/calibre/gui2/viewer/web_view.py | 2 ++ src/pyj/read_book/create_annotation.pyj | 1 - src/pyj/read_book/selection_bar.pyj | 8 ++++++++ src/pyj/read_book/ui.pyj | 2 ++ src/pyj/read_book/word_actions.pyj | 3 ++- src/pyj/viewer-main.pyj | 2 ++ 6 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index a38bf13338..c87a2f51fb 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -268,6 +268,7 @@ class ViewerBridge(Bridge): scrollbar_context_menu = from_js(object, object, object) close_prep_finished = from_js(object) highlights_changed = from_js(object) + open_url = from_js(object) create_view = to_js() start_book_load = to_js() @@ -511,6 +512,7 @@ class WebView(RestartingWebEngineView): self.bridge.scrollbar_context_menu.connect(self.scrollbar_context_menu) self.bridge.close_prep_finished.connect(self.close_prep_finished) self.bridge.highlights_changed.connect(self.highlights_changed) + self.bridge.open_url.connect(safe_open_url) self.bridge.export_shortcut_map.connect(self.set_shortcut_map) self.shortcut_map = {} self.bridge.report_cfi.connect(self.call_callback) diff --git a/src/pyj/read_book/create_annotation.pyj b/src/pyj/read_book/create_annotation.pyj index 05f7184b2a..e480f16bd8 100644 --- a/src/pyj/read_book/create_annotation.pyj +++ b/src/pyj/read_book/create_annotation.pyj @@ -18,7 +18,6 @@ from widgets import create_button # TODO: # Custom colors for highlights -# Google lookup for selections # Export all annots as plain text/JSON # Fix selection bar remaining open after removing highlight diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index d321828f53..7fcba88cf0 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -46,6 +46,7 @@ class SelectionBar: bar.appendChild(cb('copy', _('Copy to clipboard'), self.copy_to_clipboard)) bar.appendChild(cb('library', _('Lookup/search selected word'), self.lookup)) bar.appendChild(cb('highlight', _('Highlight selection'), self.create_highlight)) + bar.appendChild(cb('search', _('Search for selection on the net'), self.internet_search)) bar.appendChild(cb('close', _('Clear the selection'), self.clear_selection)) self.show_notes(bar_container, notes) return bar_container @@ -83,6 +84,13 @@ class SelectionBar: else: self.view.overlay.show_word_actions(self.view.currently_showing.selection.text) + def internet_search(self): + text = self.view.currently_showing.selection.text + if text: + q = encodeURIComponent(text) + url = f'https://google.com/search?q={q}' + ui_operations.open_url(url) + def clear_selection(self): self.view.on_handle_shortcut({'name': 'clear_selection'}) diff --git a/src/pyj/read_book/ui.pyj b/src/pyj/read_book/ui.pyj index b501eda402..d0dd974759 100644 --- a/src/pyj/read_book/ui.pyj +++ b/src/pyj/read_book/ui.pyj @@ -75,6 +75,8 @@ class ReadUI: ui_operations.toggle_full_screen = self.toggle_full_screen.bind(self) ui_operations.highlights_changed = self.highlights_changed.bind(self) ui_operations.annotations_synced = self.annotations_synced.bind(self) + ui_operations.open_url = def(url): + window.open(url, '_blank') def on_resize(self): self.view.on_resize() diff --git a/src/pyj/read_book/word_actions.pyj b/src/pyj/read_book/word_actions.pyj index 39b3d48535..004e6843a8 100644 --- a/src/pyj/read_book/word_actions.pyj +++ b/src/pyj/read_book/word_actions.pyj @@ -9,12 +9,13 @@ from book_list.globals import get_session_data from book_list.item_list import create_item, create_item_list from dom import clear, ensure_id from modals import error_dialog +from read_book.globals import ui_operations from widgets import create_button def lookup(close_panel_func, url): close_panel_func() - window.open(url, '_blank') + ui_operations.open_url(url) def toggle_custom_container(container_id, visible): diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 3aa54d23cc..3662bab613 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -345,6 +345,8 @@ if window is window.top: sd.set('footer', defaults.footer) view.update_header_footer() to_python.reset_interface() + ui_operations.open_url = def(url): + to_python.open_url(url) ui_operations.quit = def(): to_python.quit() ui_operations.toggle_lookup = def(force_show):