简体   繁体   English

Chrome扩展程序-寻求新权限

[英]Chrome Extension - asking for new permissions

I have a chrome extension already in the store to which I need to add more permissions as I want to apply it to more websites. 我的商店中已经有一个chrome扩展程序,我想向其添加更多权限,因为我想将其应用于更多网站。 But putting them into the permissions array disables the extension and the only indication that it requires new permissions is under the hamburger menu, which is entirely useless. 但是将它们放入权限数组将禁用扩展,并且唯一需要表明新权限的指示是在汉堡菜单下,这完全没有用。

Here is the original, which seems to have an implied " https://mail.google.com " permission due to it being in the content_scripts object: 这是原始文件,由于它位于content_scripts对象中,因此似乎具有隐含的“ https://mail.google.com ”权限:

 { "manifest_version": 2, "name": "My Extension", "short_name": "Extension", "version": "1.5.16", "description": "Description here", "homepage_url": "https://test.com/", "icons": { "16": "icon16.png", "48": "icon48.png", "128": "icon128.png" }, "content_scripts": [ { "matches": [ "https://mail.google.com/*" ], "js": [ "injector.js" ], "css": [ "style.css" ] } ], "web_accessible_resources": [ "app.js", "libs.js" ] } 

In a perfect world I'd just add additional "content_scripts" objects, but this disables the extension due to the implied permission to the new website domain. 在理想情况下,我只会添加其他“ content_scripts”对象,但是由于隐含对新网站域的许可,因此将禁用扩展。

Putting "optional_permissions": [" https://hangouts.google.com "] still disables the plugin and doesn't notify the user. 放置“ optional_permissions”:[“ https://hangouts.google.com ”]仍会禁用该插件,并且不会通知用户。

If I put any code at all into a background script, say something that accesses chrome.tabs in any way... BOOM! 如果我将任何代码都放到后台脚本中,请说一些可以以任何方式访问chrome.tabs的内容…… It's implied permission and the extension is disabled. 这是隐含的权限,扩展名已禁用。

How should I be going about getting new permissions without disabling the extension? 我应该如何在不禁用扩展的情况下获得新权限? Popups to ask for permission would be a perfect solution, but that's not what I'm being offered. 弹出窗口以寻求许可将是一个完美的解决方案,但这不是我所提供的。

Setting aside the question of content scripts, in most cases optional permissions are not worth it for patching a list of permissions from one fixed list to another. 抛开内容脚本的问题,在大多数情况下,将权限列表从一个固定列表修补到另一个固定列表是不值得的。

You'll make both existing and new users go through runtime permissions dialog that's perplexing to new users. 您将使现有用户和新用户都通过对新用户感到困惑的运行时权限对话框。

The real purpose of optional permissions is to allow the users to choose among domains not known at runtime. 可选权限的真正目的是允许用户在运行时未知的域之间进行选择。

It's best to bite the bullet and risk disabling of the extension. 最好是硬着头皮,冒着禁用扩展的风险。

If you wish, you can publish a second extension with the new permissions, make the first unlisted, and publish an update that urges users to install the new extension. 如果愿意,您可以发布具有新权限的第二个扩展,将第一个扩展名不列出,然后发布更新,敦促用户安装新扩展。

Some users might prefer this, some might instead be more annoyed - it's a lose-lose sitation from the start, you won't retain all your users. 有些用户可能会喜欢这种方式,有些则可能会更烦恼-从一开始就是输家,您不会保留所有用户。 With the migration route you will lose the accumulated rating in the store, though. 但是,通过迁移途径,您将失去商店中的累计评分。


Adding a content script is, indeed, an implicit grant of host permission. 实际上,添加内容脚本实际上是对主机权限的隐式授予。 And the declarative API to change content script configuration at runtime never became supported/stable . 而且在运行时更改内容脚本配置的声明性API 从未得到支持/稳定

So if you absolutely resolved to go the optional permissions route, you'll need to (inefficiently, with restrictions) do programmatic injection . 因此,如果您绝对决定采用可选权限路线,则需要(效率低下,有限制)进行编程注入

In addition to the existing answer (which is totally valid), I'll suggest how to handle optional permissions in case you decide to avoid adding new permissions in manifest.json 除了现有答案(完全有效)之外,我还将建议如何处理可选权限,以防您决定避免在manifest.json添加新权限。

Programmatic script injection 程序脚本注入

There's a new contentScripts.register() API that you can use to efficiently register your scripts on new domains after you get the permission. 有一个新的contentScripts.register() API ,您可以在获得许可后使用它在新域上有效地注册脚本。

browser.contentScripts.register({
    matches: ['https://your-dynamic-domain.example.com/*'],
    js: [{file: 'content.js'}]
});

This API is only available in Firefox but there's a Chrome polyfill you can use. 该API仅在Firefox中可用,但是您可以使用Chrome polyfill

Acquiring new permissions 获取新权限

You can use chrome.permissions.request to let the user add new domains on demand or you can install a module I wrote recently ( webext-domain-permission-toggle ) to provide a simple UI to toggle permissions on any domains. 您可以使用chrome.permissions.request允许用户按需添加新域,也可以安装我最近编写的模块( webext-domain-permission-toggle ),以提供一个简单的UI来切换任何域的权限。

When used together with webext-dynamic-content-scripts , you can add any domain and the module will automatically register your existing content_scripts on them. webext-dynamic-content-scripts一起使用时 ,您可以添加任何域,并且模块将自动在它们上注册您现有的content_scripts

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

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