Chrome extension: function must be called during a user gesture

爱⌒轻易说出口 提交于 2019-12-13 06:53:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!