From 7117187e2c28072d77f64aef6712ded7baca8d75 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 11 Jun 2023 10:06:22 -0300 Subject: [PATCH] fix notifications for people who haven't allowed them. --- extension/background.js | 40 ++++------------------------------------ extension/common.js | 23 +++++++++++++++++++++++ extension/manifest.json | 2 +- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/extension/background.js b/extension/background.js index 7deefbf..e37e5ae 100644 --- a/extension/background.js +++ b/extension/background.js @@ -12,7 +12,8 @@ import {Mutex} from 'async-mutex' import { NO_PERMISSIONS_REQUIRED, getPermissionStatus, - updatePermission + updatePermission, + showNotification } from './common' const {encrypt, decrypt} = nip04 @@ -101,44 +102,11 @@ async function handleContentScriptMessage({type, params, host}) { if (allowed === true) { // authorized, proceed releasePromptMutex() - browser.notifications - .create(undefined, { - type: 'basic', - title: `${type} allowed for ${host}`, - message: JSON.stringify( - params?.event - ? { - kind: params.event.kind, - content: params.event.content, - tags: params.event.tags - } - : params, - null, - 2 - ), - iconUrl: 'icons/48x48.png' - }) - .then(console.log) - .catch(console.log) + showNotification(host, allowed, type, params) } else if (allowed === false) { // denied, just refuse immediately releasePromptMutex() - browser.notifications.create(undefined, { - type: 'basic', - title: `${type} denied for ${host}`, - message: JSON.stringify( - params?.event - ? { - kind: params.event.kind, - content: params.event.content, - tags: params.event.tags - } - : params, - null, - 2 - ), - iconUrl: 'icons/denied48.png' - }) + showNotification(host, allowed, type, params) return { error: 'denied' } diff --git a/extension/common.js b/extension/common.js index 5c9284b..9b9185b 100644 --- a/extension/common.js +++ b/extension/common.js @@ -91,3 +91,26 @@ export async function removePermissions(host, accept, type) { delete policies[host]?.[accept]?.[type] browser.storage.local.set({policies}) } + +export async function showNotification(host, answer, type, params) { + let ok = await browser.storage.local.get('notifications') + if (ok) { + let action = answer ? 'allowed' : 'denied' + browser.notifications.create(undefined, { + type: 'basic', + title: `${type} ${action} for ${host}`, + message: JSON.stringify( + params?.event + ? { + kind: params.event.kind, + content: params.event.content, + tags: params.event.tags + } + : params, + null, + 2 + ), + iconUrl: 'icons/48x48.png' + }) + } +} diff --git a/extension/manifest.json b/extension/manifest.json index 3a2c1ba..4ba4cd0 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -1,7 +1,7 @@ { "name": "nos2x", "description": "Nostr Signer Extension", - "version": "2.0.1", + "version": "2.0.2", "homepage_url": "https://github.com/fiatjaf/nos2x", "manifest_version": 3, "icons": {