starting the plumbing for the extension

This commit is contained in:
Ryan Breen
2023-01-14 15:03:52 -05:00
parent f5f800ee72
commit e1b3e919b1
4 changed files with 91 additions and 35 deletions

View File

@@ -1,10 +1,26 @@
import { generatePrivateKey } from "nostr-tools";
import { generatePrivateKey, getPublicKey } from "nostr-tools";
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('generating a key!');
console.log(generatePrivateKey());
console.log("Received request: ", request);
let profiles = [
{name: 'Default', privKey: generatePrivateKey(), hosts: [
{host: 'yosup.app', allowed: true},
{host: 'iris.to', allowed: false},
]},
{name: 'Extra', privKey: generatePrivateKey(), hosts: []},
];
if (request.greeting === "hello")
sendResponse({ farewell: "goodbye" });
let activeProfile = 0;
browser.runtime.onMessage.addListener((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 === 'getActiveProfile') {
sendResponse(activeProfile);
}
});

View File

@@ -9,7 +9,16 @@ body {
font-family: system-ui;
}
#priv-key {
label {
display: inline-block;
width: 100px;
}
input {
width: 32em;
}
#priv-key, #pub-key {
font-family: monospace;
}

View File

@@ -8,16 +8,22 @@
<body x-data="popup">
<div class="profiles">
<label for="profile">Profile</label>
<select x-model="profile" name="profile" id="profile" @change="getPrivKeyForProfile()">
<template x-for="prof in profiles">
<option :value="prof" x-text="prof"></option>
<select x-model="activeProfile" name="profile" id="profile" @change="getPrivKeyForProfile()">
<template x-for="(prof, index) in profiles">
<option x-text="prof.name" :value="index"></option>
</template>
</select>
<button @click="newProfile">New</button>
</div>
<div class="profile-name">
<label for="profile-name">Profile Name</label>
<input type="text" id="profile-name" x-model="profile.name">
</div>
<div class="key">
<label for="priv-key">Private Key</label>
<input id="priv-key" x-model="privKey" :type="visibleKey ? 'text' : 'password'" width="65">
<input id="priv-key" x-model="profile.privKey" :type="visibleKey ? 'text' : 'password'">
</div>
<div class="buttons">
@@ -25,13 +31,18 @@
<button @click="saveKey()">Save</button>
</div>
<div class="allowed-sites" x-show="allowedSites.length > 0">
<div x-show="hasValidPubKey">
<label for="pub-key">Pub Key:</label>
<input type="text" id="pub-key" x-model="pubKey" disabled>
</div>
<div class="allowed-sites" x-show="hosts.length > 0">
<h3>Allowed Sites</h3>
<table>
<template x-for="(site, index) in allowedSites" :key="site.site">
<template x-for="(host, index) in hosts" :key="host.host">
<tr>
<td class="allowed" x-text="site.allowed ? 'Yes' : 'No'"></td>
<td x-text="site.site"></td>
<td class="allowed" x-text="host.allowed ? 'Yes' : 'No'"></td>
<td x-text="host.host"></td>
<td><button @click="deleteSite(index)">Delete</button></td>
</tr>
</template>

View File

@@ -2,42 +2,62 @@ import Alpine from "alpinejs";
window.Alpine = Alpine;
Alpine.data('popup', () => ({
privKey: '',
profiles: [],
profile: '',
pubKey: '',
profiles: [{name: 'Default', privKey: '', hosts: []}],
activeProfile: 0,
visibleKey: false,
allowedSites: [],
async init() {
await this.getProfiles();
await this.getPrivKeyForProfile();
await this.getAllowedSites();
},
saveKey() {
console.log(`Saving key ${this.privKey}`);
async saveKey() {
await this.getPubKey();
},
async getProfiles() {
this.profiles = ['Default', 'Extra'];
this.profile = 'Default';
this.profiles = await browser.runtime.sendMessage({kind: 'getProfiles'});
this.activeProfile = await browser.runtime.sendMessage({kind: 'getActiveProfile'});
},
async getPrivKeyForProfile() {
this.privKey = this.profile;
},
async getAllowedSites() {
this.allowedSites = [
{site: 'yosupp.app', allowed: true},
{site: 'iris.to', allowed: false},
];
await this.getPubKey();
},
async deleteSite(index) {
let newSites = [...this.allowedSites];
confirm("hello");
let newSites = [...this.hosts];
newSites.splice(index, 1);
this.allowedSites = newSites;
this.hosts = newSites;
},
async getPubKey() {
this.pubKey = await browser.runtime.sendMessage({kind: 'getPubKey', payload: this.profile.privKey});
console.log('Pub key: ', this.pubKey);
},
async newProfile() {
let newKey = await browser.runtime.sendMessage({kind: 'newKey'});
const newProfile = {name: 'New Profile', privKey: newKey};
this.profiles.push(newProfile);
this.activeProfile = this.profiles.length - 1;
},
get hasValidPubKey() {
return typeof(this.pubKey) === 'string' && this.pubKey.length > 0;
},
get profile() {
return this.profiles[this.activeProfile];
},
get hosts() {
return this.profile.hosts;
},
set hosts(value) {
this.profiles[this.activeProfile].hosts = value;
}
}));