From: Daniel Carl Date: Sat, 23 Mar 2019 23:53:47 +0000 (+0100) Subject: Prevent opening links into new window #544. X-Git-Url: https://git.owens.tech/editable-focus.html/editable-focus.html/git?a=commitdiff_plain;h=6ef95d13cf1f6313b67cf63acdb7fc07ac02cb2b;p=vimb.git Prevent opening links into new window #544. Added setting 'prevent-newwindow' to enforce opening links into same window even if they are crafted by `target="_blank"` or using `window.open()`. This option does not change the behaviour for links fired by hinting. --- diff --git a/doc/vimb.1 b/doc/vimb.1 index f725e00..b9ad452 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -1234,6 +1234,11 @@ not allow a page to store data in the windows sessionStorage. .B plugins (bool) Determines whether or not plugins on the page are enabled. .TP +.B prevent-newwindow (bool) +Whether to open links, that would normally open in a new window, in the +current window. +This option does not affect links fired by hinting. +.TP .B sans-serif-font (string) The font family used as the default for content using sans-serif font. .TP diff --git a/src/main.c b/src/main.c index 87ba59e..8127235 100644 --- a/src/main.c +++ b/src/main.c @@ -1214,6 +1214,14 @@ static void on_webview_close(WebKitWebView *webview, Client *c) static WebKitWebView *on_webview_create(WebKitWebView *webview, WebKitNavigationAction *navact, Client *c) { + WebKitURIRequest *req; + if (c->config.prevent_newwindow) { + req = webkit_navigation_action_get_request(navact); + vb_load_uri(c, &(Arg){TARGET_CURRENT, (char*)webkit_uri_request_get_uri(req)}); + + return NULL; + } + Client *new = client_new(webview); return new->webview; @@ -1302,7 +1310,12 @@ static void decide_new_window_action(Client *c, WebKitPolicyDecision *dec) * without user gesture. */ if (webkit_navigation_action_is_user_gesture(a)) { req = webkit_navigation_action_get_request(a); - spawn_new_instance(webkit_uri_request_get_uri(req)); + if (c->config.prevent_newwindow) { + /* Load the uri into the browser instance. */ + vb_load_uri(c, &(Arg){TARGET_CURRENT, (char*)webkit_uri_request_get_uri(req)}); + } else { + spawn_new_instance(webkit_uri_request_get_uri(req)); + } } break; diff --git a/src/main.h b/src/main.h index 61478ae..767efb2 100644 --- a/src/main.h +++ b/src/main.h @@ -235,6 +235,7 @@ struct Client { guint scrollstep; gboolean input_autohide; gboolean incsearch; + gboolean prevent_newwindow; guint default_zoom; /* default zoom level in percent */ Shortcut *shortcuts; } config; diff --git a/src/setting.c b/src/setting.c index ea58fc2..51883ee 100644 --- a/src/setting.c +++ b/src/setting.c @@ -114,6 +114,7 @@ void setting_init(Client *c) setting_add(c, "monospace-font-size", TYPE_INTEGER, &i, webkit, 0, "default-monospace-font-size"); setting_add(c, "offline-cache", TYPE_BOOLEAN, &on, webkit, 0, "enable-offline-web-application-cache"); setting_add(c, "plugins", TYPE_BOOLEAN, &on, webkit, 0, "enable-plugins"); + setting_add(c, "prevent-newwindow", TYPE_BOOLEAN, &off, internal, 0, &c->config.prevent_newwindow); setting_add(c, "print-backgrounds", TYPE_BOOLEAN, &on, webkit, 0, "print-backgrounds"); setting_add(c, "private-browsing", TYPE_BOOLEAN, &off, webkit, 0, "enable-private-browsing"); setting_add(c, "sans-serif-font", TYPE_CHAR, &"sans-serif", webkit, 0, "sans-serif-font-family"); diff --git a/tests/manual/window-open.html b/tests/manual/window-open.html index dbf8b64..6a8e81b 100644 --- a/tests/manual/window-open.html +++ b/tests/manual/window-open.html @@ -3,13 +3,19 @@ Window open - target=""
- target="_new"
- target="_blank"
- target iframe
- javascript:window-open
- onclick window-open
- +

+ Enable set prevent-newwindow=on
+ Following link should be opened into current window.

+ target="_new"
+ target="_blank"
+ javascript:window-open
+ onclick window-open
+

+

+ This target iframe link should + load into the iframe.
+ +