show notifications when a request is denied or accepted.

This commit is contained in:
fiatjaf
2023-06-11 09:07:24 -03:00
parent b82ac28a8e
commit b62d044b0a
4 changed files with 72 additions and 2 deletions

View File

@@ -101,9 +101,44 @@ async function handleContentScriptMessage({type, params, host}) {
if (allowed === true) { if (allowed === true) {
// authorized, proceed // authorized, proceed
releasePromptMutex() 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/denied48.png'
})
.then(console.log)
.catch(console.log)
} else if (allowed === false) { } else if (allowed === false) {
// denied, just refuse immediately // denied, just refuse immediately
releasePromptMutex() 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'
})
return { return {
error: 'denied' error: 'denied'
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

View File

@@ -26,6 +26,7 @@
} }
], ],
"permissions": ["storage"], "permissions": ["storage"],
"optional_permissions": ["notifications"],
"web_accessible_resources": [ "web_accessible_resources": [
{ {
"resources": ["nostr-provider.js"], "resources": ["nostr-provider.js"],

View File

@@ -13,6 +13,7 @@ function Options() {
let [policies, setPermissions] = useState() let [policies, setPermissions] = useState()
let [protocolHandler, setProtocolHandler] = useState(null) let [protocolHandler, setProtocolHandler] = useState(null)
let [hidingPrivateKey, hidePrivateKey] = useState(true) let [hidingPrivateKey, hidePrivateKey] = useState(true)
let [showNotifications, setNotifications] = useState(false)
let [message, setMessage] = useState('') let [message, setMessage] = useState('')
const showMessage = useCallback(msg => { const showMessage = useCallback(msg => {
@@ -22,7 +23,7 @@ function Options() {
useEffect(() => { useEffect(() => {
browser.storage.local browser.storage.local
.get(['private_key', 'relays', 'protocol_handler']) .get(['private_key', 'relays', 'protocol_handler', 'notifications'])
.then(results => { .then(results => {
if (results.private_key) { if (results.private_key) {
setPrivKey(nip19.nsecEncode(results.private_key)) setPrivKey(nip19.nsecEncode(results.private_key))
@@ -40,6 +41,9 @@ function Options() {
if (results.protocol_handler) { if (results.protocol_handler) {
setProtocolHandler(results.protocol_handler) setProtocolHandler(results.protocol_handler)
} }
if (results.notifications) {
setNotifications(true)
}
}) })
}, []) }, [])
@@ -156,7 +160,23 @@ function Options() {
</label> </label>
{policies?.length > 0 && ( {policies?.length > 0 && (
<> <>
<div style={{display: 'flex', alignItems: 'center'}}>
<h2>policies</h2> <h2>policies</h2>
<label
style={{
display: 'flex',
alignItems: 'center',
marginLeft: '14px'
}}
>
show a notification when a permission is used by a page
<input
type="checkbox"
checked={showNotifications}
onClick={handleNotifications}
/>
</label>
</div>
<table> <table>
<thead> <thead>
<tr> <tr>
@@ -355,6 +375,20 @@ function Options() {
} }
} }
async function handleNotifications() {
if (showNotifications) {
setNotifications(false)
} else {
let granted = await browser.permissions.request({
permissions: ['notifications']
})
if (granted) {
await browser.storage.local.set({notifications: true})
setNotifications(true)
}
}
}
async function saveRelays() { async function saveRelays() {
await browser.storage.local.set({ await browser.storage.local.set({
relays: Object.fromEntries( relays: Object.fromEntries(