0

simple code:

((WebKit.DOM.EventTarget) img).add_event_listener_with_closure ("click", on_enter, false);

void on_enter() {

}

valac error:

error: Argument 2: Cannot convert from `on_enter' to `GLib.Closure'

Here is the full code example:

private void on_click(WebKit.DOM.EventTarget target, WebKit.DOM.Event event)
{
  try
  {
    //var image = (WebKit.DOM.HTMLImageElement)target;
    //image.set_attribute("class", "clickable-img-hover");
  }
  catch(GLib.Error e)
  {
  }
}

public static void webkit_web_extension_initialize (WebKit.WebExtension web_extension)
{
  string PROG = "[webkit2_webextension_minimal_so.so] ";    

  print(PROG + "PLUGIN activated\n");

  web_extension.page_created.connect((extension, web_page)=> {
      print(PROG + "SIGNAL: page_created\n");
      try {
        var dom = web_page.get_dom_document();

        WebKit.DOM.Element img = dom.create_element("img");
        img.set_attribute("src", "file:///home/o/avatar.png");
        img.set_attribute("alt", "avatar-1.jpg");
        ((WebKit.DOM.EventTarget) img).add_event_listener_with_closure("click", on_click, false);
        dom.body.insert_before(img, null);
      } catch (FileError e) {
        stdout.printf ("Message: \"%s\"\n", e.message);
            stdout.printf ("Error code: FileError.EXIST = %d\n", e.code);
            stdout.printf ("FileErrors identification: %" + uint32.FORMAT + "\n", e.domain);
        }

      web_page.send_request.connect((request)=> {
          if(request.uri.has_prefix("http://www.google") || request.uri.has_prefix("data")){
            print(PROG + "SIGNAL: send request [REJECTED] " + request.uri + "\n");
            return true;  // true => block query
          }
          print(PROG + "SIGNAL: send request " + request.uri + "\n");
          return false;
        });

      return;
    });
}

Compile with:

valac --pkg glib-2.0 --pkg webkit2gtk-web-extension-4.0 --library=webext webext.vala -H webext.h -X -fPIC -X -shared -o webext.so

How to add event listener?

PS: This is a follow up question to this earlier question.

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
J.Jak
  • 9
  • 1
  • 4

1 Answers1

1

Looking at the documentation for the method:

public bool add_event_listener_with_closure (
    string event_name,
    owned EventTargetFunc handler,
    bool use_capture
) 

The second argument is owned EventTargetFunc handler.

So looking at the documentation of the delegate type:

public delegate void EventTargetFunc (EventTarget target, Event event)

Your method does not have this delgate type.

So you probably have to fix your method signature to something like:

void on_enter (EventTarget target, Event event) {

}
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
  • delegate void on_enter(WebKit.DOM.Element element, WebKit.DOM.Event event); and ((WebKit.DOM.EventTarget) img).add_event_listener_with_closure("click", on_enter()); result: > error: invocation not supported in this context – J.Jak Apr 13 '17 at 15:47
  • 1
    Two things. First, EventTargetFunc is already defined, it is a function signature that any delegate has to match. `delegate void on_enter(...);` is creating a new delegate type `on_enter`. That's not what you want to do, use the existing type. Second, `on_enter()` invokes (or calls) the delegate. You just want to pass the function so it can be invoked later, so just use `on_enter` without the parentheses. – AlThomas Apr 13 '17 at 18:16
  • For the `invocation not supported` error, see also this question: https://stackoverflow.com/questions/43187122/how-do-i-connect-a-custom-function-to-the-clicked-action-of-a-gtk-button – Jens Mühlenhoff Apr 13 '17 at 20:21
  • Ok, again: delegate void on_enter(); on_enter test = () => {} ((WebKit.DOM.EventTarget) img).add_event_listener_with_closure("click", test); return valac: error: Argument 2: Cannot convert from `on_enter' to `GLib.Closure' I just want to call a function when I click on an element. Change the old version: ((WebKit.DOM.EventTarget) el).add_event_listener("dblclick", (Callback) on_enter, false, this); – J.Jak Apr 14 '17 at 08:43
  • Jens advice is to change your simple code from `((WebKit.DOM.EventTarget) img).add_event_listener_with_closure ("click", on_enter, false); void on_enter() {}` to `((WebKit.DOM.EventTarget) img).add_event_listener_with_closure ("click", on_enter, false); void on_enter(EventTarget target, Event event) {}` – AlThomas Apr 14 '17 at 09:06
  • return: error: Argument 2: Cannot convert from `on_enter' to `GLib.Closure' – J.Jak Apr 14 '17 at 09:28
  • Here are a couple of examples: [WebKit Javascript extension](https://gist.github.com/geoffjay/dc84e642d412f403b33ae666812761fa#file-extension-vala-L90) and [FeedReader/WebExtension/webextension.vala](https://github.com/jangernert/FeedReader/blob/master/WebExtension/webextension.vala#L81) Looking at those you should probably use `((WebKit.DOM.EventTarget) img).add_event_listener_with_closure ("click", on_enter, false); void on_enter(WebKit.DOM.EventTarget target, WebKit.DOM.Event event) {}` – AlThomas Apr 14 '17 at 10:10
  • Yes, I do exactly the same thing... https://pastebin.com/jnH2jtvn valac --pkg glib-2.0 --pkg webkit2gtk-web-extension-4.0 --library=webext webext.vala -H webext.h -X -fPIC -X -shared -o webext.so compile error: error: Argument 2: Cannot convert from `on_click' to `GLib.Closure' – J.Jak Apr 14 '17 at 10:47
  • Your pastebin example compiles fine for me with both Vala 0.30.2 and Vala compiled from master – AlThomas Apr 14 '17 at 10:56
  • A saw a similar example on SO, where the OP was explicitly casting the event handler (like `(WebKit.DOM.EventTarget) img).add_event_listener_with_closure ("click", (Closure) on_enter, false);`). Maybe this was necessary in some older Vala version? – Jens Mühlenhoff Apr 14 '17 at 13:23
  • Ah I see now, that this was your original question of which this one here is a follow up. I'm going to add this to the question. – Jens Mühlenhoff Apr 14 '17 at 13:25
  • Please tell us which version of the Vala compiler you are using. – Jens Mühlenhoff Apr 14 '17 at 13:54