diff options
author | vnugent <public@vaughnnugent.com> | 2023-09-06 13:51:13 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-09-06 13:51:13 -0400 |
commit | cd8e865dad326f85ff2357ad90bbd6aa65dea68e (patch) | |
tree | 0d4a0bb8bafc4f807407e99c5e6bf4e1cb34217a /extension/src/entries/background/main.ts |
initial commit
Diffstat (limited to 'extension/src/entries/background/main.ts')
-rw-r--r-- | extension/src/entries/background/main.ts | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/extension/src/entries/background/main.ts b/extension/src/entries/background/main.ts new file mode 100644 index 0000000..b4080d6 --- /dev/null +++ b/extension/src/entries/background/main.ts @@ -0,0 +1,105 @@ +// Copyright (C) 2023 Vaughn Nugent +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +import { runtime } from "webextension-polyfill"; +import { HistoryEvent, useHistory } from "./history"; +import { useNostrApi } from "./nostr-api"; +import { useIdentityApi } from "./identity-api"; +import { useSettings } from "./settings"; +import { onMessage } from "webext-bridge/background"; +import { useAuthApi } from "./auth-api"; +import { JsonObject } from "type-fest"; + +//Init the history api +useHistory(); + +runtime.onInstalled.addListener(() => { + console.info("Extension installed successfully"); +}); + + +//Register settings handlers +const { onGetSiteConfig, onSetSitConfig } = useSettings(); + +onMessage('getSiteConfig', onGetSiteConfig); +onMessage('setSiteConfig', onSetSitConfig); + +//Register the api handlers +const { onGetProfile, onGetStatus, onLogin, onLogout, protect } = useAuthApi(); + +onMessage('getProfile', onGetProfile); +onMessage('getStatus', onGetStatus); +onMessage('login', onLogin); +onMessage('logout', onLogout); + +//Register the identity handlers +const { onCreateIdentity, onUpdateIdentity } = useIdentityApi(); + +onMessage('createIdentity', onCreateIdentity); +onMessage('updateIdentity', onUpdateIdentity); + +//Register the nostr handlers +const { + onGetPubKey, + onSelectKey, + onSignEvent, + onGetAllKeys, + onGetRelays, + onNip04Decrypt, + onNip04Encrypt, + onDeleteKey, + onSetRelay +} = useNostrApi(); + +onMessage('getPublicKey', onGetPubKey); +onMessage('selectKey', onSelectKey); +onMessage('signEvent', onSignEvent); +onMessage('getAllKeys', onGetAllKeys); +onMessage('getRelays', onGetRelays); +onMessage('setRelay', onSetRelay); +onMessage('deleteKey', onDeleteKey); +onMessage('nip04.decrypt', onNip04Decrypt); +onMessage('nip04.encrypt', onNip04Encrypt); + +//Use history api +const { getHistory, clearHistory, removeItem, pushEvent } = useHistory(); + +enum HistoryType { + get = 'get', + clear = 'clear', + remove = 'remove', + push = 'push' +} + +interface HistoryMessage extends JsonObject { + action: HistoryType, + event: string +} + +onMessage <HistoryMessage>('history', protect(async ({data}) =>{ + switch(data.action){ + case HistoryType.get: + return getHistory(); + case HistoryType.clear: + clearHistory(); + break; + case HistoryType.remove: + removeItem(data.event); + break; + case HistoryType.push: + pushEvent(data.event); + break; + } +}))
\ No newline at end of file |