Skip to content

Commit

Permalink
extension: allow running without the backend if host set to empty string
Browse files Browse the repository at this point in the history
relevant to #120
  • Loading branch information
karlicoss committed Nov 4, 2020
1 parent ca353db commit e867b26
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Visits have the following fields:

- source (or indexer) is any function that extract visits from raw files and generates a stream of visits (i.e. =Iterable[Visit]=).
- promnesia indexer goes through the sources, specified in config, collects the visits and puts in the database
- promnesia server reads visits form the database, and them to the extension
- promnesia server reads visits form the database, and sends them to the extension


Now let's consider some *concrete* examples of different kinds of Visits:
Expand Down
15 changes: 13 additions & 2 deletions extension/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type {Locator, Src, Url, Second, JsonArray, JsonObject, AwareDate, NaiveDate} from './common';
import {Visit, Visits, Blacklisted, unwrap, Methods, ldebug, linfo, lerror, lwarn} from './common';
import {get_options_async, setOptions, THIS_BROWSER_TAG} from './options';
import {get_options_async, getOptions, setOptions, THIS_BROWSER_TAG} from './options';
import {chromeTabsExecuteScriptAsync, chromeTabsInsertCSS, chromeTabsQueryAsync, chromeRuntimeGetPlatformInfo, chromeTabsGet} from './async_chrome';
import {showTabNotification, showBlackListedNotification, showIgnoredNotification, defensify, notify} from './notifications';
import {Blacklist} from './blacklist'
Expand Down Expand Up @@ -64,7 +64,17 @@ function rawToVisits(vis: JsonObject): Visits {


async function queryBackendCommon<R>(params, endp: string): Promise<R> {
const opts = await get_options_async();
const opts = await getOptions()
if (opts.host == '') {
// the user only wants to use browser visits?
// todo: won't work for all endpoints, but can think how to fix later..
if (endp == 'visits') {
return (({visits: []} : any): R)
} else {
throw Error(`'${endp}' isn't implemented without the backend yet. Please set host in the extension settings.`)
}
}

const endpoint = `${opts.host}/${endp}`;
// TODO cors mode?
const response = await fetch(endpoint, {
Expand All @@ -91,6 +101,7 @@ async function getBackendVisits(u: Url): Promise<Visits> {


// TODO include browser visits here too?
// see /~https://github.com/karlicoss/promnesia/issues/120
export async function searchVisits(u: Url): Promise<Visits> {
return queryBackendCommon<JsonObject>({url: u}, 'search').then(rawToVisits);
}
Expand Down
2 changes: 1 addition & 1 deletion extension/src/options_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<table>
<tr>
<td><label for="host_id">Host</label> </td>
<td><label for="host_id">Host (set to empty string if you want to use in-browser visits only)</label> </td>
<td><input type="URL" id="host_id"> </td>
</tr>
<!-- Warn about auth token being plaintext -->
Expand Down
5 changes: 2 additions & 3 deletions extension/src/options_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,15 @@ unwrap(document.getElementById('backend_status_id')).addEventListener('click', d
},
}, second).then(res => {
if (!res.ok) {
throw `Backend error: ${res.status} ${res.statusText}` // TODO
throw `Backend error: ${res.status} ${res.statusText}`
}
return res;
}).then(async res => {
// TODO ugh. need to reject if ok is false...
const resj = await res.json()
// TODO log debug?
alert(`Success! ${JSON.stringify(resj)}`)
}, err => {
alertError(err);
alertError(`${err}. See /~https://github.com/karlicoss/promnesia/blob/master/doc/TROUBLESHOOTING.org`);
});
}));

Expand Down

0 comments on commit e867b26

Please sign in to comment.