Commit 9b2fb17c authored by Cédric Bellegarde's avatar Cédric Bellegarde

Block ads in Webview.new_page()

parent 6874e84c
......@@ -13,7 +13,6 @@
from urllib.parse import urlparse
from eolie.define import App
from eolie.database_adblock import DatabaseAdblock
from eolie.logger import Logger
......@@ -27,7 +26,6 @@ class AdblockExtension:
Connect wanted signal
@param extension as WebKit2WebExtension
"""
self.__adblock = DatabaseAdblock()
extension.connect("page-created", self.__on_page_created)
#######################
......@@ -60,9 +58,9 @@ class AdblockExtension:
elif App().settings.get_value("adblock") and\
parsed_request.scheme in ["http", "https"] and\
not App().adblock_exceptions.find_parsed(parsed_request):
if self.__adblock.is_netloc_blocked(parsed_request.netloc) or\
self.__adblock.is_uri_blocked(request_uri,
parsed_request.netloc):
if App().adblock.is_netloc_blocked(parsed_request.netloc) or\
App().adblock.is_uri_blocked(request_uri,
parsed_request.netloc):
Logger.debug("AdblockExtension: blocking %s ->%s",
request_uri, uri)
return True
......
......@@ -236,22 +236,33 @@ class WebView(WebKit2.WebView):
self._cancelled = True
WebKit2.WebView.stop_loading(self)
def new_page(self, loading_type):
def new_page(self, uri, loading_type):
"""
Open a new page
@param uri as uri
@param loading_type as Gdk.LoadingType
"""
parsed = urlparse(uri)
if App().settings.get_value("adblock") and\
parsed.scheme in ["http", "https"] and\
not App().adblock_exceptions.find_parsed(parsed):
if App().adblock.is_netloc_blocked(parsed.netloc) or\
App().adblock.is_uri_blocked(uri,
parsed.netloc):
Logger.debug("AdblockWebView: blocking %s ->%s",
uri, self.uri)
return True
if loading_type == LoadingType.POPOVER:
if self.ephemeral:
webview = WebView.new_ephemeral(self._window, None)
else:
webview = WebView.new(self._window, None)
self._window.container.popup_webview(webview, True)
GLib.idle_add(webview.load_uri, self._navigation_uri)
GLib.idle_add(webview.load_uri, uri)
else:
self._new_pages_opened += 1
webview = self._window.container.add_webview(
self._navigation_uri,
uri,
loading_type,
self.ephemeral,
None,
......
......@@ -349,27 +349,27 @@ class WebViewNavigation:
return False
navigation_action = decision.get_navigation_action()
self._navigation_uri = navigation_action.get_request().get_uri()
navigation_uri = navigation_action.get_request().get_uri()
mouse_button = navigation_action.get_mouse_button()
parsed_navigation = urlparse(self._navigation_uri)
parsed_navigation = urlparse(navigation_uri)
self.clear_text_entry()
if parsed_navigation.scheme not in ["http", "https", "file", "about",
"populars", "accept"]:
try:
Gtk.show_uri_on_window(self._window,
self._navigation_uri,
navigation_uri,
Gtk.get_current_event_time())
except Exception as e:
Logger.error("WebViewNavigation::__on_decide_policy(): %s", e)
decision.ignore()
elif mouse_button == 0:
# Prevent opening empty pages
if self._navigation_uri == "about:blank":
if navigation_uri == "about:blank":
self.reset_last_click_event()
decision.use()
return True
elif decision_type == WebKit2.PolicyDecisionType.NEW_WINDOW_ACTION:
self.new_page(LoadingType.FOREGROUND)
self.new_page(navigation_uri, LoadingType.FOREGROUND)
decision.ignore()
return True
else:
......@@ -382,24 +382,24 @@ class WebViewNavigation:
loading_type = LoadingType.POPOVER
else:
loading_type = LoadingType.FOREGROUND
self.new_page(loading_type)
self.new_page(navigation_uri, loading_type)
decision.ignore()
return True
elif self._window.modifiers & Gdk.ModifierType.CONTROL_MASK:
self.new_page(LoadingType.BACKGROUND)
self.new_page(navigation_uri, LoadingType.BACKGROUND)
decision.ignore()
return True
elif self._window.modifiers & Gdk.ModifierType.SHIFT_MASK:
self.new_page(LoadingType.POPOVER)
self.new_page(navigation_uri, LoadingType.POPOVER)
decision.ignore()
return True
else:
App().history.set_page_state(self._navigation_uri)
self.__switch_profile(self._navigation_uri)
App().history.set_page_state(navigation_uri)
self.__switch_profile(navigation_uri)
self._error = False
decision.use()
return False
else:
self.new_page(LoadingType.BACKGROUND)
self.new_page(navigation_uri, LoadingType.BACKGROUND)
decision.ignore()
return True
......@@ -22,6 +22,7 @@ from eolie.extension_forms import FormsExtension
from eolie.extension_adblock import AdblockExtension
from eolie.extension_jsblock import JSblockExtension
from eolie.extension_proxy import ProxyExtension
from eolie.database_adblock import DatabaseAdblock
from eolie.database_exceptions import DatabaseExceptions
from eolie.settings import Settings
......@@ -35,6 +36,7 @@ class Application(Gio.Application):
app.__class__ = Application
app.cursors = {}
app.settings = Settings.new()
app.adblock = DatabaseAdblock()
app.adblock_exceptions = DatabaseExceptions("adblock")
app.js_exceptions = DatabaseExceptions("js")
return app
......
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