【问题标题】:activeTab permissions not working as expected - Chrome ExtensionactiveTab 权限未按预期工作 - Chrome 扩展
【发布时间】:2018-07-07 16:10:25
【问题描述】:

根据permissions documentation,如果我们有activeTab权限,我们不需要指定基于URL的权限

以下任何 URL 都匹配所有主机:

http://*/*
https://*/*
*://*/*
<all_urls>

请注意,您可以避免声明所有主机权限 使用 activeTab 权限。

但这只能工作一次,第二次出现错误(扩展 UI 在我们第二次尝试时打开),如果我们通过单击扩展图标再次启动弹出窗口,它工作正常。

Unchecked runtime.lastError while running tabs.executeScript: 
Cannot access contents of the page. 
Extension manifest must request permission to access the respective host.

以下是详细信息

ma​​nifest.json

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "This is hello world example",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab"
  ]
}

popup.html

<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <div>Hello World!</div>
    <input type="button" id="refreshPage" value="Refresh Page"/>
  </body>
</html>

popup.js

document.addEventListener('DOMContentLoaded', function() {

    function refreshPage() {
        chrome.tabs.executeScript(null, {
            code: 'window.location.reload(true)'
        }, function(){
            console.log("Page refershed");
        });
    }

    document.getElementById("refreshPage").addEventListener("click", refreshPage);

});

如果我们添加"*://localhost/*"(这适用于本地主机),还有另一种方法可以为所有主机指定*://*/*(不确定这是否正确且安全),刷新按钮可以多次工作而无需重新启动弹出UI . activeTab 与基于 URL 的权限之间是否存在差异,并且由于特定原因,建议使用任何特定方式而不是另一种方式?

【问题讨论】:

标签: google-chrome-extension


【解决方案1】:

activeTab 暂时授予权限,直到标签被导航或关闭。我的猜测是,通过在点击刷新时重新加载选项卡会撤销 activeTab 权限。避免使用 window.location.reload 或为您尝试执行脚本的域指定 url 匹配。

https://developer.chrome.com/extensions/activeTab

activeTab 权限使扩展可以临时访问 用户调用扩展程序时当前活动的选项卡 - 例如 通过单击其浏览器操作。对选项卡的访问持续到选项卡 已导航或关闭。

【讨论】:

  • 正确。没有错误。
  • 鉴于 Chrome 网上应用店现在强制要求使用 activeTab,没有在重新加载时起作用的权限一个错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多