tweaking the UI, not working quite yet. may totally revise

This commit is contained in:
Ryan Breen
2023-01-15 21:48:00 -05:00
parent abc84d4694
commit b84be8c351
3 changed files with 108 additions and 42 deletions

View File

@@ -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;
}