diff --git a/Shared (Extension)/Resources/background.js b/Shared (Extension)/Resources/background.js index 90fb50f..af609b5 100644 --- a/Shared (Extension)/Resources/background.js +++ b/Shared (Extension)/Resources/background.js @@ -1,38 +1,90 @@ -import { generatePrivateKey, getPublicKey } from "nostr-tools"; +import { generatePrivateKey , getPublicKey } from "nostr-tools"; const storage = browser.storage.local; -let profiles = [ - {name: 'Default', privKey: generatePrivateKey(), hosts: [ - {host: 'yosup.app', allowed: true}, - {host: 'iris.to', allowed: false}, - ]}, - {name: 'Extra', privKey: generatePrivateKey(), hosts: []}, -]; - -let profileIndex = 0; - browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) => { console.log(message); - if (message.kind === 'getPubKey') { - const privKey = getPublicKey(message.payload); - sendResponse(privKey); - } else if (message.kind === 'newKey') { - const privKey = generatePrivateKey(); - sendResponse(privKey); - } else if (message.kind === 'getProfiles') { - sendResponse(profiles); - } else if (message.kind === 'getProfileIndex') { - sendResponse(await getProfileIndex()); - } else if (message.kind === 'setProfileIndex') { - await setProfileIndex(message.payload); + + switch (message.kind) { + case 'init': + await initialize(); + break; + case 'getProfiles': + let names = await getProfileNames(); + sendResponse(names); + break; + case 'getProfileIndex': + sendResponse(await getProfileIndex()); + break; + case 'setProfileIndex': + await setProfileIndex(message.payload); + break; + case 'getActiveProfile': + let ap = await currentProfile(); + sendResponse(ap); + break; + case 'newKey': + sendResponse(generatePrivateKey()); + break; + case 'newProfile': + sendResponse(await newProfile()); + break; + default: + break; } }); +async function get(item) { + return (await storage.get(item))[item]; +} + +async function getOrSetDefault(key, def) { + let val = storage.get(key)[key]; + if (!val) { + await storage.set({[key]: def}); + return def; + } + + return val; +} + +async function initialize() { + await getOrSetDefault('profileIndex', 0); + await getOrSetDefault('profiles', [{name: 'Default', privKey: generatePrivateKey(), hosts: []}]); +} + async function getProfileIndex() { - return await storage.get('profileIndex').profileIndex; + return (await storage.get('profileIndex')).profileIndex; } async function setProfileIndex(profileIndex) { await storage.set({profileIndex}); +} + +async function currentProfile() { + let index = (await storage.get('profileIndex')).profileIndex; + let profiles = (await storage.get('profiles')).profiles; + + if (!profiles || !profiles[index]) { + let newProfile = {name: 'Default', privKey: generatePrivateKey(), hosts: []}; + await storage.set({profileIndex: 0}); + await storage.set({profiles: [newProfile]}); + return newProfile; + } + + return profiles[index]; +} + +async function getProfileNames() { + let profiles = (await storage.get({profiles: []})).profiles; + console.log('Profiles: ', profiles); + return profiles.map(p => p.name); +} + +async function newProfile() { + let profiles = await get('profiles'); + const newProfile = {name: 'New Profile', privKey: generatePrivateKey(), hosts: []}; + profiles.push(newProfile); + await storage.set({profiles}); + return profiles.index - 1; } \ No newline at end of file diff --git a/Shared (Extension)/Resources/popup.html b/Shared (Extension)/Resources/popup.html index 08c55a4..5ca7abe 100644 --- a/Shared (Extension)/Resources/popup.html +++ b/Shared (Extension)/Resources/popup.html @@ -10,7 +10,7 @@ @@ -18,12 +18,12 @@