[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
From: |
Gavin D. Smith |
Date: |
Sun, 20 Mar 2022 04:55:00 -0400 (EDT) |
branch: master
commit 974257eff41d5c82b6b7ceb97acfa4c22f480fd4
Author: Gavin Smith <gavinsmith0123@gmail.com>
AuthorDate: Thu May 9 18:32:32 2019 +0100
get index data
---
js/wkinfo/Makefile.am | 4 +-
js/wkinfo/common.h | 1 +
js/wkinfo/extension.c | 107 ++++++++++++++++++++++++++++++++++++++++++-
js/wkinfo/main.c | 123 ++++++++++++++++++++++++++++++++------------------
4 files changed, 189 insertions(+), 46 deletions(-)
diff --git a/js/wkinfo/Makefile.am b/js/wkinfo/Makefile.am
index 8bccc6d715..3d98a8185b 100644
--- a/js/wkinfo/Makefile.am
+++ b/js/wkinfo/Makefile.am
@@ -3,14 +3,14 @@ ACLOCAL_AMFLAGS = -I m4
webextension_LTLIBRARIES = libmyappwebextension.la
webextensiondir = $(libdir)/MyApp/web-extension
-libmyappwebextension_la_SOURCES = extension.c
+libmyappwebextension_la_SOURCES = extension.c common.h
libmyappwebextension_la_CFLAGS = $(AM_CFLAGS) $(WEB_EXTENSION_CFLAGS)
libmyappwebextension_la_LIBADD = $(WEB_EXTENSION_LIBS)
libmyappwebextension_la_LDFLAGS = -module -avoid-version -no-undefined
bin_PROGRAMS=webkit
-webkit_SOURCES=main.c
+webkit_SOURCES=main.c common.h
AM_CFLAGS=`pkg-config --cflags gtk+-3.0 webkit2gtk-3.0`
diff --git a/js/wkinfo/common.h b/js/wkinfo/common.h
new file mode 100644
index 0000000000..40cbda700a
--- /dev/null
+++ b/js/wkinfo/common.h
@@ -0,0 +1 @@
+#define PACKET_SIZE 1000
diff --git a/js/wkinfo/extension.c b/js/wkinfo/extension.c
index 9f14ca425a..0a208ff226 100644
--- a/js/wkinfo/extension.c
+++ b/js/wkinfo/extension.c
@@ -8,9 +8,12 @@
#include <sys/socket.h>
#include <sys/un.h>
+#include <glib.h>
#include <gtk/gtk.h>
#include <webkit2/webkit-web-extension.h>
+#include "common.h"
+
/* For communicating with the main Gtk process */
static struct sockaddr_un main_name;
static size_t main_name_size;
@@ -27,6 +30,8 @@ remove_our_socket (void)
unlink (our_socket_file);
}
+static int send_index_p = 0;
+
gboolean
request_callback (WebKitWebPage *web_page,
WebKitURIRequest *request,
@@ -35,13 +40,107 @@ request_callback (WebKitWebPage *web_page,
{
const char *uri = webkit_uri_request_get_uri (request);
- // g_print ("Intercepting link <%s>\n", uri);
+ g_print ("Intercepting link <%s>\n", uri);
+
+ send_index_p = 0;
+
+ const char *p = strchr (uri, '?');
+ if (p)
+ {
+ char *new_uri = strdup (uri);
+ new_uri[p - uri] = 0;
+ webkit_uri_request_set_uri (request, new_uri);
+ g_print ("new_uri %s\n", new_uri);
+ free (new_uri);
+
+ p++;
+ g_print ("request type %s\n", p);
+ if (!strcmp (p, "send-index"))
+ send_index_p = 1;
+
+ }
/* Could block external links here */
return false;
}
+void
+send_datagram (GString *s)
+{
+ ssize_t result;
+ result = sendto (socket_id, s->str, s->len + 1, 0,
+ (struct sockaddr *) &main_name, main_name_size);
+
+ if (result == -1)
+ {
+ g_print ("sending datagram failed: %s\n",
+ strerror(errno));
+ }
+}
+
+void
+send_index (WebKitDOMHTMLCollection *links, gulong num_links)
+{
+ g_print ("trying to send index\n");
+
+ gulong i = 0;
+ GString *s = g_string_new (NULL);
+
+ /* Break index information up into datagrams each of size less
+ than PACKET_SIZE. */
+
+ g_string_assign (s, "index\n");
+
+ for (; i < num_links; i++)
+ {
+ WebKitDOMNode *node
+ = webkit_dom_html_collection_item (links, i);
+ if (!node)
+ {
+ g_print ("No node\n");
+ return;
+ }
+
+ WebKitDOMElement *element;
+ if (WEBKIT_DOM_IS_ELEMENT(node))
+ {
+ element = WEBKIT_DOM_ELEMENT(node);
+ }
+ else
+ {
+ /* When would this happen? */
+ g_print ("Not an DOM element\n");
+ continue;
+ }
+
+ gchar *href = webkit_dom_element_get_attribute (element, "href");
+ if (href)
+ {
+ int try = 0;
+ gsize old_len = s->len;
+ do
+ {
+ g_string_append (s, href);
+ g_string_append (s, "\n");
+ if (s->len > PACKET_SIZE && try != 1)
+ {
+ g_string_truncate (s, old_len);
+ g_print ("sending packet %u||%s||\n", s->len, s->str);
+ send_datagram (s);
+ g_string_assign (s, "index\n");
+ try++;
+ continue;
+ }
+ }
+ while (0);
+ }
+ }
+ send_datagram (s);
+
+ g_string_free (s, TRUE);
+}
+
void
document_loaded_callback (WebKitWebPage *web_page,
gpointer user_data)
@@ -72,6 +171,12 @@ document_loaded_callback (WebKitWebPage *web_page,
g_print ("Found %d links\n",
webkit_dom_html_collection_get_length (links));
+ if (send_index_p)
+ {
+ send_index (links, num_links);
+ return;
+ }
+
gulong i;
for (i = 0; i < num_links; i++)
{
diff --git a/js/wkinfo/main.c b/js/wkinfo/main.c
index c73b498352..68f7f127be 100644
--- a/js/wkinfo/main.c
+++ b/js/wkinfo/main.c
@@ -6,11 +6,32 @@
#include <sys/socket.h>
#include <sys/un.h>
+#include <time.h>
+#include <stdarg.h>
+
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
#include <webkit2/webkit2.h>
+#include "common.h"
+
+void
+vmsg (char *fmt, va_list v)
+{
+ vprintf (fmt, v);
+}
+
+void
+msg (char *fmt, ...)
+{
+ va_list v;
+ va_start (v, fmt);
+ printf ("%lu: ", clock ());
+ vmsg (fmt, v);
+ va_end (v);
+}
+
static void destroyWindowCb(GtkWidget *widget, GtkWidget *window);
static gboolean closeWebViewCb(WebKitWebView* webView, GtkWidget* window);
static gboolean onkeypress(GtkWidget *webView,
@@ -37,7 +58,7 @@ socket_cb (GSocket *socket,
GIOCondition condition,
gpointer user_data)
{
- static char buffer[256];
+ static char buffer[PACKET_SIZE+1];
GError *err = 0;
gssize result;
@@ -51,7 +72,7 @@ socket_cb (GSocket *socket,
gtk_main_quit ();
}
- buffer[255] = '\0';
+ buffer[PACKET_SIZE] = '\0';
g_print ("Received le data: <%s>\n", buffer);
char *p, *q;
@@ -102,40 +123,39 @@ initialize_web_extensions (WebKitWebContext *context,
{
/* Make a Unix domain socket for communication with the browser process.
Some example code and documentation for WebKitGTK uses dbus instead. */
-
if (!socket_file)
- socket_file = tmpnam (0);
- else
- g_print ("bug: more than one web process started\n");
+ {
+ socket_file = tmpnam (0);
- GError *err = 0;
+ GError *err = 0;
- err = 0;
- GSocket *gsocket = g_socket_new (G_SOCKET_FAMILY_UNIX,
- G_SOCKET_TYPE_DATAGRAM,
- 0,
- &err);
- if (!gsocket)
- {
- g_print ("no socket: %s\n", err->message);
- gtk_main_quit ();
- }
+ err = 0;
+ GSocket *gsocket = g_socket_new (G_SOCKET_FAMILY_UNIX,
+ G_SOCKET_TYPE_DATAGRAM,
+ 0,
+ &err);
+ if (!gsocket)
+ {
+ g_print ("no socket: %s\n", err->message);
+ gtk_main_quit ();
+ }
- err = 0;
- GSocketAddress *address = g_unix_socket_address_new (socket_file);
- g_socket_bind (gsocket, address, FALSE, &err);
- if (!gsocket)
- {
- g_print ("bind socket: %s\n", err->message);
- gtk_main_quit ();
- }
- err = 0;
+ err = 0;
+ GSocketAddress *address = g_unix_socket_address_new (socket_file);
+ g_socket_bind (gsocket, address, FALSE, &err);
+ if (!gsocket)
+ {
+ g_print ("bind socket: %s\n", err->message);
+ gtk_main_quit ();
+ }
+ err = 0;
- GSource *gsource = g_socket_create_source (gsocket, G_IO_IN, NULL);
- g_source_set_callback (gsource, (GSourceFunc)(socket_cb), NULL, NULL);
- g_source_attach (gsource, NULL);
+ GSource *gsource = g_socket_create_source (gsocket, G_IO_IN, NULL);
+ g_source_set_callback (gsource, (GSourceFunc)(socket_cb), NULL, NULL);
+ g_source_attach (gsource, NULL);
- atexit (&remove_socket);
+ atexit (&remove_socket);
+ }
webkit_web_context_set_web_extensions_directory (
context, WEB_EXTENSIONS_DIRECTORY);
@@ -149,9 +169,21 @@ int main(int argc, char* argv[])
// Initialize GTK+
gtk_init(&argc, &argv);
- // 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);
+ msg ("started\n");
+
+
+ webkit_web_context_set_process_model (
+ webkit_web_context_get_default (),
+ WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
+
+ WebKitWebViewGroup *group = webkit_web_view_group_new (NULL);
+
+ /* Disable JavaScript */
+ WebKitSettings *settings = webkit_web_view_group_get_settings (group);
+ webkit_settings_set_enable_javascript (settings, FALSE);
+
+ webkit_web_context_get_default ();
+
/* Load "extensions". The web browser is run in a separate process
and we can only access the DOM in that process. */
@@ -160,16 +192,20 @@ int main(int argc, char* argv[])
G_CALLBACK (initialize_web_extensions),
NULL);
- // Create a browser instance
- WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
+ WebKitWebView *webView = WEBKIT_WEB_VIEW(
+ webkit_web_view_new_with_group(group));
- /* Disable JavaScript */
- WebKitSettings *settings = webkit_web_view_group_get_settings
(webkit_web_view_get_group (webView));
- webkit_settings_set_enable_javascript (settings, FALSE);
+ // 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 a web view to parse index files. */
+ WebKitWebView *hiddenWebView = WEBKIT_WEB_VIEW(webkit_web_view_new());
// Put the browser area into the main window
gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(webView));
+ // Make sure the main window and all its contents are visible
+ gtk_widget_show_all (main_window);
// Set up callbacks so that if either the main window or the browser
// instance is closed, the program will exit.
@@ -178,16 +214,17 @@ int main(int argc, char* argv[])
g_signal_connect(webView, "key_press_event", G_CALLBACK(onkeypress),
main_window);
- // Load a web page into the browser instance
- webkit_web_view_load_uri (webView,
- "file:/home/g/src/texinfo/GIT/js/test/hello/index.html");
+ webkit_web_view_load_uri (hiddenWebView,
+ "file:/home/g/src/texinfo/GIT/js/test/elisp/Index.html?send-index");
// Make sure that when the browser area becomes visible, it will get mouse
// and keyboard events
gtk_widget_grab_focus (GTK_WIDGET(webView));
- // Make sure the main window and all its contents are visible
- gtk_widget_show_all (main_window);
+
+ webkit_web_view_load_uri (webView,
+ "file:/home/g/src/texinfo/GIT/js/test/hello/index.html");
+ //"file:/home/g/src/texinfo/GIT/js/wkinfo/test.html");
// Run the main GTK+ event loop
gtk_main ();
- master updated (cd0de30b76 -> ca0de032c7), Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject],
Gavin D. Smith <=
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20