问题描述
我正在使用在这里
我在request
方法上遇到问题,其中所有权限请求都导致:
I'm having a problem with the request
method, wherein all of my permissions requests result in:
Error: permissions.request may only be called from a user input handler
您可以在Firefox中通过调试任何插件或扩展并将其输入browser.permissions.request({origins: ["https://google.com/*"]})
到控制台中来生成它.
You can produce this in firefox by debugging any addon or extension and entering browser.permissions.request({origins: ["https://google.com/*"]})
into the console.
我很难理解权限请求必须在父堆栈跟踪中始终具有用户输入事件回调.我使用的是Vue.js,我的权限归因于用户互动,但我的用户互动与其触发的事件脱钩.
I find it hard to swallow that a permissions request must always have a user input event callback in the parent stack trace. I'm using Vue.js, and my Permissions are due to user interaction, but my user interactions are decoupled from the events they trigger.
- 什么算作
user input handler
? - 为什么它会这样工作?
- 有一个好的解决方法吗?
推荐答案
我想用一些代码示例添加到安德鲁的答案中.
I'd like to add onto Andrew's answer with some code examples.
事实证明,承诺链破坏了浏览器关于由用户输入处理程序触发的内容的概念.以下面的代码为例:
As it turns out, promise chains destroy the browser's notion of what is and isn't triggered by a user input handler. Take the code below, for example:
document.getElementById('foo').addEventListener('click', event => {
browser.permissions.request({origins: ["https://google.com/*"]})
})
此代码可以正常工作.我最初以为Vue.js独特的事件处理框架正在吞噬我的浏览器事件",例如执行<div @click="somefunc"></div>
时.只要您将权限请求放在somefunc
中,这实际上就可以正常工作.
This code works as expected. I originally assumed that it was Vue.js's unique event handling framework that was eating my "browser events", such as when you do <div @click="somefunc"></div>
. This actually works just fine, as long as you put your permissions request in somefunc
.
现在变得很有趣.如果将您的权限请求替换为一个可解决的承诺,然后再执行一个权限请求,则VIOLA!
Now it gets fun. If you replace your permissions request with a promise that resolves and then does a permissions request, VIOLA!
Promise.resolve('foobar').then(foobar => {
browser.permissions.request({origins: ["https://google.com/*"]})
})
结果:
Error: permissions.request may only be called from a user input handler
我猜想它与堆栈跟踪有关.如果许可请求发生在promise链中,则Firefox无法检测到许可来自堆栈,其根源是用户输入事件.
I'm going to guess it has to do with stack traces. Firefox can't detect that a permission came from a stack with a user input event at the root if the permissions request happens in a promise chain.
我认为这是一个非常糟糕的设计选择.我的应用程序很大(> 4K LoC),并且为了使其简单起见,我依赖于Promise链来使意大利面条远离.这削弱了我编写干净代码的能力,结果,我不再要求optional_permissions
,而是仅在需要时提示用户许可,而在安装时过度允许.
I consider this to be a pretty egregious design choice. My app is large (>4K LoC) and to keep it simple I rely on promise chains to keep the spaghetti away. This has crippled my ability to write clean code, and as a result, I've moved from asking for optional_permissions
and then prompting the user for permissions only when needed to just being overly permissive at the time of installation.
GG,Firefox.
GG, Firefox.
这篇关于Firefox扩展API-只能从用户输入处理程序调用Permissions.request吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!