Skip to content

Commit

Permalink
Prevent opening links into new window #544.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fanglingsu committed Mar 23, 2019
1 parent bae3e38 commit 6ef95d1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions doc/vimb.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/setting.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
20 changes: 13 additions & 7 deletions tests/manual/window-open.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 6ef95d1

Please sign in to comment.