0

我正在尝试构建带有 url 栏的浏览器。URL 栏需要反映浏览器中的任何导航变化,文档推荐使用 navigation-policy-decision-requested;回调有 5 个参数,类型为 WebkitWebView、WebKitWebFrame、WebKitNetworkRequest、WebKitNavigationAction、webKitPolicyDecision 和 gpointer。

当我尝试编译时,我收到了几个变体“错误:未知类型名称”的错误。除了 WebkitWebView 之外,所有列出的都会出现这种情况。我知道我包含了 webkit2.h。我知道我尝试过显式定义,例如通过初始化 WebKitWebFrame,但它所做的只是创建了更多错误。类型还没有定义吗?还是我必须自己定义它们?

编辑:我之前测试的计算机上的 Wifi 连接真的很弱,所以我无法提供代码或编译输出。对不起。

主程序

static void uriUpdateCb(WebKitWebView* web_view, WebKitWebFrame* frame, WebkitNetworkRequest* request, WebkitWebNavigationAction* navigation_action, WebkitWebPolicyDecision* policy_decision, gpointer user_data){
  const gchar* uri = webkit_network_request_get_uri(request);
  char* file_type = strchr(uri, '.');
  if(file_type && (!strcmp(file_type, ".pdf") || !strcmp(file_type, ".db") || !strcmp(file_type, ".exe") || !strcmp(file_type, ".deb") || !strcmp(file_type, ".rpm") || !strcmp(file_type, ".dmg"))){
      // note to self: replace above if statement with a loop and a file that contains these extensions
      webkit_web_policy_decision_download(policy_decision);
  }
  else{
    webkit_web_policy_decision_use(policy_decision);
    gtk_entry_set_text(user_data, uri);
  }
}

window.h(控制 webkitWebview)

g_signal_connect(webView, "navigation-policy-decision-requested", G_CALLBACK(uriUpdateCb), url_bar);

callbacks.h(定义)

static void uriUpdateCb(WebKitWebView* web_view, WebKitWebFrame* frame, WebkitNetworkRequest* request, WebkitWebNavigationAction* navigation_action, WebkitWebPolicyDecision* policy_decision, gpointer user_data);

编译错误

In file included from main.c:35:0:
callbacks.h:4:50: error: unknown type name ‘WebKitWebFrame’
 static void uriUpdateCb(WebKitWebView* web_view, WebKitWebFrame* frame, WebkitNet
                                                  ^
callbacks.h:4:73: error: unknown type name ‘WebkitNetworkRequest’
 atic void uriUpdateCb(WebKitWebView* web_view, WebKitWebFrame* frame, WebkitNetwo
                                                                       ^
callbacks.h:4:104: error: unknown type name ‘WebkitWebNavigationAction’
 View* web_view, WebKitWebFrame* frame, WebkitNetworkRequest* request, WebkitWebNa
                                                                       ^
In file included from main.c:35:0:
callbacks.h:4:150: error: unknown type name ‘WebkitWebPolicyDecision’
 etworkRequest* request, WebkitWebNavigationAction* navigation_action, WebkitWebPo
                                                                       ^
In file included from /usr/include/glib-2.0/gobject/gobject.h:28:0,
                 from /usr/include/glib-2.0/gobject/gbinding.h:29,
                 from /usr/include/glib-2.0/glib-object.h:23,
                 from /usr/include/glib-2.0/gio/gioenums.h:28,
                 from /usr/include/glib-2.0/gio/giotypes.h:28,
                 from /usr/include/glib-2.0/gio/gio.h:26,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from main.c:29:
window.h: In function ‘create_window’:
window.h:56:81: error: ‘uriUpdateCb’ undeclared (first use in this function)
 l_connect(webView, "navigation-policy-decision-requested", G_CALLBACK(uriUpdateCb
                                                                       ^
/usr/include/glib-2.0/gobject/gsignal.h:475:60: note: in definition of macro ‘g_signal_connect’
     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NU
                                                            ^
window.h:56:70: note: in expansion of macro ‘G_CALLBACK’
    g_signal_connect(webView, "navigation-policy-decision-requested", G_CALLBACK(u
                                                                      ^
window.h:56:81: note: each undeclared identifier is reported only once for each function it appears in
 l_connect(webView, "navigation-policy-decision-requested", G_CALLBACK(uriUpdateCb
                                                                       ^
/usr/include/glib-2.0/gobject/gsignal.h:475:60: note: in definition of macro ‘g_signal_connect’
     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NU
#include <gtk/gtk.h>
#include <string.h>
#include <gtk/gtk.h>
#include <string.h>
#include <stdio.h>
#include <webkit2/webkit2.h>

// window.h
int create_window(){

  // Create an 800x600 window that will contain the browser instance
  GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(GTK_WINDOW(main_window), 800, 600);

  // create the tab manager
  GtkWidget *notebook = gtk_notebook_new();

  GtkWidget *label = gtk_label_new ("test");

  // create the gtk box that'll set the layout and put box in window
  GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);


  gtk_notebook_append_page(GTK_NOTEBOOK (notebook), box, label);


  gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(notebook));

  GtkWidget *grid = gtk_grid_new();
  gtk_container_add(GTK_CONTAINER(box), GTK_WIDGET(grid));

  // create url_bar and add to grid

  GtkWidget *back_button = gtk_button_new_with_label("Back");
  GtkWidget *forward_button = gtk_button_new_with_label("Forward");
  GtkWidget *stop_connection_button = gtk_button_new_with_label("Stop");

  GtkEntryBuffer *buf = gtk_entry_buffer_new("about:blank", 12);
  GtkWidget *url_bar = gtk_entry_new_with_buffer(buf);

  gtk_grid_attach(GTK_GRID(grid), back_button, 0, 0, 1, 1);
  gtk_grid_attach(GTK_GRID(grid), forward_button, 1, 0, 1, 1);
                                                            ^
window.h:56:70: note: in expansion of macro ‘G_CALLBACK’
    g_signal_connect(webView, "navigation-policy-decision-requested", G_CALLBACK(u
                                                                      ^
main.c: At top level:
main.c:107:50: error: unknown type name ‘WebKitWebFrame’
 static void uriUpdateCb(WebKitWebView* web_view, WebKitWebFrame* frame, WebkitNet
                                                  ^
main.c:107:73: error: unknown type name ‘WebkitNetworkRequest’
 atic void uriUpdateCb(WebKitWebView* web_view, WebKitWebFrame* frame, WebkitNetwo
                                                                       ^
main.c:107:104: error: unknown type name ‘WebkitWebNavigationAction’
 View* web_view, WebKitWebFrame* frame, WebkitNetworkRequest* request, WebkitWebNa
                                                                       ^
main.c:107:150: error: unknown type name ‘WebkitWebPolicyDecision’
 etworkRequest* request, WebkitWebNavigationAction* navigation_action, WebkitWebPo
4

1 回答 1

2

WebKitWebFrame 和 WebKitNetworkRequest 是来自过时的 webkitgtk-1.0 和 webkitgtk-3.0 API 的类型,但是由于您包含 webkit2.h,因此很明显您正在尝试使用现代 webkit2gtk-4.0 API(应该如此)。您可能正在查看旧 API 的文档。这是现代文档。

您无需听取导航策略决策即可获取 URL。只需通过连接到 notify::uri 信号来查找 WebKitWebView::uri 的属性更改。如果您不确定如何执行此操作,请查看 g_signal_connect() 的文档。

于 2018-03-04T03:57:31.110 回答