1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
//
// !!! THIS IS STILL A CONCEPT/PROTOTYPE. USE AT YOUR OWN RISK !!!
//
// Author: Michal Idziorek <m.i@gmx.at>
//
// Last Update: 16th March 2018
//
// Last Tested: with surf commit 81f0452bc6c2a110239fa303ad1e72f11c33dc94
// from htps://git.suckless.org/surf
//
// The following simple Webkit2GTK+ extension was hacked together
// in the sleepless night between 09th and 10th March 2018 (and later
// refined).
//
// The DOM is manipulated,i.e: a <span> with a number added to every
// link encountered.
//
// Named FIFO pipes are used for communication with the ui process.
//
// TODO: g_free where appropriate !!
// TODO: allow focus on input boxes/forms
// TODO: 1024 hardcoded doc references!. make dynamic and free unused.
// TODO: more todos directly in source
// TODO: implement New window in surf on msg receive
// TODO: fix hints positions!
//
#include <webkit2/webkit-web-extension.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include "piper_webext.h"
#define CSS_CLASS_NAME "webext_piper_hint"
#define MAX_DOCS 1024
WebKitDOMHTMLDocument *my_doc[MAX_DOCS];
bool hints_active[MAX_DOCS];
void hide_hints(WebKitDOMHTMLDocument *doc,int skip)
{
WebKitDOMNodeList *hints=webkit_dom_document_get_elements_by_class_name (doc,CSS_CLASS_NAME);
gulong c=webkit_dom_node_list_get_length (hints);
int s=0;
for(gulong i=0;i<c;i++)
{
if(i==skip){s++;c--;}
if(i==c)continue;
WebKitDOMNode *node = webkit_dom_node_list_item (hints,s);
WebKitDOMNode *parent = webkit_dom_node_get_parent_node (node);
webkit_dom_node_remove_child (parent,node,NULL);
}
}
void show_hints(WebKitDOMHTMLDocument *doc)
{
WebKitDOMElement *body = webkit_dom_document_get_body(doc);
WebKitDOMHTMLCollection *links = webkit_dom_document_get_links(doc);
gulong c = webkit_dom_html_collection_get_length (links);
for(gulong i=0;i<c;i++)
{
char buf[1024];
WebKitDOMHTMLAnchorElement *node = webkit_dom_html_collection_item (links,i);
WebKitDOMClientRect *rect=webkit_dom_element_get_bounding_client_rect (node);
snprintf(buf,1024,"<span class='%s' style='"\
"border:1px solid black;"\
"font-weight:bold;"\
"font-face:Arial;"\
"font-size:10px;"\
"color:rgba(255,255,255,1);"\
"padding:3px;"\
"background-color:rgba(255,0,0,0.5);"\
"position:absolute;"\
"z-index:100000;"\
"left:%ipx;"\
"top:%ipx;"\
"'>%03d</span>",
CSS_CLASS_NAME,(int)webkit_dom_element_get_scroll_left(body)+(int)webkit_dom_client_rect_get_left(rect),(int)webkit_dom_element_get_scroll_top(body)+(int)webkit_dom_client_rect_get_top(rect),i);
webkit_dom_element_insert_adjacent_html (body,"beforeend",buf,NULL);
}
}
void toggle_hints(int i)
{
if(hints_active[i])
{
hide_hints(my_doc[i],-1);
hints_active[i]=false;
return;
}
show_hints(my_doc[i]);
hints_active[i]=true;
}
void
document_loaded_callback (WebKitWebPage *web_page,
GPtrArray *elements,
GVariant *user_data)
{
int id=webkit_web_page_get_id(web_page);
g_print("Document Loaded Callback Called (page id:%i)\n",id);
hints_active[id]=false;
my_doc[id] = webkit_web_page_get_dom_document(web_page);
}
void
page_created_callback (WebKitWebExtension *extension,
WebKitWebPage *web_page,
GVariant *user_data)
{
g_print("Page Created Callback Called\n");
g_signal_connect (web_page, "document-loaded",
G_CALLBACK (document_loaded_callback),
user_data);
}
static void follow_link(int id,int link_num,bool new_win)
{
hide_hints(my_doc[id],link_num);
hints_active[id]=false;
WebKitDOMHTMLCollection *links = webkit_dom_document_get_links(my_doc[id]);
gulong c = webkit_dom_html_collection_get_length (links);
if(link_num>=c)return;
WebKitDOMHTMLAnchorElement *node = webkit_dom_html_collection_item (links,link_num);
if(!new_win)
{
webkit_dom_html_element_click(node);
return;
}
char *href=webkit_dom_html_anchor_element_get_href (node);
char buf[1024];
snprintf(buf,1024,"%s\n",href);
piper_send(buf);
}
void rcv(char *msg)
{
static int num[3];
static int l=0;
static bool new=false;
int id;
int cmd;
sscanf(msg,"%d %d",&id,&cmd);
if(cmd<10)
{
num[l++]=cmd;
if(l==3)
{
int link_num=100*num[0]+10*num[1]+num[2];
g_printf("link_num: %i\n",link_num);
l=0;
follow_link(id,link_num,new);
}
}
else
{
l=0;
if(cmd==100)new=false;
if(cmd==101)new=true;
toggle_hints(id);
}
}
G_MODULE_EXPORT void
webkit_web_extension_initialize_with_user_data (WebKitWebExtension *extension,
GVariant *user_data)
{
g_print("webext started [%i]\n",getpid());
piper_init("surf-to-ext","ext-to-surf",rcv,NULL);
g_signal_connect (extension, "page-created",
G_CALLBACK (page_created_callback),
user_data );
}
|