问题
In a Chrome Extension I want to ask the user for full access to pages through a browser_action's popup's link (aka a toolbar popup with a link). When the user clicks the link, I get this error:
runtime.lastError while running permissions.request: This function must be called during a user gesture
Yet the user actually clicked the link and the 'user gesture'. Any ideas of how to debug what is considered a user gesture?
On the button click, I request full access via chrome's chrome.permissions.request api:
$('#button-requestpermissions').click(function(){
requestAmbientPermission(function(granted) {
if (granted) {
// code never reached as granted === false
}
});
});
function requestAmbientPermission(callback){
// Permissions must be requested from inside a user gesture, like a button's click handler.
chrome.permissions.request({
permissions: ['activeTab'],
origins: ['<all_urls>']
}, callback);
}
The manifest has optional permissions set like so:
"permissions": [
"https://www.meethue.com/",
"https://colorlovers.herokuapp.com/"
],
"optional_permissions": [
"activeTab",
"<all_urls>"
],
回答1:
Too late to answer? You can only request a new permission from a user action, for example, a button clicked:
document.querySelector('#my-button').addEventListener('click', function(event) {
// Permissions must be requested from inside a user gesture, like a button's
// click handler.
chrome.permissions.request({
permissions: ['tabs'],
origins: ['http://www.google.com/']
}, function(granted) {
// The callback argument will be true if the user granted the permissions.
if (granted) {
doSomething();
} else {
doSomethingElse();
}
});
});
A detailed explanation can be found here
来源:https://stackoverflow.com/questions/27669590/chrome-extension-function-must-be-called-during-a-user-gesture