summaryrefslogtreecommitdiff
path: root/content
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-07-21 22:00:20 +0200
committerWojtek Kosior <koszko@koszko.org>2021-07-21 22:00:20 +0200
commitc483ae19e110ef5c1e539883a38fbc79b3dd4e4e (patch)
tree60298e3852ce74233eacc95c77a6e5cdc297418d /content
parent5c68551800e477db41ae6fe3a318b2ff2d7a9cb1 (diff)
downloadbrowser-extension-c483ae19e110ef5c1e539883a38fbc79b3dd4e4e.tar.gz
browser-extension-c483ae19e110ef5c1e539883a38fbc79b3dd4e4e.zip
add ability to query page content from repo and display it in the popup
Diffstat (limited to 'content')
-rw-r--r--content/activity_info_server.js23
-rw-r--r--content/main.js3
-rw-r--r--content/repo_query.js80
3 files changed, 106 insertions, 0 deletions
diff --git a/content/activity_info_server.js b/content/activity_info_server.js
index 8435377..a1384e9 100644
--- a/content/activity_info_server.js
+++ b/content/activity_info_server.js
@@ -11,6 +11,8 @@
* IMPORTS_START
* IMPORT listen_for_connection
* IMPORT CONNECTION_TYPE
+ * IMPORT set_repo_query_repos
+ * IMPORT set_repo_query_callback
* IMPORTS_END
*/
@@ -33,9 +35,22 @@ function report_script(script_data)
function report_settings(settings)
{
+ const [pattern, settings_data, repos] = settings;
+ set_repo_query_repos(repos);
+
report_activity("settings", settings);
}
+function report_repo_query_result(result)
+{
+ report_activity("repo_query_result", result);
+}
+
+function trigger_repo_query()
+{
+ set_repo_query_callback(report_repo_query_result);
+}
+
function new_connection(port)
{
console.log("new activity info connection!");
@@ -44,6 +59,14 @@ function new_connection(port)
for (const activity of activities)
port.postMessage(activity);
+
+ /*
+ * So far the only thing we expect to receive is repo query order. Once more
+ * possibilities arrive, we will need to complicate this listener.
+ */
+ port.onMessage.addListener(trigger_repo_query);
+
+ port.onDisconnect.addListener(() => ports.delete(port));
}
function start_activity_info_server()
diff --git a/content/main.js b/content/main.js
index 8f8375e..437a32b 100644
--- a/content/main.js
+++ b/content/main.js
@@ -20,6 +20,7 @@
* IMPORT is_chrome
* IMPORT is_mozilla
* IMPORT start_activity_info_server
+ * IMPORT set_repo_query_url
* IMPORTS_END
*/
@@ -129,4 +130,6 @@ if (!is_privileged_url(document.URL)) {
}
start_activity_info_server();
+
+ set_repo_query_url(document.URL);
}
diff --git a/content/repo_query.js b/content/repo_query.js
new file mode 100644
index 0000000..b8c8ed9
--- /dev/null
+++ b/content/repo_query.js
@@ -0,0 +1,80 @@
+/**
+ * part of Hachette
+ * Getting available content for site from remote repositories.
+ *
+ * Copyright (C) 2021 Wojtek Kosior
+ * Redistribution terms are gathered in the `copyright' file.
+ */
+
+/*
+ * IMPORTS_START
+ * IMPORT make_ajax_request
+ * IMPORTS_END
+ */
+
+var query_started = false;
+
+var url = undefined;
+var repos = undefined;
+var callback = undefined;
+
+async function query(repo)
+{
+ const [repo_url, data] = repo;
+
+ let response = "Query failed";
+ const query_url = `${repo_url}/query?n=${encodeURIComponent(url)}`;
+
+ try {
+ let xhttp = await make_ajax_request("GET", query_url);
+ if (xhttp.status === 200)
+ response = xhttp.responseText;
+ console.log(xhttp);
+ } catch (e) {
+ console.log(e);
+ }
+
+ callback([repo_url, response]);
+}
+
+function start_query()
+{
+ if (query_started || !url || !repos || !callback)
+ return;
+
+ query_started = true;
+
+ console.log(`about to query ${url} from ${repos}`);
+
+ for (const repo of repos)
+ query(repo);
+}
+
+function set_repo_query_url(_url)
+{
+ url = _url;
+
+ start_query();
+}
+
+function set_repo_query_repos(_repos)
+{
+ repos = _repos;
+
+ start_query();
+}
+
+function set_repo_query_callback(_callback)
+{
+ callback = _callback;
+
+ start_query();
+}
+
+/*
+ * EXPORTS_START
+ * EXPORT set_repo_query_url
+ * EXPORT set_repo_query_repos
+ * EXPORT set_repo_query_callback
+ * EXPORTS_END
+ */