From c483ae19e110ef5c1e539883a38fbc79b3dd4e4e Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 21 Jul 2021 22:00:20 +0200 Subject: add ability to query page content from repo and display it in the popup --- background/page_actions_server.js | 27 ++----------- common/ajax.js | 39 +++++++++++++++++++ content/activity_info_server.js | 23 +++++++++++ content/main.js | 3 ++ content/repo_query.js | 80 +++++++++++++++++++++++++++++++++++++++ html/display-panel.html | 23 ++++++++--- html/display-panel.js | 35 +++++++++++++++-- 7 files changed, 197 insertions(+), 33 deletions(-) create mode 100644 common/ajax.js create mode 100644 content/repo_query.js diff --git a/background/page_actions_server.js b/background/page_actions_server.js index c6800a0..a7a44c1 100644 --- a/background/page_actions_server.js +++ b/background/page_actions_server.js @@ -14,6 +14,7 @@ * IMPORT listen_for_connection * IMPORT sha256 * IMPORT get_query_best + * IMPORT make_ajax_request * IMPORTS_END */ @@ -23,9 +24,10 @@ var handler; function send_actions(url, port) { - let [pattern, settings] = query_best(url); + const [pattern, settings] = query_best(url); + const repos = storage.get_all(TYPE_PREFIX.REPO); - port.postMessage(["settings", [pattern, settings]]); + port.postMessage(["settings", [pattern, settings, repos]]); if (settings === undefined) return; @@ -85,27 +87,6 @@ async function get_script_text(script_name) } } -function ajax_callback() -{ - if (this.readyState == 4) - this.resolve_callback(this); -} - -function initiate_ajax_request(resolve, method, url) -{ - var xhttp = new XMLHttpRequest(); - xhttp.resolve_callback = resolve; - xhttp.onreadystatechange = ajax_callback; - xhttp.open(method, url, true); - xhttp.send(); -} - -function make_ajax_request(method, url) -{ - return new Promise((resolve, reject) => - initiate_ajax_request(resolve, method, url)); -} - async function fetch_remote_script(script_data) { try { diff --git a/common/ajax.js b/common/ajax.js new file mode 100644 index 0000000..8082bbe --- /dev/null +++ b/common/ajax.js @@ -0,0 +1,39 @@ +/** + * part of Hachette + * Wrapping XMLHttpRequest into a Promise. + * + * Copyright (C) 2021 Wojtek Kosior + * Redistribution terms are gathered in the `copyright' file. + */ + +function ajax_callback() +{ + if (this.readyState == 4) + this.resolve_callback(this); +} + +function initiate_ajax_request(resolve, reject, method, url) +{ + const xhttp = new XMLHttpRequest(); + xhttp.resolve_callback = resolve; + xhttp.onreadystatechange = ajax_callback; + xhttp.open(method, url, true); + try { + xhttp.send(); + } catch(e) { + console.log(e); + setTimeout(reject, 0); + } +} + +function make_ajax_request(method, url) +{ + return new Promise((resolve, reject) => + initiate_ajax_request(resolve, reject, method, url)); +} + +/* + * EXPORTS_START + * EXPORT make_ajax_request + * EXPORTS_END + */ 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 + */ diff --git a/html/display-panel.html b/html/display-panel.html index 9b6d619..d8d7f5d 100644 --- a/html/display-panel.html +++ b/html/display-panel.html @@ -21,6 +21,14 @@ display: none; } + .show_hide_next2:not(:checked)+* { + display: none; + } + + .show_hide_next2:checked+*+* { + display: none; + } + .hide { display: none; } @@ -34,10 +42,6 @@ #container_for_injected>#none_injected:not(:last-child) { display: none; } - - input#connected_chbx:checked+div+h3 { - display: none; - } @@ -65,12 +69,19 @@ - +

Matched pattern: ... + +
+

Queried from repositories

+
+

diff --git a/html/display-panel.js b/html/display-panel.js index 650c234..1693182 100644 --- a/html/display-panel.js +++ b/html/display-panel.js @@ -144,6 +144,7 @@ function handle_page_info(message) } const connected_chbx = by_id("connected_chbx"); +const query_pattern_but = by_id("query_pattern"); function try_to_connect(tab_id) { @@ -151,21 +152,35 @@ function try_to_connect(tab_id) const connect_info = {name: CONNECTION_TYPE.ACTIVITY_INFO, frameId: 0}; const port = browser.tabs.connect(tab_id, connect_info); - port.onDisconnect.addListener(port => handle_disconnect(tab_id)); + const button_cb = (e) => start_querying_repos(port); + + port.onDisconnect.addListener(port => handle_disconnect(tab_id, button_cb)); port.onMessage.addListener(handle_activity_report); + query_pattern_but.addEventListener("click", button_cb); + if (is_mozilla) setTimeout(() => monitor_connecting(port, tab_id), 1000); } +const query_started_chbx = by_id("query_started_chbx"); + +function start_querying_repos(port) +{ + port.postMessage("dummy (trigger repo querying)"); + query_started_chbx.checked = true; +} + const loading_chbx = by_id("loading_chbx"); -function handle_disconnect(tab_id) +function handle_disconnect(tab_id, button_cb) { + query_pattern_but.removeEventListener("click", button_cb); + if (is_chrome && !browser.runtime.lastError) return; - /* return if there was no connection initialization failure */ + /* return if error was not during connection initialization */ if (connected_chbx.checked) return; @@ -189,6 +204,7 @@ const blocked_span = by_id("blocked"); const payload_span = by_id("payload"); const view_payload_but = by_id("view_payload"); const container_for_injected = by_id("container_for_injected"); +const container_for_repo_responses = by_id("container_for_repo_responses"); function handle_activity_report(message) { @@ -197,7 +213,7 @@ function handle_activity_report(message) const [type, data] = message; if (type === "settings") { - let [pattern, settings] = data; + let [pattern, settings, repos] = data; settings = settings || {}; blocked_span.textContent = settings.allow ? "no" : "yes"; @@ -231,6 +247,17 @@ function handle_activity_report(message) container_for_injected.appendChild(h4); container_for_injected.appendChild(pre); } + if (type === "repo_query_result") { + const [repo_url, response_text] = data; + + const h4 = document.createElement("h4"); + const pre = document.createElement("pre"); + h4.textContent = repo_url; + pre.textContent = response_text; + + container_for_repo_responses.appendChild(h4); + container_for_repo_responses.appendChild(pre); + } } by_id("settings_but") -- cgit v1.2.3