use a mutex to prevent multiple prompts from opening in loop.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import browser from 'webextension-polyfill'
|
import browser from 'webextension-polyfill'
|
||||||
import {validateEvent, signEvent, getEventHash, getPublicKey} from 'nostr-tools'
|
import {validateEvent, signEvent, getEventHash, getPublicKey} from 'nostr-tools'
|
||||||
import {encrypt, decrypt} from 'nostr-tools/nip04'
|
import {encrypt, decrypt} from 'nostr-tools/nip04'
|
||||||
|
import {Mutex} from 'async-mutex'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
PERMISSIONS_REQUIRED,
|
PERMISSIONS_REQUIRED,
|
||||||
@@ -8,7 +9,9 @@ import {
|
|||||||
updatePermission
|
updatePermission
|
||||||
} from './common'
|
} from './common'
|
||||||
|
|
||||||
const prompts = {}
|
let openPrompt = null
|
||||||
|
let promptMutex = new Mutex()
|
||||||
|
let releasePromptMutex = () => {}
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(async (req, sender) => {
|
browser.runtime.onMessage.addListener(async (req, sender) => {
|
||||||
let {prompt} = req
|
let {prompt} = req
|
||||||
@@ -89,25 +92,28 @@ function handlePromptMessage({id, condition, host, level}, sender) {
|
|||||||
switch (condition) {
|
switch (condition) {
|
||||||
case 'forever':
|
case 'forever':
|
||||||
case 'expirable':
|
case 'expirable':
|
||||||
prompts[id]?.resolve?.()
|
openPrompt?.resolve?.()
|
||||||
updatePermission(host, {
|
updatePermission(host, {
|
||||||
level,
|
level,
|
||||||
condition
|
condition
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
case 'single':
|
case 'single':
|
||||||
prompts[id]?.resolve?.()
|
openPrompt?.resolve?.()
|
||||||
break
|
break
|
||||||
case 'no':
|
case 'no':
|
||||||
prompts[id]?.reject?.()
|
openPrompt?.reject?.()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
delete prompts[id]
|
openPrompt = null
|
||||||
|
releasePromptMutex()
|
||||||
browser.windows.remove(sender.tab.windowId)
|
browser.windows.remove(sender.tab.windowId)
|
||||||
}
|
}
|
||||||
|
|
||||||
function promptPermission(host, level, params) {
|
async function promptPermission(host, level, params) {
|
||||||
|
releasePromptMutex = await promptMutex.acquire()
|
||||||
|
|
||||||
let id = Math.random().toString().slice(4)
|
let id = Math.random().toString().slice(4)
|
||||||
let qs = new URLSearchParams({
|
let qs = new URLSearchParams({
|
||||||
host,
|
host,
|
||||||
@@ -117,13 +123,13 @@ function promptPermission(host, level, params) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
openPrompt = {resolve, reject}
|
||||||
|
|
||||||
browser.windows.create({
|
browser.windows.create({
|
||||||
url: `${browser.runtime.getURL('prompt.html')}?${qs.toString()}`,
|
url: `${browser.runtime.getURL('prompt.html')}?${qs.toString()}`,
|
||||||
type: 'popup',
|
type: 'popup',
|
||||||
width: 340,
|
width: 340,
|
||||||
height: 330
|
height: 330
|
||||||
})
|
})
|
||||||
|
|
||||||
prompts[id] = {resolve, reject}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
||||||
|
"async-mutex": "^0.3.2",
|
||||||
"esbuild": "^0.14.11",
|
"esbuild": "^0.14.11",
|
||||||
"esbuild-plugin-alias": "^0.2.1",
|
"esbuild-plugin-alias": "^0.2.1",
|
||||||
"eslint": "^8.6.0",
|
"eslint": "^8.6.0",
|
||||||
|
|||||||
12
yarn.lock
12
yarn.lock
@@ -113,6 +113,13 @@ array.prototype.flatmap@^1.2.5:
|
|||||||
define-properties "^1.1.3"
|
define-properties "^1.1.3"
|
||||||
es-abstract "^1.19.0"
|
es-abstract "^1.19.0"
|
||||||
|
|
||||||
|
async-mutex@^0.3.2:
|
||||||
|
version "0.3.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/async-mutex/-/async-mutex-0.3.2.tgz#1485eda5bda1b0ec7c8df1ac2e815757ad1831df"
|
||||||
|
integrity sha512-HuTK7E7MT7jZEh1P9GtRW9+aTWiDWWi9InbZ5hjxrnRa39KS4BW04+xLBhYNS2aXhHUIKZSw3gj4Pn1pj+qGAA==
|
||||||
|
dependencies:
|
||||||
|
tslib "^2.3.1"
|
||||||
|
|
||||||
balanced-match@^1.0.0:
|
balanced-match@^1.0.0:
|
||||||
version "1.0.2"
|
version "1.0.2"
|
||||||
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
|
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
|
||||||
@@ -1404,6 +1411,11 @@ tr46@~0.0.3:
|
|||||||
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
|
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
|
||||||
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
|
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
|
||||||
|
|
||||||
|
tslib@^2.3.1:
|
||||||
|
version "2.3.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
|
||||||
|
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==
|
||||||
|
|
||||||
tstl@^2.0.7:
|
tstl@^2.0.7:
|
||||||
version "2.5.2"
|
version "2.5.2"
|
||||||
resolved "https://registry.yarnpkg.com/tstl/-/tstl-2.5.2.tgz#655ae93192e4c18ac74495cea2bf862159775cd9"
|
resolved "https://registry.yarnpkg.com/tstl/-/tstl-2.5.2.tgz#655ae93192e4c18ac74495cea2bf862159775cd9"
|
||||||
|
|||||||
Reference in New Issue
Block a user