Initial import

This commit is contained in:
Adrian Perez de Castro 2015-08-25 19:05:14 +03:00
commit f180885d41
6 changed files with 183 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.*.sw[op]
*.py[odc]
*.so
*.o
__pycache__/

26
Makefile Normal file
View file

@ -0,0 +1,26 @@
#
# Makefile
# Adrian Perez, 2015-08-25 11:58
#
CFLAGS ?= -Os -Wall
PYTHON ?= python3
PKG_MODULES := pygobject-3.0 webkit2gtk-web-extension-4.0 ${PYTHON}
WEB_EXT_FLAGS := $(shell pkg-config ${PKG_MODULES} --cflags)
WEB_EXT_LIBS := $(shell pkg-config ${PKG_MODULES} --libs)
CPPFLAGS += ${WEB_EXT_FLAGS}
LDLIBS += ${WEB_EXT_LIBS}
all: pythonloader.so
pythonloader.so: pythonloader.o
${LD} ${LDFLAGS} -fPIC -shared -o $@ $^ ${LDLIBS}
pythonloader.so: CFLAGS += -fPIC
clean:
${RM} pythonloader.o pythonloader.so
# vim:ft=make
#

31
browse-with-extension Executable file
View file

@ -0,0 +1,31 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 Adrian Perez <aperez@igalia.com>
#
# Distributed under terms of the MIT license.
from gi.repository import WebKit2, Gtk
from os import path
import sys
mydir = path.abspath(path.dirname(__file__))
print("Extension directory:", mydir)
ctx = WebKit2.WebContext.get_default()
ctx.set_web_extensions_directory(mydir)
wnd = Gtk.Window()
web = WebKit2.WebView.new_with_context(ctx)
wnd.connect("destroy", Gtk.main_quit)
wnd.add(web)
wnd.set_default_size(1152, 800)
wnd.show_all()
if len(sys.argv) > 1:
web.load_uri(sys.argv[1])
else:
web.load_uri("http://ddg.gg")
Gtk.main()

26
extension.py Normal file
View file

@ -0,0 +1,26 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 Adrian Perez <aperez@igalia.com>
#
# Distributed under terms of the MIT license.
def on_document_loaded(webpage):
# As en example, use the DOM to print the document title
print("document-loaded: uri =", webpage.get_uri())
document = webpage.get_dom_document()
print("document-loaded: title =", document.get_title())
def on_page_created(extension, webpage):
print("page-created: extension =", extension)
print("page-created: page =", webpage)
print("page-created: uri =", webpage.get_uri())
webpage.connect("document-loaded", on_document_loaded)
def initialize(extension, arguments):
print("initialize: extension =", extension)
print("initialize: arguments =", arguments)
extension.connect("page-created", on_page_created)

85
pythonloader.c Normal file
View file

@ -0,0 +1,85 @@
/*
* pythonloader.c
* Copyright (C) 2015 Adrian Perez <aperez@igalia.com>
*
* Distributed under terms of the MIT license.
*/
#include <webkit2/webkit-web-extension.h>
#include <pygobject.h>
#define py_auto __attribute__((cleanup(py_object_cleanup)))
static void
py_object_cleanup(void *ptr)
{
PyObject **py_obj_location = ptr;
if (py_obj_location) {
Py_DECREF (*py_obj_location);
*py_obj_location = NULL;
}
}
G_MODULE_EXPORT void
webkit_web_extension_initialize_with_user_data (WebKitWebExtension *extension,
const GVariant *user_data)
{
g_printerr ("Python loader, extension=%p\n", extension);
Py_Initialize ();
pygobject_init (-1, -1, -1);
if (PyErr_Occurred ()) {
g_warning ("Could not initialize PyGObject");
return;
}
pyg_enable_threads ();
PyEval_InitThreads ();
PyObject py_auto *web_ext_module =
PyImport_ImportModule ("gi.repository.WebKit2WebExtension");
if (!web_ext_module) {
if (PyErr_Occurred ()) {
g_printerr ("Could not import gi.repository.WebKit2WebExtension: ");
PyErr_Print ();
} else {
g_printerr ("Could not import gi.repository.WebKit2WebExtension"
" (no error given)\n");
}
return;
}
PyObject py_auto *py_filename = PyUnicode_FromString ("extension");
PyObject py_auto *py_module = PyImport_Import (py_filename);
if (!py_module) {
g_warning ("Could not import '%s'", "extension");
return;
}
PyObject py_auto *py_func = PyObject_GetAttrString (py_module, "initialize");
if (!py_func) {
g_warning ("Could not obtain '%s.initialize'", "extension");
return;
}
if (!PyCallable_Check (py_func)) {
g_warning ("Object '%s.initialize' is not callable", "extension");
return;
}
PyObject py_auto *py_extension = pygobject_new (G_OBJECT (extension));
PyObject py_auto *py_extra_args = pyg_boxed_new (G_TYPE_VARIANT,
(gpointer) user_data,
TRUE,
TRUE);
PyObject py_auto *py_func_args = PyTuple_New (2);
PyTuple_SetItem (py_func_args, 0, py_extension);
PyTuple_SetItem (py_func_args, 1, py_extra_args);
PyObject py_auto *py_retval = PyObject_CallObject (py_func, py_func_args);
if (!py_retval) {
g_printerr ("Error calling '%s.initialize':\n", "extension");
PyErr_Print ();
}
}

10
run.sh Executable file
View file

@ -0,0 +1,10 @@
#! /bin/sh
#
# run.sh
# Copyright (C) 2015 Adrian Perez <aperez@igalia.com>
#
# Distributed under terms of the MIT license.
#
make -s
export PYTHONPATH=$(pwd)
exec ./browse-with-extension "$@"