-
Notifications
You must be signed in to change notification settings - Fork 0
/
webkit_2_6.go
297 lines (250 loc) · 9.14 KB
/
webkit_2_6.go
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package webkit
// #cgo pkg-config: webkit2gtk-4.0
// #include <webkit2/webkit2.h>
// #include "webkit_2_6.go.h"
import "C"
import (
//"errors"
"runtime"
"unsafe"
//"github.com/andre-hub/gotk3/gtk"
"github.com/gotk3/gotk3/glib"
)
func init() {
tm := []glib.TypeMarshaler{
// Enums
{glib.Type(C.webkit_user_content_injected_frames_get_type()), marshalUserContentInjectedFrames},
{glib.Type(C.webkit_user_script_injection_time_get_type()), marshalUserScriptInjectionTime},
{glib.Type(C.webkit_user_style_level_get_type()), marshalUserStyleLevel},
// Objects/Interfaces
{glib.Type(C.webkit_user_content_manager_get_type()), marshalUserContentManager},
// Boxed
{glib.Type(C.webkit_user_script_get_type()), marshalUserScript},
{glib.Type(C.webkit_user_style_sheet_get_type()), marshalUserStyleSheet},
}
glib.RegisterGValueMarshalers(tm)
}
/*
* Constants
*/
// UserContentInjectedFrames is a wrapper around WebKitUserContentInjectedFrames.
type UserContentInjectedFrames int
const (
USER_CONTENT_INJECT_ALL_FRAMES UserContentInjectedFrames = C.WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES
USER_CONTENT_INJECT_TOP_FRAME UserContentInjectedFrames = C.WEBKIT_USER_CONTENT_INJECT_TOP_FRAME
)
func marshalUserContentInjectedFrames(p uintptr) (interface{}, error) {
c := C.g_value_get_enum((*C.GValue)(unsafe.Pointer(p)))
return UserContentInjectedFrames(c), nil
}
// UserScriptInjectionTime is a wrapper around WebKitUserScriptInjectionTime.
type UserScriptInjectionTime int
const (
USER_SCRIPT_INJECT_AT_DOCUMENT_START UserScriptInjectionTime = C.WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START
USER_SCRIPT_INJECT_AT_DOCUMENT_END UserScriptInjectionTime = C.WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END
)
func marshalUserScriptInjectionTime(p uintptr) (interface{}, error) {
c := C.g_value_get_enum((*C.GValue)(unsafe.Pointer(p)))
return UserScriptInjectionTime(c), nil
}
// UserStyleLevel is a wrapper around WebKitUserStyleLevel.
type UserStyleLevel int
const (
USER_STYLE_LEVEL_USER UserStyleLevel = C.WEBKIT_USER_STYLE_LEVEL_USER
USER_STYLE_LEVEL_AUTHOR UserStyleLevel = C.WEBKIT_USER_STYLE_LEVEL_AUTHOR
)
func marshalUserStyleLevel(p uintptr) (interface{}, error) {
c := C.g_value_get_enum((*C.GValue)(unsafe.Pointer(p)))
return UserStyleLevel(c), nil
}
/*
* WebKitUserContentManager
*/
type UserContentManager struct {
*glib.Object
}
// native returns a pointer to the underlying WebKitUserContentManager.
func (v *UserContentManager) native() *C.WebKitUserContentManager {
if v == nil || v.GObject == nil {
return nil
}
p := unsafe.Pointer(v.GObject)
return C.toWebKitUserContentManager(p)
}
func marshalUserContentManager(p uintptr) (interface{}, error) {
c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))}
return wrapUserContentManager(obj), nil
}
func wrapUserContentManager(obj *glib.Object) *UserContentManager {
return &UserContentManager{obj}
}
// UserContentManagerNew() is a wrapper around webkit_user_content_manager_new().
func UserContentManagerNew() (*UserContentManager, error) {
c := C.webkit_user_content_manager_new()
if c == nil {
return nil, nilPtrErr
}
obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))}
b := wrapUserContentManager(obj)
b.RefSink()
runtime.SetFinalizer(obj, (*glib.Object).Unref)
return b, nil
}
// AddScript() is a wrapper around webkit_user_content_manager_add_script_sheet().
func (v *UserContentManager) AddScript(script *UserScript) {
C.webkit_user_content_manager_add_script(v.native(), script.native())
}
// AddStyleSheet() is a wrapper around webkit_user_content_manager_add_style_sheet().
func (v *UserContentManager) AddStyleSheet(sheet *UserStyleSheet) {
C.webkit_user_content_manager_add_style_sheet(v.native(), sheet.native())
}
// RemoveAllScripts() is a wrapper around webkit_user_content_manager_remove_all_scripts().
func (v *UserContentManager) RemoveAllScripts() {
C.webkit_user_content_manager_remove_all_scripts(v.native())
}
// RemoveAllStyleSheets() is a wrapper around webkit_user_content_manager_remove_all_style_sheets().
func (v *UserContentManager) RemoveAllStyleSheets() {
C.webkit_user_content_manager_remove_all_style_sheets(v.native())
}
/*
* WebKitWebView
*/
// WebViewNewWithSettings() is a wrapper around webkit_web_view_new_with_settings().
func WebViewNewWithSettings(settings *Settings) (*WebView, error) {
c := C.webkit_web_view_new_with_settings(settings.native())
if c == nil {
return nil, nilPtrErr
}
obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))}
b := wrapWebView(obj)
b.RefSink()
runtime.SetFinalizer(obj, (*glib.Object).Unref)
return b, nil
}
// WebViewNewWithUserContentManager() is a wrapper around webkit_web_view_new_with_user_content_manager().
func WebViewNewWithUserContentManager(manager *UserContentManager) (*WebView, error) {
c := C.webkit_web_view_new_with_user_content_manager(manager.native())
if c == nil {
return nil, nilPtrErr
}
obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))}
b := wrapWebView(obj)
b.RefSink()
runtime.SetFinalizer(obj, (*glib.Object).Unref)
return b, nil
}
// LoadBytes() is a wrapper around webkit_web_view_load_bytes().
func (v *WebView) LoadBytes(bytes []byte, mimeType, encoding, baseUri string) {
gbytes := C.g_bytes_new(C.gconstpointer(&bytes[0]), C.gsize(len(bytes)))
defer C.g_bytes_unref(gbytes)
cmimeType := C.CString(mimeType)
defer C.free(unsafe.Pointer(cmimeType))
cencoding := C.CString(encoding)
defer C.free(unsafe.Pointer(cencoding))
cbaseUri := C.CString(baseUri)
defer C.free(unsafe.Pointer(cbaseUri))
C.webkit_web_view_load_bytes(v.native(), gbytes, (*C.gchar)(cmimeType), (*C.gchar)(cencoding), (*C.gchar)(cbaseUri))
}
// GetUserContentManager() is a wrapper around webkit_web_view_get_user_content_manager().
func (v *WebView) GetUserContentManager() *UserContentManager {
c := C.webkit_web_view_get_user_content_manager(v.native())
if c == nil {
return nil
}
obj := &glib.Object{glib.ToGObject(unsafe.Pointer(c))}
w := wrapUserContentManager(obj)
obj.RefSink()
runtime.SetFinalizer(obj, (*glib.Object).Unref)
return w
}
/*
* WebKitUserScript
*/
type UserScript struct {
WebKitUserScript *C.WebKitUserScript
}
// native returns a pointer to the underlying WebKitUserScript.
func (v *UserScript) native() *C.WebKitUserScript {
if v == nil {
return nil
}
return v.WebKitUserScript
}
func marshalUserScript(p uintptr) (interface{}, error) {
c := C.g_value_get_boxed((*C.GValue)(unsafe.Pointer(p)))
return (*UserScript)(unsafe.Pointer(c)), nil
}
// UserScriptNew() is a wrapper around webkit_user_script_new().
func UserScriptNew(source string, injectedFrames UserContentInjectedFrames, injectionTime UserScriptInjectionTime, whitelist, blacklist []string) (*UserScript, error) {
csource := C.CString(source)
defer C.free(unsafe.Pointer(csource))
cwhite, cwhiteFree := gstringArray(whitelist)
for _, i := range cwhiteFree {
defer C.free(unsafe.Pointer(i))
}
cblack, cblackFree := gstringArray(blacklist)
for _, i := range cblackFree {
defer C.free(unsafe.Pointer(i))
}
c := C.webkit_user_script_new((*C.gchar)(csource), C.WebKitUserContentInjectedFrames(injectedFrames), C.WebKitUserScriptInjectionTime(injectionTime), cwhite, cblack)
if c == nil {
return nil, nilPtrErr
}
b := &UserScript{c}
runtime.SetFinalizer(b, (*UserScript).Unref)
return b, nil
}
// Unref is a wrapper around webkit_user_script_unref(). This is for internal use.
func (v *UserScript) Unref() {
C.webkit_user_script_unref(v.native())
}
// RefSink is a wrapper around webkit_user_script_ref(). This is for internal use.
func (v *UserScript) Ref() {
C.webkit_user_script_ref(v.native())
}
/*
* WebKitUserStyleSheet
*/
type UserStyleSheet struct {
WebKitUserStyleSheet *C.WebKitUserStyleSheet
}
// native returns a pointer to the underlying WebKitUserStyleSheet.
func (v *UserStyleSheet) native() *C.WebKitUserStyleSheet {
if v == nil {
return nil
}
return v.WebKitUserStyleSheet
}
func marshalUserStyleSheet(p uintptr) (interface{}, error) {
c := C.g_value_get_boxed((*C.GValue)(unsafe.Pointer(p)))
return (*UserStyleSheet)(unsafe.Pointer(c)), nil
}
// UserStyleSheetNew() is a wrapper around webkit_user_style_sheet_new().
func UserStyleSheetNew(source string, injectedFrames UserContentInjectedFrames, level UserStyleLevel, whitelist, blacklist []string) (*UserStyleSheet, error) {
csource := C.CString(source)
defer C.free(unsafe.Pointer(csource))
cwhite, _ := gstringArray(whitelist)
/*for _, i := range cwhiteFree {
defer C.free(unsafe.Pointer(i))
}*/
cblack, _ := gstringArray(blacklist)
/*for _, i := range cblackFree {
defer C.free(unsafe.Pointer(i))
}*/
c := C.webkit_user_style_sheet_new((*C.gchar)(csource), C.WebKitUserContentInjectedFrames(injectedFrames), C.WebKitUserStyleLevel(level), cwhite, cblack)
if c == nil {
return nil, nilPtrErr
}
b := &UserStyleSheet{c}
runtime.SetFinalizer(b, (*UserStyleSheet).Unref)
return b, nil
}
// Unref is a wrapper around webkit_user_script_unref(). This is for internal use.
func (v *UserStyleSheet) Unref() {
C.webkit_user_style_sheet_unref(v.native())
}
// RefSink is a wrapper around webkit_user_script_ref(). This is for internal use.
func (v *UserStyleSheet) Ref() {
C.webkit_user_style_sheet_ref(v.native())
}