Skip to content

Commit

Permalink
Make permissions.request run on user-action
Browse files Browse the repository at this point in the history
Don't put a promise call in between, FF doesn't like that.
  • Loading branch information
stefanw committed Aug 24, 2021
1 parent 932dd4a commit 446e631
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const defaults = {
providerOptions: {}
}

let currentPermissions = null

function showOptions () {
const provider = document.getElementById('provider').value
Array.from(document.getElementsByClassName('provider-options-container')).forEach(el => {
Expand Down Expand Up @@ -90,6 +92,8 @@ function restore () {
ul.appendChild(li)
})
})

getPermissions()
}

function save () {
Expand All @@ -116,23 +120,30 @@ function save () {
}

browser.storage.sync.set(values)
checkPermissions(provider)
requestPermissions(providers[provider].permissions)
}

function checkPermissions (key) {
const provider = providers[key]
if (provider.permissions) {
browser.permissions.getAll().then((permissions) => {
const neededPermissions = []
console.log(provider.permissions, permissions.origins)
provider.permissions.forEach(p => {
if (!permissions.origins.includes(p)) {
neededPermissions.push(p)
}
})
if (neededPermissions.length > 0) {
browser.permissions.request({ origins: neededPermissions })
}
function getPermissions () {
browser.permissions.getAll().then((permissions) => {
currentPermissions = permissions
return permissions
})
}

function requestPermissions (providerPermissions) {
if (!providerPermissions) {
return
}
const neededPermissions = []
console.log(providerPermissions, currentPermissions.origins)
providerPermissions.forEach(p => {
if (!currentPermissions.origins.includes(p)) {
neededPermissions.push(p)
}
})
if (neededPermissions.length > 0) {
browser.permissions.request({ origins: neededPermissions }).then(() => {
getPermissions()
})
}
}
Expand Down

0 comments on commit 446e631

Please sign in to comment.