简体   繁体   中英

Chrome Extension - Request Optional Permissions for current tab

I'm trying to require tab permission for current origin. Ex: I'm on http://stackoverflow.com , clicking on badge, then on button from popup, and I should get a prompt box to allow manipulating on this tab.

What I'm doing:

  • manifest
    \n... \n"permissions": ["tabs", "contextMenus"], \n"optional_permissions": [ "<all_urls>" ], \n... \n
  • popup

     $('#reqPermision').click(function() \n{ \n    bg.reqPerm(url); \n});  

  • background

     function reqPerm(url) \n{ \n    chrome.permissions.request({permissions: ['tabs'], origins: [url]}, function (granted) \n    { \n        log('permision:', granted) \n    }); \n}  

What I'm getting:

Error during permissions.request : Optional permissions must be listed in extension manifest.

If I set url manually (in this ex. https://stackoverflow.com/ ) in "optional_permissions" array, all is working how I need.

This is currently not possible, you can only request permissions for URLs that have been explicitly listed in the manifest. However, this feature request should make what you are trying to do (automatically grant permission when clicking on a browser action) doable.

What you're trying to do should work. In the popup the code should be:

$('#reqPermision').click(function() {
    chrome.permissions.request({origins: [url]}, function (granted) {
        log('permision:', granted)
    });
});

You don't need to call it from a background page.


However it's probably better to use a module like webext-domain-permission-toggle because it will handle the whole UI for you.

firstly optional permission of is not allowed //error in your code secondly you need to specify the tab permission in the array of optional permission you did this

"permissions": ["tabs", "contextMenus"], "optional_permissions": [ "<all_urls>" ],

you do it like this

"permissions": [, "contextMenus"], "optional_permissions": [ 'tabs' ,<any other permission you want>],

third you should check before it request permission and it not granted then you you should request it refference google optional permission

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM