summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormiguel <miguel@localhost>2018-03-14 19:37:33 +0100
committermiguel <miguel@localhost>2018-03-14 19:37:33 +0100
commit7019970b24380cd823beb9760f34a6c00b569054 (patch)
tree36f80cbb98307affc330e37305392d5c714a1006
parente35799ee201af5d1fe734598f1ef9509d441fc9f (diff)
using gnome loop and changing to fifo pipes
-rw-r--r--Makefile7
-rw-r--r--surf-webext-dom.c99
2 files changed, 60 insertions, 46 deletions
diff --git a/Makefile b/Makefile
index 51b3267..f68e56c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,10 @@
+#https://csclub.uwaterloo.ca/~jy2wong/jenerated/blog/2014-12-02.webkit2_webextensions.html
+#http://rvr.typepad.com/wind/2011/10/webkit-extending-javascript-1.html
+#https://github.com/vain/lariza
+#https://github.com/rschroll/webkitdom
+#https://github.com/jun7/wyeb
+
+
include config.mk
.PHONY: rebuild clean install
diff --git a/surf-webext-dom.c b/surf-webext-dom.c
index 2f18806..9d25704 100644
--- a/surf-webext-dom.c
+++ b/surf-webext-dom.c
@@ -2,7 +2,8 @@
// !!! 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:
+// in the sleepless night between 09th and 10th March 2018 (and later
+// refined) by:
//
// Michal Idziorek <m.i@gmx.at>.
//
@@ -19,7 +20,7 @@
// 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 Update: 14th March 2018
//
// Last Tested: with surf commit 81f0452bc6c2a110239fa303ad1e72f11c33dc94
// from htps://git.suckless.org/surf
@@ -33,22 +34,11 @@
#include <string.h>
#include <pthread.h>
-
bool str_prefix(const char *str, const char *pre);
static int call=0;
-
-void* perform_work(void* argument) {
- int passed_in_value;
-
- passed_in_value = *((int*) argument);
- for(int i=0;i<10;i++)
- {
- printf("Hello World! It's me, thread with argument %d!\n", passed_in_value);
- sleep(1);
- }
- return NULL;
-}
+static char *shared_buf;
+WebKitDOMHTMLDocument *my_doc;
void
document_loaded_callback (WebKitWebPage *web_page,
@@ -56,9 +46,16 @@ document_loaded_callback (WebKitWebPage *web_page,
GVariant *user_data)
{
- pthread_t thread;
- g_print("called counter: %i\n",call++);
- pthread_create(&thread, NULL, perform_work, &call);
+ my_doc = webkit_web_page_get_dom_document(web_page);
+// if(!WEBKIT_DOM_IS_HTML_DOCUMENT(my_doc))return;
+
+// GThread *t1 = g_thread_new ("show hints", show_hints, doc);
+ // g_thread_join(t1);
+}
+
+GThreadFunc
+show_hints(WebKitDOMHTMLDocument *doc)
+{
// TODO: g_free where appropriate !!
// TODO: allow focus on input boxes/forms
@@ -67,12 +64,7 @@ document_loaded_callback (WebKitWebPage *web_page,
// 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;
+ int DEBUG=9;
// get url
char *url = webkit_dom_document_get_url (doc);
@@ -88,41 +80,28 @@ document_loaded_callback (WebKitWebPage *web_page,
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);
+ WebKitDOMHTMLElement * node = webkit_dom_node_list_item (links,i);
// add hint for keyboard navigation
+
+ //g_print("node address: %p\n", node);
+
char *txt = webkit_dom_html_element_get_inner_html (node);
// TODO: style via stylesheet
@@ -130,20 +109,23 @@ document_loaded_callback (WebKitWebPage *web_page,
"<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;\
+ background-color:#77b;padding:0 5px;margin:0;\
margin-right:5px;'>%i</span>%s</span>", i,txt);
+ if(DEBUG>10) g_print("extracted link : %s\n",txt);
webkit_dom_html_element_set_inner_html (node,buf,NULL);
-
+
+ //sleep(1);
// 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);
+ //txt = webkit_dom_html_link_element_get_href (node);
// so we have to use this:
txt = webkit_dom_element_get_attribute (node,"href");
+ if(txt==NULL)continue;
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);
@@ -153,6 +135,17 @@ document_loaded_callback (WebKitWebPage *web_page,
}
}
+static gboolean mycallback(GIOChannel *ch, GIOCondition c, gpointer p)
+{
+ g_print("callback called\n");
+ gchar *line;
+ g_io_channel_read_line(ch, &line, NULL, NULL, NULL);
+ g_print("read: %s\n",line);
+ g_free(line);
+ show_hints(my_doc);
+ return true;
+
+}
static void
page_created_callback (WebKitWebExtension *extension,
WebKitWebPage *web_page,
@@ -161,15 +154,28 @@ page_created_callback (WebKitWebExtension *extension,
g_signal_connect (web_page, "document-loaded",
G_CALLBACK (document_loaded_callback),
user_data);
+
+ GMainContext *ctx = g_main_context_default();
+ GIOChannel *io = g_io_channel_new_file("/home/miguel/temp/mypipe", "r+", NULL);
+ GSource *watch = g_io_create_watch(io, G_IO_IN);
+ g_source_attach(watch,ctx);
+ g_source_set_callback(watch,(GSourceFunc)mycallback,NULL,NULL);
+
}
G_MODULE_EXPORT void
webkit_web_extension_initialize_with_user_data (WebKitWebExtension *extension,
GVariant *user_data)
{
+ g_print("Initalizing Miguel's Surfin' Quick Links WebKit2GTK+ Extension\n");
+ g_print("Extension PID: %d\n",getpid());
+ gchar *str=g_variant_get_string(user_data,NULL);
+ g_print("UI Process Data: %s\n",str);
+// g_free(str);
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
@@ -179,3 +185,4 @@ bool str_prefix(const char *str, const char *pre)
lenstr = strlen(str);
return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}
+