diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-03-15 10:12:06 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-03-24 20:43:40 +0100 |
commit | bbc9fae4291d0c2cb3976d158ecd20e0bd2a8ea0 (patch) | |
tree | 9d72c3184f85b61d3d71bc2d65cddb8058d9222a /html/repo_query.js | |
parent | 65351e8c69659455096d1e37c4b78d1298fb7021 (diff) | |
download | browser-extension-bbc9fae4291d0c2cb3976d158ecd20e0bd2a8ea0.tar.gz browser-extension-bbc9fae4291d0c2cb3976d158ecd20e0bd2a8ea0.zip |
serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage
Diffstat (limited to 'html/repo_query.js')
-rw-r--r-- | html/repo_query.js | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/html/repo_query.js b/html/repo_query.js index 601a0aa..97f60ac 100644 --- a/html/repo_query.js +++ b/html/repo_query.js @@ -49,6 +49,8 @@ #FROM html/install.js IMPORT InstallView #FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas +#FROM html/repo_query_cacher_client.js IMPORT indirect_fetch + const coll = new Intl.Collator(); function ResultEntry(repo_entry, mapping_ref) { @@ -76,36 +78,42 @@ function RepoEntry(query_view, repo_url) { this.repo_url_label.innerText = repo_url; - const query_results = async () => { - const msg = [ - "repo_query", - `${repo_url}query?url=${encodeURIComponent(query_view.url)}` - ]; - const response = await browser.tabs.sendMessage(query_view.tab_id, msg); + const encoded_queried_url = encodeURIComponent(query_view.url); + const query_url = `${repo_url}query?url=${encoded_queried_url}`; - if ("error" in response) + const query_results = async () => { + try { + var response = await indirect_fetch(query_view.tab_id, query_url); + } catch(e) { + console.error("Haketilo:", e); throw "Failure to communicate with repository :("; + } if (!response.ok) throw `Repository sent HTTP code ${response.status} :(`; - if ("error_json" in response) + + try { + var json = await response.json(); + } catch(e) { + console.error("Haketilo:", e); throw "Repository's response is not valid JSON :("; + } const $id = `https://hydrilla.koszko.org/schemas/api_query_result-1.0.1.schema.json`; const schema = haketilo_schemas[$id]; - const result = haketilo_validator.validate(response.json, schema); + const result = haketilo_validator.validate(json, schema); if (result.errors.length > 0) { console.error("Haketilo:", result.errors); const reg = new RegExp(schema.properties.$schema.pattern); - if (response.json.$schema && !reg.test(response.json.$schema)) + if (json.$schema && !reg.test(json.$schema)) throw "Results were served using unsupported Hydrilla API version. You might need to update Haketilo."; throw "Results were served using a nonconforming response format."; } - return response.json.mappings; + return json.mappings; } const populate_results = async () => { |