简体   繁体   English

Chrome扩展程序-请求“当前”标签的可选权限

[英]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. 例如:我在http://stackoverflow.com上 ,单击徽章,然后从弹出窗口单击按钮,我应该得到一个提示框,允许在此选项卡上进行操作。

What I'm doing: 我在做什么:

  • manifest 表现
    \n... ...\n"permissions": ["tabs", "contextMenus"], “权限”:[“标签”,“ contextMenus”],\n"optional_permissions": [ "<all_urls>" ], “ optional_permissions”:[ "<all_urls>" ],\n... ...\n
  • popup 弹出

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

  • background 背景

     function reqPerm(url) 函数reqPerm(url)\n{ {\n    chrome.permissions.request({permissions: ['tabs'], origins: [url]}, function (granted) chrome.permissions.request({permissions:['tabs'],origin:[url]},功能(已授予)\n    { {\n        log('permision:', granted) log('permision:',已授予)\n    }); });\n} } 

What I'm getting: 我得到的是:

Error during permissions.request : Optional permissions must be listed in extension manifest. Permissions.request期间出错:扩展清单中必须列出可选权限。

If I set url manually (in this ex. https://stackoverflow.com/ ) in "optional_permissions" array, all is working how I need. 如果我在“ optional_permissions”数组中手动​​设置了url (在此示例中为https://stackoverflow.com/ ,则一切都按我的需要工作。

This is currently not possible, you can only request permissions for URLs that have been explicitly listed in the manifest. 当前这是不可能的,您只能请求对清单中已明确列出的URL的权限。 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. 但是,最好使用像webext-domain-permission-toggle这样的模块,因为它将为您处理整个UI。

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 首先,不允许使用//的可选权限//代码中出现错误,然后您需要在执行此操作的可选权限数组中指定tab权限

"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 第三,您应该在请求权限之前检查它,并且它未被授予,那么您应该请求它引用google可选权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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