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) => { let profiles = [
console.log('generating a key!'); {name: 'Default', privKey: generatePrivateKey(), hosts: [
console.log(generatePrivateKey()); {host: 'yosup.app', allowed: true},
console.log("Received request: ", request); {host: 'iris.to', allowed: false},
]},
{name: 'Extra', privKey: generatePrivateKey(), hosts: []},
];
if (request.greeting === "hello") let activeProfile = 0;
sendResponse({ farewell: "goodbye" });
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; font-family: system-ui;
} }
#priv-key { label {
display: inline-block;
width: 100px;
}
input {
width: 32em;
}
#priv-key, #pub-key {
font-family: monospace; font-family: monospace;
} }

View File

@@ -8,16 +8,22 @@
<body x-data="popup"> <body x-data="popup">
<div class="profiles"> <div class="profiles">
<label for="profile">Profile</label> <label for="profile">Profile</label>
<select x-model="profile" name="profile" id="profile" @change="getPrivKeyForProfile()"> <select x-model="activeProfile" name="profile" id="profile" @change="getPrivKeyForProfile()">
<template x-for="prof in profiles"> <template x-for="(prof, index) in profiles">
<option :value="prof" x-text="prof"></option> <option x-text="prof.name" :value="index"></option>
</template> </template>
</select> </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>
<div class="key"> <div class="key">
<label for="priv-key">Private Key</label> <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>
<div class="buttons"> <div class="buttons">
@@ -25,13 +31,18 @@
<button @click="saveKey()">Save</button> <button @click="saveKey()">Save</button>
</div> </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> <h3>Allowed Sites</h3>
<table> <table>
<template x-for="(site, index) in allowedSites" :key="site.site"> <template x-for="(host, index) in hosts" :key="host.host">
<tr> <tr>
<td class="allowed" x-text="site.allowed ? 'Yes' : 'No'"></td> <td class="allowed" x-text="host.allowed ? 'Yes' : 'No'"></td>
<td x-text="site.site"></td> <td x-text="host.host"></td>
<td><button @click="deleteSite(index)">Delete</button></td> <td><button @click="deleteSite(index)">Delete</button></td>
</tr> </tr>
</template> </template>

View File

@@ -2,42 +2,62 @@ import Alpine from "alpinejs";
window.Alpine = Alpine; window.Alpine = Alpine;
Alpine.data('popup', () => ({ Alpine.data('popup', () => ({
privKey: '', pubKey: '',
profiles: [], profiles: [{name: 'Default', privKey: '', hosts: []}],
profile: '', activeProfile: 0,
visibleKey: false, visibleKey: false,
allowedSites: [],
async init() { async init() {
await this.getProfiles(); await this.getProfiles();
await this.getPrivKeyForProfile(); await this.getPrivKeyForProfile();
await this.getAllowedSites();
}, },
saveKey() { async saveKey() {
console.log(`Saving key ${this.privKey}`); await this.getPubKey();
}, },
async getProfiles() { async getProfiles() {
this.profiles = ['Default', 'Extra']; this.profiles = await browser.runtime.sendMessage({kind: 'getProfiles'});
this.profile = 'Default'; this.activeProfile = await browser.runtime.sendMessage({kind: 'getActiveProfile'});
}, },
async getPrivKeyForProfile() { async getPrivKeyForProfile() {
this.privKey = this.profile; await this.getPubKey();
},
async getAllowedSites() {
this.allowedSites = [
{site: 'yosupp.app', allowed: true},
{site: 'iris.to', allowed: false},
];
}, },
async deleteSite(index) { async deleteSite(index) {
let newSites = [...this.allowedSites]; confirm("hello");
let newSites = [...this.hosts];
newSites.splice(index, 1); 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;
} }
})); }));