bul
bul

Reputation: 41

Intercept click on link in webkit.webview

In an HTML page, by clicking on a link, the page HTML given is displayed.

But how to make for

open this page in a another tab, with another program?

If the link points to another thing that one .htm[l], php… (zip, iso, etc.) how to detect and launch the download?

It would be necessary to detect the click on one link, to launch the adequate treatment, and I do not see how to make,

I do not find examples (with vala).

Example: test.vala:

using Gtk;
using WebKit;
/* 
valac --pkg gtk+-3.0 --pkg webkit2gtk-4.0 --pkg posix test.vala && ./test 
*/
int main (string[] args) {
    Gtk.init(ref args);
    var window=new Gtk.Window();
    window.destroy.connect(Gtk.main_quit);
    WebView wv=new WebKit.WebView();
    string url="file://"+Posix.realpath(args[0])+".html";
    wv.load_uri(url);
    window.add(wv);
    window.show_all();
    Gtk.main();
    return 0;
}

test.html:

<html>
    <body>
        <a href='https://searx.aquilenet.fr'>searx</a>
        <a href='https://launchpad.net/xpad/trunk/5.0.0/+download/xpad-5.0.0.tar.bz2'>xpad</a>
    </body>
</html>

Upvotes: 0

Views: 664

Answers (2)

bul
bul

Reputation: 41

it goes :

wv.decide_policy.connect((policy, type) => {
    if (type == WebKit.PolicyDecisionType.NAVIGATION_ACTION ) {
        WebKit.NavigationPolicyDecision nav_policy =
            (WebKit.NavigationPolicyDecision) policy;
        if (nav_policy.get_navigation_type() ==
                WebKit.NavigationType.LINK_CLICKED) {
            string href = nav_policy.request.uri;
GLib.stdout.printf("%s\n",href);
        } }
    return true;
});

thank you very much

Upvotes: 0

Michael Gratton
Michael Gratton

Reputation: 516

Connect to the decide-policy signal on your WebView instance, and examine the two objects passed in:

wv.decide_policy.connect((policy, type) => {
    if (type == WebKit.PolicyDecisionType.NAVIGATION_ACTION &&)
        WebKit.NavigationPolicyDecision nav_policy =
            (WebKit.NavigationPolicyDecision) policy;
        if (nav_policy.get_navigation_type() ==
                WebKit.NavigationType.LINK_CLICKED) {
            string href = nav_policy.request.uri;
            // Do something with the href
        }
});

This is how you handle new links, new windows, display or save a response, etc. See the docs for the signal and for the two parameters WebKit.PolicyDecision and WebKit.PolicyDecisionType.

Upvotes: 0

Related Questions