我正在尝试使用网址栏构建浏览器。 URL栏需要反映浏览器中的任何导航更改,文档建议使用navigation-policy-decision-requested;回调需要5个参数,类型为WebkitWebView,WebKitWebFrame,WebKitNetworkRequest,WebKitNavigationAction,webKitPolicyDecision和gpointer。
当我尝试编译时,我得到了变体“error:unknown type name”的几个错误。除了WebkitWebView之外,所有列出的都会发生这种情况。我知道我包含了webkit2.h。我知道我尝试通过初始化WebKitWebFrame来明确定义,但它所做的就是创建更多错误。这些类型尚未定义吗?或者我必须自己定义它们?
编辑:我之前测试的计算机上的Wifi连接非常弱,所以我无法提供代码或编译输出。遗憾。的main.c
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
答案 0 :(得分:1)
WebKitWebFrame和WebKitNetworkRequest是来自过时的webkitgtk-1.0和webkitgtk-3.0 API的类型,但由于您包含webkit2.h,很明显您正在尝试使用现代的webkit2gtk-4.0 API(如您所愿)。您可能正在查看旧API的文档。 Here is the modern documentation.
您无需听取导航政策决定即可获取网址。只需通过连接到notify :: uri信号来查找对WebKitWebView :: uri的属性更改。如果您不确定如何执行此操作,请查找g_signal_connect()的文档。