Skip to content

Commit c55bdd4

Browse files
committed
ensure only links from user gestures open in webbrowser
1 parent ef8af2a commit c55bdd4

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

devdocs_desktop.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ def __init__(self):
4949
self.args = argparse.ArgumentParser(prog='devdocs-desktop')
5050
self.args.add_argument('s', metavar='STR', help='the string to search', nargs='?', default='')
5151

52-
self.app_url = 'https://devdocs.io'
53-
self.search = self.args.parse_args().s.strip()
54-
self.filter = ''
55-
self.options = self.read_settings_json('cookies')
56-
self.prefs = self.read_settings_json('prefs')
57-
self.globals = Gtk.Settings.get_default()
52+
self.app_url = 'https://devdocs.io'
53+
self.search = self.args.parse_args().s.strip()
54+
self.filter = ''
55+
self.open_link = False
56+
self.options = self.read_settings_json('cookies')
57+
self.prefs = self.read_settings_json('prefs')
58+
self.globals = Gtk.Settings.get_default()
5859

5960
self.main = Gtk.Builder()
6061
self.main.add_from_file(self.file_path('ui/main.ui'))
@@ -424,8 +425,11 @@ def on_webview_decide_policy(self, _widget, decision, dtype):
424425
if dtype == WebKit2.PolicyDecisionType.NAVIGATION_ACTION:
425426
nav = decision.get_navigation_action()
426427
uri = nav.get_request().get_uri()
428+
usr = self.open_link or nav.is_user_gesture()
427429

428-
if not uri.startswith(self.app_url):
430+
self.open_link = False
431+
432+
if usr and not uri.startswith(self.app_url):
429433
decision.ignore()
430434
webbrowser.open(uri)
431435

@@ -446,13 +450,20 @@ def on_history_changed(self, _list, _added, _removed):
446450
forward = self.webview.can_go_forward()
447451
self.header_forward.set_sensitive(forward)
448452

453+
def on_webview_open_link(self, _action, _variant):
454+
self.open_link = True
455+
449456
def on_webview_context_menu(self, _widget, menu, _coords, _keyboard):
450457
for item in menu.get_items():
451458
action = item.get_stock_action()
452459

453460
if not item.is_separator() and not action in CTX_MENU:
454461
menu.remove(item)
455462

463+
if action == WebKit2.ContextMenuAction.OPEN_LINK:
464+
gaction = item.get_gaction()
465+
gaction.connect('activate', self.on_webview_open_link)
466+
456467

457468
class DevdocsDesktopService(dbus.service.Object):
458469

0 commit comments

Comments
 (0)