Skip to content

Commit 67b6eb8

Browse files
authored
Initial support for cross-browser Web Extension API (#250)
* Initial support for cross-browser Web Extension API * Json-GLib is a introduced as a new dependency to read manifest.json * The README contains a new section to summarize the Web Extension API * Web extension support is implemented as a Peas plugin * Web extensions can be built-in, system-wide or user-installed * The Javascript interface is added to the core GLib.Resource * Midori.Tab shares a WebKit.UserContentManager per WebKit.WebContext Fixes: #244 * Only insert loaded web extensions and employ a delay installing the API Otherwise opening a new window with many extensions would result in significant delay.
1 parent bbddf92 commit 67b6eb8

13 files changed

Lines changed: 470 additions & 10 deletions

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
name: Install dependencies
1010
command: |
1111
apt-get update
12-
apt-get install -y cmake xvfb valac libwebkit2gtk-4.0-dev libsoup-gnome2.4-dev libgcr-3-dev libpeas-dev libsqlite3-dev intltool libxml2-utils
12+
apt-get install -y cmake xvfb valac libwebkit2gtk-4.0-dev libsoup-gnome2.4-dev libgcr-3-dev libpeas-dev libsqlite3-dev libjson-glib-dev intltool libxml2-utils
1313
- run:
1414
name: Build
1515
command: |

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ pkg_check_modules(DEPS_GTK REQUIRED
5454
webkit2gtk-4.0>=2.16.6
5555
gcr-ui-3>=2.32
5656
libpeas-gtk-1.0
57+
json-glib-1.0>=0.12
5758
)
58-
set(PKGS ${PKGS} gtk+-3.0 libsoup-2.4 gcr-ui-3 libpeas-gtk-1.0)
59+
set(PKGS ${PKGS} gtk+-3.0 libsoup-2.4 gcr-ui-3 libpeas-gtk-1.0 json-glib-1.0)
5960
set(EXTRA_VAPIS
6061
${CMAKE_SOURCE_DIR}/vapi/config.vapi
6162
${CMAKE_SOURCE_DIR}/vapi/webkit2gtk-4.0.vapi

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Midori is a lightweight yet powerful web browser which runs just as well on litt
3030
* Customizable side panels.
3131
* User scripts and styles a la Greasemonkey.
3232
* Web developer tools powered by WebKit.
33+
* Cross-browser extensions compatible with Chrome, Firefox, Opera and Vivaldi
3334

3435
Please report comments, suggestions and bugs to:
3536
https://github.com/midori-browser/core/issues
@@ -64,14 +65,15 @@ You can opt-in for the [beta release on the Play Store](https://play.google.com/
6465
* [Vala](https://wiki.gnome.org/Projects/Vala) 0.30
6566
* GCR 2.32
6667
* Peas
68+
* JSON-Glib 0.12
6769

6870
Install dependencies on Astian OS, Ubuntu, Debian or other Debian-based distros:
6971

70-
sudo apt install cmake valac libwebkit2gtk-4.0-dev libgcr-3-dev libpeas-dev libsqlite3-dev intltool libxml2-utils
72+
sudo apt install cmake valac libwebkit2gtk-4.0-dev libgcr-3-dev libpeas-dev libsqlite3-dev libjson-glib-dev intltool libxml2-utils
7173

7274
Install dependencies on openSUSE:
7375

74-
sudo zypper in cmake vala gcc webkit2gtk3-devel libgcr-devel libpeas-devel sqlite3-devel fdupes gettext-tools intltool libxml2-devel
76+
sudo zypper in cmake vala gcc webkit2gtk3-devel libgcr-devel libpeas-devel sqlite3-devel json-glib-devel fdupes gettext-tools intltool libxml2-devel
7577

7678
Use CMake to build Midori:
7779

@@ -247,13 +249,51 @@ Push your branch and **propose it for merging into master**.
247249

248250
This will automatically request a **review from other developers** who can then comment on it and provide feedback.
249251

252+
# Extensions
253+
254+
## Cross-browser web extensions
255+
256+
The following API specification is supported by Midori:
257+
258+
manifest.json
259+
name
260+
version
261+
description
262+
background:
263+
page: *.html
264+
scripts:
265+
- *.js
266+
browser_action:
267+
default_popup: *.html
268+
default_icon: *.png
269+
default_title
270+
content_scripts:
271+
js:
272+
- *.js
273+
css:
274+
- *.css
275+
manifest_version: 2
276+
277+
*.js
278+
browser (chrome)
279+
tabs
280+
create
281+
- url: uri
282+
executeScript
283+
- code: string
284+
notifications
285+
create
286+
- title: string
287+
message: string
288+
250289
# Jargon
251290

252291
* **freeze**: a period of bug fixes eg. 4/2 cycle means 4 weeks of features and 2 weeks of stabilization
253292
* **PR**: pull request, a branch proposed for review, analogous to **MR** (merge request) with Bazaar
254293
* **ninja**: an internal tab, usually empty label, used for taking screenshots
255294
* **fortress**: user of an ancient release like 0.4.3 as found on Raspberry Pie, Debian, Ubuntu
256295
* **katze, sokoke, tabby**: legacy API names and coincidentally cat breeds
296+
* web extension: a cross-browser extension (plugin) - or in a webkit context, the multi-process api
257297

258298
# Midori for Android
259299

core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ add_custom_command(OUTPUT resources.c
4848
${CMAKE_SOURCE_DIR}/gresource.xml
4949
DEPENDS ${CMAKE_SOURCE_DIR}/gresource.xml
5050
DEPENDS ${CMAKE_SOURCE_DIR}/data/gtk3.css
51+
DEPENDS ${CMAKE_SOURCE_DIR}/data/web-extension-api.js
5152
DEPENDS ${UI_FILES}
5253
)
5354

core/tab.vala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ namespace Midori {
5959

6060
public Tab (Tab? related, WebKit.WebContext web_context,
6161
string? uri = null, string? title = null) {
62-
Object (related_view: related, web_context: web_context, visible: true);
62+
63+
// One content manager per web context
64+
var content = web_context.get_data<WebKit.UserContentManager> ("user-content-manager");
65+
if (content == null) {
66+
content = new WebKit.UserContentManager ();
67+
web_context.set_data<WebKit.UserContentManager> ("user-content-manager", content);
68+
}
69+
70+
Object (related_view: related, web_context: web_context, user_content_manager: content, visible: true);
6371

6472
var settings = get_settings ();
6573
settings.user_agent += " %s".printf (Config.CORE_USER_AGENT_VERSION);

data/web-extension-api.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Promise-based message handler
2+
var promises = [];
3+
var last_promise = 0;
4+
var m = function (fn, args, cb) {
5+
var promise = new Promise (function (resolve, reject) {
6+
window.webkit.messageHandlers.midori.postMessage ({fn: fn, args: args, promise: last_promise});
7+
last_promise = promises.push({resolve: resolve, reject: reject});
8+
});
9+
return promise;
10+
}
11+
12+
// Browser API
13+
window.browser = {
14+
tabs: {
15+
create: function (args, cb) { return m ('tabs.create', args, cb); },
16+
executeScript: function (args, cb) { return m ('tabs.executeScript', args, cb); },
17+
},
18+
notifications: {
19+
create: function (args, cb) { return m ('notifications.create', args, cb); },
20+
}
21+
}
22+
23+
// Compatibility with Chrome
24+
window.chrome = window.browser;

extensions/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ foreach(UNIT_SRC ${EXTENSIONS})
1515
string(FIND ${UNIT_SRC} "." UNIT_EXTENSION)
1616
if (UNIT_EXTENSION EQUAL -1)
1717
set(UNIT ${UNIT_SRC})
18+
file(GLOB MANIFEST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${UNIT_SRC}/manifest.json")
19+
# Web Extension
20+
if (MANIFEST MATCHES "(manifest.json)$")
21+
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/${UNIT} ${CMAKE_CURRENT_BINARY_DIR}/${UNIT})
22+
continue()
23+
endif ()
1824
file(GLOB UNIT_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${UNIT_SRC}/*.vala")
1925
elseif (${UNIT_SRC} MATCHES "(.vala)$")
2026
string(REPLACE ".vala" "" UNIT ${UNIT_SRC})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[Plugin]
2+
Module=web-extensions
3+
IAge=3
4+
Builtin=true
5+
Name=Web Extensions

0 commit comments

Comments
 (0)