gtkgjswebkitgtk

How to add menu items to WebKitContextMenu (javascript, gjs)?


When I try to add menuitems to a webview with this code:

my_webview.connect('context-menu', Lang.bind(this, function(webview, c_menu, event, hit_test){
      var action = new Gtk.Action({name:"some_name", label:"Some Label"});
      action.connect('activate', Lang.bind(this, function(){
          print("Your Menu Item !");
      }))

      var m_item = new WebKit.ContextMenuItem(action);
      c_menu.append(m_item);
      m_item.show();
      return false;
  }));

the app exit with the error "Segmentation fault (core dumped)" What's the correct way to add a menuitem to the context menu of a WebView ?


Solution

  • Small correction:

    var m_item = WebKit.ContextMenuItem.new(action);
    

    This is confusing to say the least, it's because WebKit.ContextMenuItem has a custom constructor and not the usual GObject constructor that would be invoked with new WebKit.ContextMenuItem. The segmentation fault is due to WebKit choking when its context menu item doesn't receive an action; however, it would be good if GJS at least warned you what was going on here.

    I've opened a bug report for GJS here.