summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormiguel <miguel@localhost>2018-03-10 19:00:32 +0100
committermiguel <miguel@localhost>2018-03-10 19:00:32 +0100
commit700a87f27c4c5d63b8350d37f06410081951addb (patch)
tree0bb7fa858936b37391fb93058056346ee4c5755d
first commit
-rw-r--r--Makefile23
-rw-r--r--README.md39
-rw-r--r--config.mk11
-rw-r--r--doc/webkitext.ogvbin0 -> 1168975 bytes
-rw-r--r--surf-webext-dom.c160
-rw-r--r--surf.c.diff71
6 files changed, 304 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..51b3267
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+include config.mk
+
+.PHONY: rebuild clean install
+
+surf-webext-dom.so: surf-webext-dom.o
+ @echo CREATING SHARED LIB
+ $(CC) -shared -o surf-webext-dom.so surf-webext-dom.o
+
+surf-webext-dom.o: surf-webext-dom.c
+ @echo COMPILILNG OBJECT
+ $(CC) $(CFLAGS) -fPIC -c surf-webext-dom.c
+
+rebuild:
+ @echo REBUILDING PROJECT
+
+install: surf-webext-dom.so
+ @echo INSTALLING SHARED LIB to $(LIBPREFIX)
+ cp surf-webext-dom.so $(LIBPREFIX)/
+clean:
+ @echo CLEANING UP
+ -rm surf-webext-dom.o
+ -rm surf-webext-dom.so
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ee60c72
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+Miguel's Surfing Quick Links
+============================
+
+Installing this webkit2gtk+ extension
+-------------------------------------
+* READ surf-webext-dom.c !!
+* adapt config.mk
+* make
+* sudo make install
+
+Using it from surf
+------------------
+* adapt surf/config.h and surf/surf.c (see below)
+* recompile and rerun surf
+* have fun!
+
+adapting your surf/config.h
+---------------------------
+Since I also changed my surf to multi-modal I use the number keys straight
+for the quick links like this:
+
+ //domlinks (99=magic number / follow link)
+ { 0, GDK_KEY_0, domlinks, { .i = 0 } },
+ { 0, GDK_KEY_1, domlinks, { .i = 1 } },
+ { 0, GDK_KEY_2, domlinks, { .i = 2 } },
+ { 0, GDK_KEY_3, domlinks, { .i = 3 } },
+ { 0, GDK_KEY_4, domlinks, { .i = 4 } },
+ { 0, GDK_KEY_5, domlinks, { .i = 5 } },
+ { 0, GDK_KEY_6, domlinks, { .i = 6 } },
+ { 0, GDK_KEY_7, domlinks, { .i = 7 } },
+ { 0, GDK_KEY_8, domlinks, { .i = 8 } },
+ { 0, GDK_KEY_9, domlinks, { .i = 9 } },
+ { 0, GDK_KEY_space, domlinks, { .i = 99 } },
+
+
+adapting your surf/surf.c
+---------------------------
+surf.c needs an extra function 'domlinks' and two includes for IPC.
+Consult the surf.c.diff for details
diff --git a/config.mk b/config.mk
new file mode 100644
index 0000000..21ac179
--- /dev/null
+++ b/config.mk
@@ -0,0 +1,11 @@
+VERSION = 0.1
+PREFIX = /usr/local
+LIBPREFIX = $(PREFIX)/lib/surf
+
+CC = cc
+
+EXTINC = `pkg-config --cflags webkit2gtk-web-extension-4.0`
+EXTLIB = `pkg-config --libs webkit2gtk-web-extension-4.0`
+
+CFLAGS = $(EXTINC)
+LDLAGS = -module -avoid-version -no-undefined $(EXTLIB)
diff --git a/doc/webkitext.ogv b/doc/webkitext.ogv
new file mode 100644
index 0000000..26b8012
--- /dev/null
+++ b/doc/webkitext.ogv
Binary files differ
diff --git a/surf-webext-dom.c b/surf-webext-dom.c
new file mode 100644
index 0000000..31e1308
--- /dev/null
+++ b/surf-webext-dom.c
@@ -0,0 +1,160 @@
+//
+// !!! THIS IS A BUGGY CONCEPT/PROTOTYPE. USE AT YOUR OWN RISK !!!
+//
+// The following simple Webkit2GTK+ extension was hacked together
+// in the sleepless night between 09th and 10th March 2018 by:
+//
+// Michal Idziorek <m.i@gmx.at>.
+//
+// It regjsters a document_loaded_callback, where the DOM is manipulated,
+// i.e: a <span> with a number added to every link encountered.
+//
+// The urls are extracted from the DOM and shared with the browsers
+// process over a system V memory segment.
+//
+// As an example, this can be used to enable keyboardless navigation
+// in surf by simply typing in the number of the link.
+//
+// TODO: Support multiple clients! See source for more TODOS!
+// THE biggest limitation by now is the lazily hardcoded:
+// key_t my_ftok = ftok("~/surf-webext-dom-shared-mem",'a');
+//
+// Last Update: 10th March 2018
+//
+// Last Tested: with surf commit 81f0452bc6c2a110239fa303ad1e72f11c33dc94
+// from htps://git.suckless.org/surf
+//
+
+#include <webkit2/webkit-web-extension.h>
+
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <errno.h>
+#include <string.h>
+
+bool str_prefix(const char *str, const char *pre);
+
+void
+document_loaded_callback (WebKitWebPage *web_page,
+ GPtrArray *elements,
+ GVariant *user_data)
+{
+
+ // TODO: g_free where appropriate !!
+ // TODO: allow focus on input boxes/forms
+ // TODO: Put this somehwere else? (but I dislike #defines)
+ // share this info with surf itself! maybe surf can supply this along
+ // the ftok path via the user_data GVariant.
+ int MAX_LINKS=1024;
+ int MAX_LINK_LENGTH=1024;
+ int DEBUG=3;
+
+ // get html doc
+ WebKitDOMHTMLDocument *doc = webkit_web_page_get_dom_document(web_page);
+ if(!WEBKIT_DOM_IS_HTML_DOCUMENT(doc))return;
+
+ // get url
+ char *url = webkit_dom_document_get_url (doc);
+ if(DEBUG>1) g_print("document_loaded (callback): %s\n", url);
+
+ // tokenize url
+ char *url_proto=strtok(url,"/");
+ char *url_host=strtok(NULL,"/");
+ if(DEBUG>2) g_print("proto: %s\n", url_proto);
+ if(DEBUG>2) g_print("host: %s\n", url_host);
+
+ // get html body
+ WebKitDOMHTMLElement *body = webkit_dom_document_get_body(doc);
+
+ // get links (alt)
+ /* altenative way, what is the difference (TODO)?
+ WebKitDOMNodeList *links = webkit_dom_document_query_selector_all(doc,"a",NULL);
+ gulong links_count = webkit_dom_node_list_get_length (links);
+ g_print("extracted %i links\n",links_count);
+ */
+
+ // get links
+ WebKitDOMHTMLCollection *links = webkit_dom_document_get_links(doc);
+ gulong links_count = webkit_dom_html_collection_get_length (links);
+ if(DEBUG>1) g_print("extracted %i links\n",links_count);
+
+ // attach to shared memory;
+ key_t my_ftok = ftok("~/surf-webext-dom-shared-mem",'a');
+
+ // TODO: should we need the size again here?
+ int mem_seg=shmget(my_ftok,MAX_LINKS*MAX_LINK_LENGTH,IPC_CREAT|0660);
+ if(mem_seg==-1)
+ {
+ g_print("shmget failed: %s\n",strerror(errno));
+ }
+
+ char *shared_buf=shmat(mem_seg,NULL,0);
+ if(shared_buf==(void*)-1)
+ {
+ g_print("shmat failed: %s\n",strerror(errno));
+ }
+
+ // iterate over links
+ for(gulong i=0;i<links_count;i++)
+ {
+ char buf[MAX_LINK_LENGTH]; // TODO: dynamic!
+
+ WebKitDOMHTMLLinkElement * node = webkit_dom_html_collection_item (links,i);
+
+ // add hint for keyboard navigation
+ char *txt = webkit_dom_html_element_get_inner_html (node);
+
+ // TODO: style via stylesheet
+ snprintf(buf,MAX_LINK_LENGTH,
+ "<span style='padding-right:5px;border-bottom:1px solid black;'>\
+ <span style='font-size:13px;font-weight:bold;font-face:Arial;\
+ border:1px solid black;display:inline;color:white;\
+ background-color:#c0474c;padding:0 5px;margin:0;\
+ margin-right:5px;'>%i</span>%s</span>", i,txt);
+
+ webkit_dom_html_element_set_inner_html (node,buf,NULL);
+
+ // alternatively add hints at the end of the body
+ // webkit_dom_element_insert_adjacent_html (body,"beforeend",buf,NULL);
+
+ // TODO: this does not work for some sad reason.
+ // sadly, likely due to my misunderstanding.
+ // node seems not to be a LinkElement at all :(
+ // txt = webkit_dom_html_link_element_get_href (node);
+ // so we have to use this:
+ txt = webkit_dom_element_get_attribute (node,"href");
+ if(str_prefix(txt,"//")) snprintf(buf,MAX_LINK_LENGTH,"%s%s",url_proto,txt);
+ else if(str_prefix(txt,"/")) snprintf(buf,MAX_LINK_LENGTH,"%s//%s%s",url_proto,url_host,txt);
+ else snprintf(buf,MAX_LINK_LENGTH,"%s",txt);
+
+ if(i<MAX_LINKS) snprintf(&(shared_buf[MAX_LINK_LENGTH*i]),MAX_LINK_LENGTH,"%s\0",buf);
+ if(DEBUG>10) g_print("extracted link : %s\n",buf);
+ }
+}
+
+static void
+page_created_callback (WebKitWebExtension *extension,
+ WebKitWebPage *web_page,
+ GVariant *user_data)
+{
+ g_signal_connect (web_page, "document-loaded",
+ G_CALLBACK (document_loaded_callback),
+ user_data);
+}
+
+G_MODULE_EXPORT void
+webkit_web_extension_initialize_with_user_data (WebKitWebExtension *extension,
+ GVariant *user_data)
+{
+ g_signal_connect (extension, "page-created",
+ G_CALLBACK (page_created_callback),
+ user_data );
+}
+
+//https://stackoverflow.com/questions/4770985/how-to-check-if-a-string-starts-with-another-string-in-c
+bool str_prefix(const char *str, const char *pre)
+{
+ size_t lenpre = strlen(pre),
+ lenstr = strlen(str);
+ return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
+}
diff --git a/surf.c.diff b/surf.c.diff
new file mode 100644
index 0000000..329df08
--- /dev/null
+++ b/surf.c.diff
@@ -0,0 +1,71 @@
+diff --git a/surf.c b/surf.c
+index f01a91c..76d4706 100644
+--- a/surf.c
++++ b/surf.c
+@@ -2,6 +2,10 @@
+ *
+ * To understand surf, start reading main().
+ */
++
++#include <sys/ipc.h>
++#include <sys/shm.h>
++
+ #include <sys/file.h>
+ #include <sys/types.h>
+ #include <sys/wait.h>
+@@ -223,6 +227,7 @@ static void togglefullscreen(Client *c, const Arg *a);
+ static void togglecookiepolicy(Client *c, const Arg *a);
+ static void toggleinspector(Client *c, const Arg *a);
+ static void find(Client *c, const Arg *a);
++static void domlinks(Client *c, const Arg *a);
+
+ /* Buttons */
+ static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
+@@ -2052,3 +2057,47 @@ main(int argc, char *argv[])
+
+ return 0;
+ }
++
++void
++domlinks(Client *c, const Arg *a)
++{
++ static int goto_link=0;
++ int digit=a->i;
++
++ // follow link
++ if(digit==99)
++ {
++ printf("follow link %i\n",goto_link);
++
++ key_t my_ftok = ftok("~/surf-webext-dom-shared-mem",'a');
++ int mem_seg=shmget(my_ftok,1024*1024,IPC_CREAT|0660);
++ if(mem_seg==-1)
++ {
++ g_print("shmget failed\n");
++ }
++ char *shared_buf=shmat(mem_seg,NULL,0);
++ if(shared_buf==(void*)-1)
++ {
++ g_print("shmat failed\n");
++ }
++
++ printf("linking to : %s\n",&(shared_buf[goto_link*1024]));
++ Arg aa = {.v = &(shared_buf[goto_link*1024]) };
++ loaduri(c, &aa);
++ goto_link=0;
++ return;
++ }
++
++ // adjust link number
++ if(goto_link==0)
++ {
++ goto_link=digit;
++ }
++ else
++ {
++ goto_link*=10;
++ goto_link+=digit;
++ }
++ printf("update link num to: %i\n",goto_link);
++
++}