[epiphany/pgriffis/web-extension-fixes-2] WebExtensions: Split common functions out of main js file
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension-fixes-2] WebExtensions: Split common functions out of main js file
- Date: Fri, 20 May 2022 16:54:14 +0000 (UTC)
commit aba13352f40da0bc595696f64dc5ce3d519f4a2e
Author: Patrick Griffis <pgriffis igalia com>
Date: Thu May 19 15:51:00 2022 -0500
WebExtensions: Split common functions out of main js file
.../ephy-web-process-extension.c | 14 +++++
.../web-process-extension/ephy-webextension-api.c | 12 +++--
.../ephy-webextension-common.c | 18 +++----
.../resources/epiphany-web-extension.gresource.xml | 1 +
.../epiphany-web-process-extension.gresource.xml | 1 +
.../resources/js/webextensions-common.js | 62 ++++++++++++++++++++++
.../resources/js/webextensions.js | 53 +++++-------------
src/webextension/api/tabs.c | 2 -
8 files changed, 107 insertions(+), 56 deletions(-)
---
diff --git a/embed/web-process-extension/ephy-web-process-extension.c
b/embed/web-process-extension/ephy-web-process-extension.c
index cbe4f15fe..f34faf034 100644
--- a/embed/web-process-extension/ephy-web-process-extension.c
+++ b/embed/web-process-extension/ephy-web-process-extension.c
@@ -229,12 +229,26 @@ content_script_window_object_cleared_cb (WebKitScriptWorld *world,
{
EphyWebProcessExtension *extension = user_data;
g_autoptr (JSCContext) js_context = NULL;
+ g_autoptr (JSCValue) js_browser = NULL;
+ g_autoptr (JSCValue) result = NULL;
+ g_autoptr (GBytes) bytes = NULL;
JsonObject *translations;
const char *guid;
+ const char *data;
+ gsize data_size;
guid = webkit_script_world_get_name (world);
js_context = webkit_frame_get_js_context_for_script_world (frame, world);
translations = g_hash_table_lookup (extension->translation_table, guid);
+
+ js_browser = jsc_context_get_value (js_context, "browser");
+ g_assert (!jsc_value_is_object (js_browser));
+
+ bytes = g_resources_lookup_data ("/org/gnome/epiphany-web-extension/js/webextensions-common.js",
G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
+ data = g_bytes_get_data (bytes, &data_size);
+ result = jsc_context_evaluate_with_source_uri (js_context, data, data_size,
"resource:///org/gnome/epiphany-web-extension/js/webextensions-common.js", 1);
+ g_clear_object (&result);
+
ephy_webextension_install_common_apis (js_context, guid, translations);
}
diff --git a/embed/web-process-extension/ephy-webextension-api.c
b/embed/web-process-extension/ephy-webextension-api.c
index 93af7c367..3b9cb67d7 100644
--- a/embed/web-process-extension/ephy-webextension-api.c
+++ b/embed/web-process-extension/ephy-webextension-api.c
@@ -154,15 +154,21 @@ window_object_cleared_cb (WebKitScriptWorld *world,
js_context = webkit_frame_get_js_context_for_script_world (frame, world);
- ephy_webextension_install_common_apis (js_context, extension->guid, extension->translations);
-
js_browser = jsc_context_get_value (js_context, "browser");
- g_assert (jsc_value_is_object (js_browser));
+ g_assert (!jsc_value_is_object (js_browser));
+
+ bytes = g_resources_lookup_data ("/org/gnome/epiphany-web-extension/js/webextensions-common.js",
G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
+ data = g_bytes_get_data (bytes, &data_size);
+ result = jsc_context_evaluate_with_source_uri (js_context, data, data_size,
"resource:///org/gnome/epiphany-web-extension/js/webextensions-common.js", 1);
+ g_bytes_unref (bytes);
+ g_clear_object (&result);
bytes = g_resources_lookup_data ("/org/gnome/epiphany-web-extension/js/webextensions.js",
G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
data = g_bytes_get_data (bytes, &data_size);
result = jsc_context_evaluate_with_source_uri (js_context, data, data_size,
"resource:///org/gnome/epiphany-web-extension/js/webextensions.js", 1);
g_clear_object (&result);
+
+ ephy_webextension_install_common_apis (js_context, extension->guid, extension->translations);
}
static void
diff --git a/embed/web-process-extension/ephy-webextension-common.c
b/embed/web-process-extension/ephy-webextension-common.c
index ce341c16e..e94b3211b 100644
--- a/embed/web-process-extension/ephy-webextension-common.c
+++ b/embed/web-process-extension/ephy-webextension-common.c
@@ -90,18 +90,16 @@ ephy_webextension_install_common_apis (JSCContext *js_context,
g_autoptr (JSCValue) result = NULL;
g_autoptr (JSCValue) js_browser = NULL;
g_autoptr (JSCValue) js_i18n = NULL;
- g_autoptr (JSCValue) js_runtime = NULL;
+ g_autoptr (JSCValue) js_extension = NULL;
g_autoptr (JSCValue) js_function = NULL;
+ g_autoptr (JSCValue) js_object = NULL;
jsc_context_push_exception_handler (js_context, (JSCExceptionHandler)js_exception_handler, NULL, NULL);
/* APIs available in content scripts: https://developer.chrome.com/docs/extensions/mv3/content_scripts/ */
- result = jsc_context_get_value (js_context, "browser");
- g_assert (jsc_value_is_undefined (result));
-
- js_browser = jsc_value_new_object (js_context, NULL, NULL);
- jsc_context_set_value (js_context, "browser", js_browser);
+ js_browser = jsc_context_get_value (js_context, "browser");
+ g_assert (jsc_value_is_object (js_browser));
/* i18n */
js_i18n = jsc_value_new_object (js_context, NULL, NULL);
@@ -125,9 +123,9 @@ ephy_webextension_install_common_apis (JSCContext *js_context,
jsc_value_object_set_property (js_i18n, "getMessage", js_function);
g_clear_object (&js_function);
- /* runtime */
- js_runtime = jsc_value_new_object (js_context, NULL, NULL);
- jsc_value_object_set_property (js_browser, "runtime", js_runtime);
+ /* extension */
+ js_extension = jsc_value_new_object (js_context, NULL, NULL);
+ jsc_value_object_set_property (js_browser, "extension", js_extension);
js_function = jsc_value_new_function (js_context,
"getURL",
@@ -135,6 +133,6 @@ ephy_webextension_install_common_apis (JSCContext *js_context,
G_TYPE_STRING,
1,
G_TYPE_STRING);
- jsc_value_object_set_property (js_runtime, "getURL", js_function);
+ jsc_value_object_set_property (js_extension, "getURL", js_function);
g_clear_object (&js_function);
}
diff --git a/embed/web-process-extension/resources/epiphany-web-extension.gresource.xml
b/embed/web-process-extension/resources/epiphany-web-extension.gresource.xml
index 9d5221ba9..65a42b954 100644
--- a/embed/web-process-extension/resources/epiphany-web-extension.gresource.xml
+++ b/embed/web-process-extension/resources/epiphany-web-extension.gresource.xml
@@ -2,5 +2,6 @@
<gresources>
<gresource prefix="/org/gnome/epiphany-web-extension">
<file compressed="true">js/webextensions.js</file>
+ <file compressed="true">js/webextensions-common.js</file>
</gresource>
</gresources>
diff --git a/embed/web-process-extension/resources/epiphany-web-process-extension.gresource.xml
b/embed/web-process-extension/resources/epiphany-web-process-extension.gresource.xml
index 8cdf15d0c..944a27f55 100644
--- a/embed/web-process-extension/resources/epiphany-web-process-extension.gresource.xml
+++ b/embed/web-process-extension/resources/epiphany-web-process-extension.gresource.xml
@@ -4,5 +4,6 @@
<file compressed="true">js/ephy.js</file>
<file compressed="true">js/overview.js</file>
<file compressed="true">js/webextensions.js</file>
+ <file compressed="true">js/webextensions-common.js</file>
</gresource>
</gresources>
diff --git a/embed/web-process-extension/resources/js/webextensions-common.js
b/embed/web-process-extension/resources/js/webextensions-common.js
new file mode 100644
index 000000000..d3f24cc98
--- /dev/null
+++ b/embed/web-process-extension/resources/js/webextensions-common.js
@@ -0,0 +1,62 @@
+'use strict';
+
+/* exported runtimeSendMessage, runtimeOnConnect, ephy_message */
+
+window.browser = {};
+
+const promises = [];
+let last_promise = 0;
+
+let runtime_onmessage_listeners = [];
+let runtime_onconnect_listeners = [];
+
+const ephy_message = function (fn, args, cb) {
+ const promise = new Promise (function (resolve, reject) {
+ window.webkit.messageHandlers.epiphany.postMessage ({fn: fn, args: args, promise: last_promise});
+ last_promise = promises.push({resolve: resolve, reject: reject});
+ });
+ return promise;
+};
+
+const runtimeSendMessage = function(x) {
+ for (const listener of runtime_onmessage_listeners)
+ listener.callback(x);
+};
+
+const runtimeOnConnect = function(x) {
+ for (const listener of runtime_onconnect_listeners)
+ listener.callback(x);
+};
+
+window.browser.runtime = {
+ getURL: function (args, cb) { return window.browser.extension.getURL(args, cb); },
+ getManifest: function (args, cb) { return '[]'; },
+ onMessage: {
+ addListener: function (cb) {
+ runtime_onmessage_listeners.push({callback: cb});
+ },
+ removeListener: function (cb) {
+ runtime_onmessage_listeners = runtime_onmessage_listeners.filter(l => l.callback !== cb);
+ },
+ hasListener: function (cb) {
+ return !!runtime_onmessage_listeners.find(l => l.callback === cb);
+ }
+ },
+ onConnect: {
+ addListener: function (cb) {
+ runtime_onconnect_listeners.push({callback: cb});
+ },
+ removeListener: function (cb) {
+ runtime_onconnect_listeners = runtime_onconnect_listeners.filter(l => l.callback !== cb);
+ },
+ hasListener: function (cb) {
+ return !!runtime_onconnect_listeners.find(l => l.callback === cb);
+ }
+ },
+ sendMessage: function (args, cb) {
+ return ephy_message ('runtime.sendMessage', args, cb);
+ },
+};
+
+// Compatibility with Chrome
+window.chrome = window.browser;
diff --git a/embed/web-process-extension/resources/js/webextensions.js
b/embed/web-process-extension/resources/js/webextensions.js
index e234494b8..efae76f29 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -1,27 +1,15 @@
'use strict';
-/* exported pageActionOnClicked, browserActionClicked, browserActionClicked, tabsOnUpdated,
runtimeSendMessage, runtimeOnConnect */
-
-const promises = [];
-let last_promise = 0;
+/* exported pageActionOnClicked, browserActionClicked, browserActionClicked, tabsOnUpdated */
+/* global ephy_message */
const tabs_listeners = [];
const page_listeners = [];
const browser_listeners = [];
const runtime_listeners = [];
-const runtime_onmessage_listeners = [];
const runtime_onmessageexternal_listeners = [];
-const runtime_onconnect_listeners = [];
const windows_onremoved_listeners = [];
-const ephy_message = function (fn, args, cb) {
- const promise = new Promise (function (resolve, reject) {
- window.webkit.messageHandlers.epiphany.postMessage ({fn: fn, args: args, promise: last_promise});
- last_promise = promises.push({resolve: resolve, reject: reject});
- });
- return promise;
-};
-
const pageActionOnClicked = function(x) {
for (const listener of page_listeners)
listener.callback(x);
@@ -37,16 +25,6 @@ const tabsOnUpdated = function(x) {
listener.callback(x);
};
-const runtimeSendMessage = function(x) {
- for (const listener of runtime_onmessage_listeners)
- listener.callback(x);
-};
-
-const runtimeOnConnect = function(x) {
- for (const listener of runtime_onconnect_listeners)
- listener.callback(x);
-};
-
// Browser async API
window.browser.alarms = {
clearAll: function (args, cb) { return ephy_message ('alarms.clearAll', args, cb); },
@@ -74,20 +52,17 @@ window.browser.notifications = {
create: function (args, cb) { return ephy_message ('notifications.create', args, cb); },
};
-window.browser.extension = {
- getURL: function (args, cb) { return window.browser.runtime.getURL(args, cb); },
+// browser.runtime is defined in webextensions-common.js
+window.browser.runtime.getBrowserInfo = function (args, cb) { return ephy_message ('runtime.getBrowserInfo',
args, cb); };
+window.browser.runtime.connectNative = function (args, cb) { return ephy_message ('runtime.connectNative',
args, cb); };
+window.browser.runtime.openOptionsPage = function (args, cb) { return ephy_message
('runtime.openOptionsPage', args, cb); };
+window.browser.runtime.setUninstallURL = function (args, cb) { return ephy_message
('runtime.setUninstallURL', args, cb); };
+window.browser.runtime.onInstalled = {
+ addListener: function (cb) { runtime_listeners.push({callback: cb}); }
+};
+window.browser.runtime.onMessageExternal = {
+ addListener: function (cb) { runtime_onmessageexternal_listeners.push({callback: cb}); }
};
-
-window.browser.runtime.getManifest = function (args, cb) { return '[]'; };
-window.browser.runtime.getBrowserInfo = function (args, cb) { return ephy_message ('runtime.getBrowserInfo',
args, cb); },
-window.browser.runtime.onInstalled = { addListener: function (cb) { runtime_listeners.push({callback: cb});
} };
-window.browser.runtime.onMessage = { addListener: function (cb) {
runtime_onmessage_listeners.push({callback: cb}); } };
-window.browser.runtime.onMessageExternal = { addListener: function (cb) {
runtime_onmessageexternal_listeners.push({callback: cb}); } };
-window.browser.runtime.onConnect = { addListener: function (cb) {
runtime_onconnect_listeners.push({callback: cb}); } };
-window.browser.runtime.connectNative = function (args, cb) { return ephy_message ('runtime.connectNative',
args, cb); },
-window.browser.runtime.sendMessage = function (args, cb) { return ephy_message ('runtime.sendMessage', args,
cb); },
-window.browser.runtime.openOptionsPage = function (args, cb) { return ephy_message
('runtime.openOptionsPage', args, cb); },
-window.browser.runtime.setUninstallURL = function (args, cb) { return ephy_message
('runtime.setUninstallURL', args, cb); },
window.browser.pageAction = {
setIcon: function (args, cb) { return ephy_message ('pageAction.setIcon', args, cb); },
@@ -105,7 +80,3 @@ window.browser.browserAction = {
addListener: function (cb) { browser_listeners.push({callback: cb}); }
}
};
-
-// Compatibility with Chrome
-window.chrome = window.browser;
-
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index 88e4fe945..6587b7e63 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -156,8 +156,6 @@ tabs_handler_execute_script (EphyWebExtension *self,
g_autofree char *code = NULL;
EphyShell *shell = ephy_shell_get_default ();
- g_message ("tabs_handler_execute_script");
-
if (jsc_value_is_array (args)) {
obj = jsc_value_object_get_property_at_index (args, 1);
} else {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]