Prevent opening links into new window #544.
authorDaniel Carl <danielcarl@gmx.de>
Sat, 23 Mar 2019 23:53:47 +0000 (00:53 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 23 Mar 2019 23:57:34 +0000 (00:57 +0100)
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.

doc/vimb.1
src/main.c
src/main.h
src/setting.c
tests/manual/window-open.html

index f725e00..b9ad452 100644 (file)
@@ -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
index 87ba59e..8127235 100644 (file)
@@ -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;
 
index 61478ae..767efb2 100644 (file)
@@ -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;
index ea58fc2..51883ee 100644 (file)
@@ -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");
index dbf8b64..6a8e81b 100644 (file)
@@ -3,13 +3,19 @@
 <title>Window open</title>
 </head>
 <body>
-    <a href="./dummy.html" target="">target=""</a><br/>
-    <a href="./dummy.html" target="_new">target="_new"</a><br/>
-    <a href="./dummy.html" target="_blank">target="_blank"</a><br/>
-    <a href="./dummy.html" target="foo">target iframe</a><br/>
-    <a href="javascript:window.open('./dummy.html', 'winname')">javascript:window-open</a><br/>
-    <a href="#" onclick="window.open('./dummy.html', 'winname')">onclick window-open</a><br/>
-    <iframe src="" name="foo"></iframe>
+    <p>
+        Enable <code>set prevent-newwindow=on</code><br/>
+        Following link should be opened into current window.<br/><br/>
+        <a href="./dummy.html" target="_new">target="_new"</a><br/>
+        <a href="./dummy.html" target="_blank">target="_blank"</a><br/>
+        <a href="javascript:window.open('./dummy.html', 'winname')">javascript:window-open</a><br/>
+        <a href="#" onclick="window.open('./dummy.html', 'winname')">onclick window-open</a><br/>
+    </p>
+    <p>
+        This <a href="./dummy.html" target="foo">target iframe link</a> should
+        load into the iframe.<br/>
+        <iframe src="" name="foo"></iframe>
+    </p>
 </body>
 </html>