Commit 4beaf6c3 authored by Cédric Bellegarde's avatar Cédric Bellegarde

Do not run readability when not needed

parent 82d6ec3c
Pipeline #37308 passed with stage
in 11 minutes and 6 seconds
......@@ -1837,15 +1837,21 @@ var uri = {
};
if (typeof document !== 'undefined') {
reader = new Readability(uri, document);
if (reader.isProbablyReaderable(false))
// Youtube fails if we do a document.cloneNode(), so only do this if isProbablyReaderable()
if (isProbablyReaderable == true) {
var documentClone = document.cloneNode(true);
reader = new Readability(uri, documentClone);
article = reader.parse();
// BIG HACK but webkitgtk doesn't allow us to read result from js
// FIXME: webkitgtk allows js introspection since 2.20
var previous_title = document.title;
alert("@EOLIE_READER@".concat(article.content));
document.title=previous_title;
}
else {
reader = new Readability(uri, document);
if (reader.isProbablyReaderable(false)) {
var isProbablyReaderable=true;
var previous_title = document.title;
alert("@EOLIE_READERABLE@");
document.title=previous_title;
}
}
}
......@@ -152,14 +152,15 @@ class ToolbarTitle(Gtk.Bin):
self.__signal_id = self.__entry.connect("changed",
self.__on_entry_changed)
def set_reading(self):
def set_readable_button_state(self, reading):
"""
Mark readable button
@param reading as bool
"""
if self.__indicator_stack.get_visible_child_name() != "image":
return
child = self.__indicator_stack.get_visible_child()
if self.__window.container.current.reading:
if reading:
child.get_style_context().add_class("selected")
else:
child.get_style_context().remove_class("selected")
......@@ -345,7 +346,7 @@ class ToolbarTitle(Gtk.Bin):
if b:
self.__indicator_stack.show()
self.__indicator_stack.set_visible_child_name("image")
self.set_reading()
self.set_readable_button_state(False)
else:
self.__indicator_stack.hide()
......@@ -607,8 +608,13 @@ class ToolbarTitle(Gtk.Bin):
@param eventbox as Gtk.EventBox
@param event as Gdk.Event
"""
self.__window.container.current.switch_read_mode()
self.set_reading()
view = self.__window.container.current
self.set_readable_button_state(not view.reading)
if view.reading:
view.stop_reading()
else:
view.webview.run_javascript_from_gresource(
"/org/gnome/Eolie/Readability.js", None, None)
return True
def _on_indicator2_press(self, eventbox, event):
......
......@@ -76,46 +76,9 @@ class View(Gtk.Overlay):
# Connect signals
self.connect("key-press-event", self.__on_key_press_event)
webview.connect("mouse-target-changed", self.__on_mouse_target_changed)
webview.connect("readable", self.__on_readable)
webview.connect("readability-content", self.__on_readability_content)
webview.connect("close", self.__on_close)
def switch_read_mode(self):
"""
Show a readable version of page if available.
If in read mode, switch back to page
If force, always go in read mode
@param force as bool
"""
system = Gio.Settings.new("org.gnome.desktop.interface")
document_font_name = system.get_value("document-font-name").get_string(
)
document_font_size = str(int(document_font_name[-2:]) * 1.3) + "pt"
if self.__reading_view is None:
self.__reading_view = WebKit2.WebView.new()
self.__reading_view.connect("decide-policy",
self.__on_decide_policy)
self.__reading_view.show()
self.add_overlay(self.__reading_view)
if self.__webview.readable_content:
self.__in_read_mode = True
html = "<html><head>\
<style type='text/css'>\
*:not(img) {font-size: %s;\
background-color: #333333;\
color: #e6e6e6;\
margin-left: auto;\
margin-right: auto;\
width: %s}\
</style></head>" % (document_font_size,
self.get_allocated_width() / 1.5)
html += "<title>%s</title>" % self.__webview.title
html += self.__webview.readable_content
html += "</html>"
GLib.idle_add(self.__reading_view.load_html, html, None)
else:
self.__reading_view.destroy()
self.__reading_view = None
def free_webview(self):
"""
Free the webview associated with view
......@@ -131,6 +94,13 @@ class View(Gtk.Overlay):
self.emit("destroying")
GLib.timeout_add(1000, self.__destroy)
def stop_reading(self):
"""
Destroy reading view
"""
self.__reading_view.destroy()
self.__reading_view = None
def set_window(self, window):
"""
Set window
......@@ -244,10 +214,33 @@ class View(Gtk.Overlay):
else:
self.__uri_label.hide()
def __on_readable(self, webview):
def __on_readability_content(self, webview, content):
"""
Show readable button in titlebar
Show reading view
@param webview as WebView
@param content as str
"""
if webview.get_mapped():
self.__window.toolbar.title.show_readable_button(True)
system = Gio.Settings.new("org.gnome.desktop.interface")
document_font_name = system.get_value("document-font-name").get_string(
)
document_font_size = str(int(document_font_name[-2:]) * 1.3) + "pt"
if self.__reading_view is None:
self.__reading_view = WebKit2.WebView.new()
self.__reading_view.connect("decide-policy",
self.__on_decide_policy)
self.__reading_view.show()
self.add_overlay(self.__reading_view)
html = "<html><head>\
<style type='text/css'>\
*:not(img) {font-size: %s;\
background-color: #333333;\
color: #e6e6e6;\
margin-left: auto;\
margin-right: auto;\
width: %s}\
</style></head>" % (document_font_size,
self.get_allocated_width() / 1.5)
html += "<title>%s</title>" % self.__webview.title
html += content
html += "</html>"
self.__reading_view.load_html(html, None)
......@@ -423,14 +423,6 @@ class WebView(WebKit2.WebView):
"""
return self.__selection
@property
def readable_content(self):
"""
Readable content
@return content as str
"""
return self._readable_content
#######################
# PRIVATE #
#######################
......@@ -458,7 +450,6 @@ class WebView(WebKit2.WebView):
# WebKitGTK doesn't provide an API to get selection, so try to guess
# it from clipboard FIXME Get it from extensions
self.__selection = ""
self._readable_content = ""
self.__uri = None
self._title = None
self.__related_view = related_view
......
......@@ -30,7 +30,7 @@ class WebViewSignals(WebViewMenuSignals, WebViewJsSignals,
"""
gsignals = {
"readable": (GObject.SignalFlags.RUN_FIRST, None, ()),
"readability-content": (GObject.SignalFlags.RUN_FIRST, None, (str,)),
"shown": (GObject.SignalFlags.RUN_FIRST, None, ()),
"title-changed": (GObject.SignalFlags.RUN_FIRST, None, (str,)),
"uri-changed": (GObject.SignalFlags.RUN_FIRST, None, (str,)),
......
......@@ -56,8 +56,10 @@ class WebViewJsSignals:
message = dialog.get_message()
# Reader js message
if message.startswith("@EOLIE_READER@"):
self._readable_content = message.replace("@EOLIE_READER@", "")
self.emit("readable")
self.emit("readability-content",
message.replace("@EOLIE_READER@", ""))
elif message.startswith("@EOLIE_READERABLE@"):
self._window.toolbar.title.show_readable_button(True)
# OpenSearch message
elif message.startswith("@EOLIE_OPENSEARCH@"):
uri = message.replace("@EOLIE_OPENSEARCH@", "")
......
......@@ -142,8 +142,6 @@ class Window(Gtk.ApplicationWindow):
else:
self.toolbar.title.progress.hide()
self.toolbar.title.set_loading(False)
readable_content = webview.readable_content
self.toolbar.title.show_readable_button(readable_content != "")
profile = name_from_profile_id(webview.profile)
title = webview.title
if title:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment