webview_signals_js.py 4.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# Copyright (c) 2017 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

13
from gi.repository import GLib, WebKit2
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

from gettext import gettext as _

from eolie.define import El


class WebViewJsSignals:
    """
        Handle webview scripts signals
    """

    def __init__(self):
        """
            Init class
        """
29
        self.__google_fix_count = 0
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        self.__reset_js_blocker()
        self.connect("script-dialog", self.__on_script_dialog)

#######################
# PROTECTED           #
#######################
    def _on_map(self, webview):
        """
            Connect all signals
            @param webview as WebView
        """
        self.connect("resource-load-started",
                     self.__on_resource_load_started)

    def _on_unmap(self, webview):
        """
            Disconnect all signals
            @param webview as WebView
        """
        self.disconnect_by_func(self.__on_resource_load_started)

#######################
# PRIVATE             #
#######################
    def __reset_js_blocker(self):
        """
            Reset js blocker
        """
        self.__js_dialog_type = None
        self.__js_dialog_message = None

    def __on_script_dialog(self, webview, dialog):
        """
            Show message to user
            @param webview as WebView
            @param dialog as WebKit2.ScriptDialog
        """
        message = dialog.get_message()
        # Reader js message
        if message.startswith("@EOLIE_READER@"):
            self._readable_content = message.replace("@EOLIE_READER@", "")
            self.emit("readable")
72 73 74 75
        # OpenSearch message
        elif message.startswith("@EOLIE_OPENSEARCH@"):
            uri = message.replace("@EOLIE_OPENSEARCH@", "")
            El().search.install_engine(uri, self._window)
76 77 78 79 80 81 82 83 84
        # Populars view message
        elif message.startswith("@EOLIE_HIDE_BOOKMARK_POPULARS@"):
            uri = message.replace("@EOLIE_HIDE_BOOKMARK_POPULARS@", "")
            El().bookmarks.reset_popularity(uri)
        # Populars view message
        elif message.startswith("@EOLIE_HIDE_HISTORY_POPULARS@"):
            uri = message.replace("@EOLIE_HIDE_HISTORY_POPULARS@", "")
            El().history.reset_popularity(uri)
        # Here we handle JS flood
Cédric Bellegarde's avatar
Cédric Bellegarde committed
85
        elif message == self.__js_dialog_message and\
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                dialog.get_dialog_type() == self.__js_dialog_type:
            self._window.toolbar.title.show_message(
                   _("Eolie is going to close this page because it is broken"))
            self._window.container.pages_manager.close_view(self, False)
        # Webpage message
        else:
            self.__js_dialog_type = dialog.get_dialog_type()
            self.__js_dialog_message = dialog.get_message()
            self._window.toolbar.title.show_javascript(dialog)
            GLib.timeout_add(1000, self.__reset_js_blocker)
        return True

    def __on_resource_load_started(self, webview, resource, request):
        """
            Listen to off loading events
            @param webview as WebView
            @param resource WebKit2.WebResource
            @param request as WebKit2.URIRequest
        """
        # Javascript execution happened
106
        if self.current_event == WebKit2.LoadEvent.FINISHED:
107 108
            # Special google notifications fix
            # https://bugs.webkit.org/show_bug.cgi?id=175189
109 110 111 112 113 114 115 116 117 118
            storage = El().settings.get_enum("cookie-storage")
            # WebKit2.CookieAcceptPolicy.NO_THIRD_PARTY
            if storage == 2:
                uri = resource.get_uri()
                if uri.startswith("https://notifications.google.com/"):
                    self.__google_fix_count += 1
                    resource.connect("finished",
                                     self.__on_resource_load_finished)
                    cookie_manager = self.get_context().get_cookie_manager()
                    cookie_manager.set_accept_policy(0)
119

120 121 122 123 124 125 126 127 128 129
    def __on_resource_load_finished(self, resource):
        """
            Restore cookie storage default behaviour
            @param resource as WebKit2.WebResource
        """
        self.__google_fix_count -= 1
        if self.__google_fix_count == 0:
            cookie_manager = self.get_context().get_cookie_manager()
            storage = El().settings.get_enum("cookie-storage")
            cookie_manager.set_accept_policy(storage)