Added prettier config. All web files (js/html) should be formatted using prettier!

This commit is contained in:
Ryan Breen
2023-01-22 22:17:07 -05:00
parent 16589e2313
commit ea3805dd02
9 changed files with 216 additions and 137 deletions

7
.prettierrc Normal file
View File

@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"arrowParens": "avoid"
}

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}

View File

@@ -1,21 +1,28 @@
import { generatePrivateKey, getPublicKey, signEvent, nip04, nip19 } from "nostr-tools";
import {
generatePrivateKey,
getPublicKey,
signEvent,
nip04,
nip19,
} from 'nostr-tools';
const storage = browser.storage.local;
browser.runtime.onInstalled.addListener(async ({ reason }) => {
// I would like to be able to skip this for development purposes
let ignoreHook = (await storage.get('ignoreInstallHook')).ignoreInstallHook
let ignoreHook = (await storage.get('ignoreInstallHook')).ignoreInstallHook;
if (ignoreHook === true) {
return;
}
if (['install'].includes(reason)) {
browser.tabs.create({
url: 'https://ursus.camp/nostore'
})
url: 'https://ursus.camp/nostore',
});
}
});
browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) => {
browser.runtime.onMessage.addListener(
async (message, _sender, sendResponse) => {
console.log(message);
switch (message.kind) {
@@ -84,7 +91,8 @@ browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) =>
default:
break;
}
});
}
);
async function get(item) {
return (await storage.get(item))[item];
@@ -102,7 +110,9 @@ async function getOrSetDefault(key, def) {
async function initialize() {
await getOrSetDefault('profileIndex', 0);
await getOrSetDefault('profiles', [{name: 'Default', privKey: generatePrivateKey(), hosts: []}]);
await getOrSetDefault('profiles', [
{ name: 'Default', privKey: generatePrivateKey(), hosts: [] },
]);
}
async function getNsecKey() {
@@ -162,7 +172,11 @@ async function currentProfile() {
async function newProfile() {
let profiles = await get('profiles');
const newProfile = {name: 'New Profile', privKey: generatePrivateKey(), hosts: []};
const newProfile = {
name: 'New Profile',
privKey: generatePrivateKey(),
hosts: [],
};
profiles.push(newProfile);
await storage.set({ profiles });
return profiles.length - 1;

View File

@@ -2,13 +2,17 @@ let script = document.createElement('script');
script.setAttribute('src', browser.runtime.getURL('nostr.build.js'));
document.body.appendChild(script);
window.addEventListener('message', async (message) => {
const validEvents = ['getPubKey', 'signEvent', 'getRelays', 'nip04.encrypt', 'nip04.decrypt'];
window.addEventListener('message', async message => {
const validEvents = [
'getPubKey',
'signEvent',
'getRelays',
'nip04.encrypt',
'nip04.decrypt',
];
let { kind, reqId, payload } = message.data;
if (!validEvents.includes(kind))
return;
if (!validEvents.includes(kind)) return;
console.log(`Event ${reqId}: Content script received message kind ${kind}, payload: `, payload);
payload = await browser.runtime.sendMessage({ kind, payload });
kind = `return_${kind}`;

View File

@@ -16,7 +16,7 @@ window.nostr = {
// This is here for Alby comatibility. This is not part of the NIP-07 standard.
// I have found at least one site, nostr.band, which expects it to be present.
async enable() {
return {enabled: true}
return { enabled: true };
},
broadcast(kind, payload) {
@@ -28,24 +28,34 @@ window.nostr = {
});
},
nip04: {
async encrypt(pubKey, plainText) {
return await window.nostr.broadcast('nip04.encrypt', {pubKey, plainText});
return await window.nostr.broadcast('nip04.encrypt', {
pubKey,
plainText,
});
},
async decrypt(pubKey, cipherText) {
return await window.nostr.broadcast('nip04.decrypt', {pubKey, cipherText});
}
}
}
return await window.nostr.broadcast('nip04.decrypt', {
pubKey,
cipherText,
});
},
},
};
window.addEventListener('message', (message) => {
const validEvents = ['getPubKey', 'signEvent', 'getRelays', 'nip04.encrypt', 'nip04.decrypt'].map(e => `return_${e}`);
window.addEventListener('message', message => {
const validEvents = [
'getPubKey',
'signEvent',
'getRelays',
'nip04.encrypt',
'nip04.decrypt',
].map(e => `return_${e}`);
let { kind, reqId, payload } = message.data;
if (!validEvents.includes(kind))
return;
if (!validEvents.includes(kind)) return;
console.log(`Event ${reqId}: Received payload:`, payload);
window.nostr.requests[reqId](payload);

View File

@@ -1,11 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" href="popup.css">
<script defer src="popup.build.js"></script>
</head>
<body x-data="popup">
<div class="profiles">
<label for="profile">Active Profile</label>
@@ -16,7 +18,8 @@
</template>
</select>
<button @click="newProfile">New</button>
<button @click="confirmDelete = true" x-show="!confirmDelete" :disabled="profileNames.length <= 1">Delete</button>
<button @click="confirmDelete = true" x-show="!confirmDelete"
:disabled="profileNames.length <= 1">Delete</button>
<button @click="await deleteProfile()" x-show="confirmDelete">Confirm Delete</button>
</div>
</div>
@@ -65,4 +68,5 @@
All private keys are stored in the extension's sequestered local browser storage.
</div>
</body>
</html>

View File

@@ -1,4 +1,4 @@
import Alpine from "alpinejs";
import Alpine from 'alpinejs';
window.Alpine = Alpine;
Alpine.data('popup', () => ({
@@ -15,7 +15,7 @@ Alpine.data('popup', () => ({
confirmDelete: false,
async init() {
console.log("Initializing backend.");
console.log('Initializing backend.');
await browser.runtime.sendMessage({ kind: 'init' });
this.$watch('profileIndex', async () => {
@@ -46,12 +46,17 @@ Alpine.data('popup', () => ({
// Becauset the popup state resets every time it open, we use null as a guard. That way
// whenever the user opens the popup, it doesn't automatically reset the current profile
if (this.profileIndex !== null) {
await browser.runtime.sendMessage({kind: 'setProfileIndex', payload: this.profileIndex});
await browser.runtime.sendMessage({
kind: 'setProfileIndex',
payload: this.profileIndex,
});
}
},
async getNsecKey() {
this.privKey = await browser.runtime.sendMessage({kind: 'getNsecKey'});
this.privKey = await browser.runtime.sendMessage({
kind: 'getNsecKey',
});
this.pristinePrivKey = this.privKey;
},
@@ -64,7 +69,9 @@ Alpine.data('popup', () => ({
},
async getProfileNames() {
this.profileNames = await browser.runtime.sendMessage({kind: 'getProfileNames'});
this.profileNames = await browser.runtime.sendMessage({
kind: 'getProfileNames',
});
},
async getName() {
@@ -73,11 +80,15 @@ Alpine.data('popup', () => ({
},
async getProfileIndex() {
this.profileIndex = await browser.runtime.sendMessage({kind: 'getProfileIndex'});
this.profileIndex = await browser.runtime.sendMessage({
kind: 'getProfileIndex',
});
},
async newProfile() {
let newIndex = await browser.runtime.sendMessage({kind: 'newProfile'});
let newIndex = await browser.runtime.sendMessage({
kind: 'newProfile',
});
await this.refreshProfile();
this.profileIndex = newIndex;
},
@@ -85,7 +96,10 @@ Alpine.data('popup', () => ({
async saveProfile() {
let { name, privKey, hosts } = this;
let profile = { name, privKey, hosts };
await browser.runtime.sendMessage({kind: 'saveProfile', payload: profile});
await browser.runtime.sendMessage({
kind: 'saveProfile',
payload: profile,
});
await this.refreshProfile();
},
@@ -104,13 +118,15 @@ Alpine.data('popup', () => ({
// Properties
get hasValidPubKey() {
return typeof(this.pubKey) === 'string' && this.pubKey.length > 0;
return typeof this.pubKey === 'string' && this.pubKey.length > 0;
},
get needsSaving() {
return (this.privKey !== this.pristinePrivKey || this.name !== this.pristineName);
}
return (
this.privKey !== this.pristinePrivKey ||
this.name !== this.pristineName
);
},
}));
Alpine.start();

18
package-lock.json generated
View File

@@ -12,6 +12,9 @@
"alpinejs": "^3.10.5",
"esbuild": "^0.16.17",
"nostr-tools": "^1.1.1"
},
"devDependencies": {
"prettier": "^2.8.3"
}
},
"node_modules/@esbuild/android-arm": {
@@ -492,6 +495,21 @@
"@scure/bip32": "^1.1.1",
"@scure/bip39": "^1.1.0"
}
},
"node_modules/prettier": {
"version": "2.8.3",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.3.tgz",
"integrity": "sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw==",
"dev": true,
"bin": {
"prettier": "bin-prettier.js"
},
"engines": {
"node": ">=10.13.0"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
}
}
}

View File

@@ -19,5 +19,8 @@
"alpinejs": "^3.10.5",
"esbuild": "^0.16.17",
"nostr-tools": "^1.1.1"
},
"devDependencies": {
"prettier": "^2.8.3"
}
}