【问题标题】:Extension manifest must request permission to access this host扩展清单必须请求访问此主机的权限
【发布时间】:2021-05-02 02:21:21
【问题描述】:

我正在尝试将 div 附加到当前活动选项卡的页面。但是我收到了这个错误:

Error during tabs.executeScript: Cannot access contents of url .... 
Extension manifest must request permission to access this host. 

我的代码:(show_loader.js)

var dv = document.createElement('div');
dv.id = 'myid';
dv.innerHTML = 'test';
document.body.appendChild(dv);

但是当我输入这段代码时:

document.body.style.backgroundColor = 'green';

它按预期工作并且当前选项卡的背景颜色已更改,除了从 popup.js 运行的 show_loader.js 文件中的代码之外没有其他更改:

chrome.tabs.executeScript(null, {file: "show_loader.js"});

我的清单文件也有:

"permissions":
[
  "tabs",
  "notifications",
  "http://*/",
  "https://*/"
],

所以我想知道为什么当我尝试做除了设置背景颜色之外的任何其他事情时它会给出上述错误。即使是简单的alertconsole.log 单独在该页面上也会出现上述相同的错误。

如何解决?

更新:完整的相关代码

清单:

{
   ... name and description ...
   "icons":
   {
      "16" : "img/icon.png",
      "48" : "img/48.png",
      "128" : "img/128.png"
   },
   
   "permissions":
   [
      "tabs",
      "notifications",
      "http://*/*",
      "https://*/*"
   ],
   
   "browser_action":
   {
      "default_icon": "img/icon.png", 
      "default_title": "Page title",      
      "default_popup": "popup.html"       
   }
}

popup.js

// send data to server for saving
$('#btn').click(function(){

     chrome.tabs.executeScript(null, {file: "show_loader.js"});

      $loader.show();

      var data = $(this).closest('form').serialize();

      $.ajax({.....});

});

window.onload = function(){
    var $loader = $('#loader');
    $loader.show();
    
    chrome.tabs.getSelected(null, function(tab) {
        //console.log(tab);
        $('#url').val(tab.url); 
        $('#title').val(tab.title);
        $loader.hide();
    });
};

popup.html

<html>
<head>
   <link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
    
   <form action="" method="post" name="frm" id="frm">
   <table border="0" cellpadding="3" cellspecing="0" width="370">
      ......
   </table>
   </form>

<script src='js/jquery.js'></script>
<script src='popup.js?v=014423432423'></script> 

</body>
</html>

show_loader.js

console.log($); // doesn't work

// document.body.style.backgroundColor = 'green'; // WORKS

【问题讨论】:

  • 尝试将您的权限更改为 "http://*/*""https://*/*"(注意尾随星号)
  • @rgthree: 不幸的是仍然是同样的错误 :( 我认为问题可能是其他问题,因为背景颜色更改代码适用于相同的清单权限。
  • @dev02: 在什么时候这个代码chrome.tabs.executeScriptpopup.js 中被调用?
  • @Sudarshan:在 popup.html 文件中存在的按钮单击上调用它。如果我改变背景而不是其他任何东西,它在同一个地方工作
  • @dev02:我刚试了一个样例,它工作正常,你正在执行的页面是否处于加载状态?分享你完整的相关代码,看来问题似乎是另一个与此无关的问题

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

有效的代码

manifest.json

{
    "name": "Manifest Permissions",
    "description": "http://stackoverflow.com/questions/14361061/extension-manifest-must-request-permission-to-access-this-host",
    "version": "1",
    "manifest_version": 2,
    "browser_action": {
        "default_popup": "popup.html"
    },
    "permissions": [
        "tabs",
        "notifications",
        "http://*/",
        "https://*/"
    ]
}

popup.html

<html>

    <head>
        <script src="back.js"></script>
    </head>

    <body>
        <button id="tmp-clipboard">Click Me</button>
    </body>

</html>

back.js

document.addEventListener("DOMContentLoaded", function () {
    document.getElementById('tmp-clipboard').onclick = function () {
        chrome.tabs.executeScript(null, {
            file: "script.js"
        });
    }
});

script.js

var dv = document.createElement('div');
dv.id = 'myid';
dv.innerHTML = 'test';
document.body.appendChild(dv);

尝试从您的代码中删除已弃用的chrome.tabs.getSelected,并改用chrome.tabs.query

示例用法

chrome.tabs.query({
    "currentWindow": true,
    "status": true,
    "active": true //Add any parameters you want
}, function (tabs) {//It returns an array
    for (tab in tabs) {
        //Do your stuff here
    }
});

编辑 1

如果您打算在他单击browser action 的当前窗口中捕获活动浏览选项卡,请使用此代码

chrome.tabs.query({
    "currentWindow": true,//Filters tabs in current window
    "status": "complete", //The Page is completely loaded
    "active": true // The tab or web page is browsed at this state,
    "windowType": "normal" // Filters normal web pages, eliminates g-talk notifications etc
}, function (tabs) {//It returns an array
    for (tab in tabs) {
        $('#url').val(tabs[tab].url); 
        $('#title').val(tabs[tab].title);
        $loader.hide(); 
    }
});

【讨论】:

  • 感谢您抽出宝贵时间提供帮助和解答。我会将其标记为已接受并尝试一个问题。您如何将chrome.tabs.getSelected(null, function(tab) {$('#url').val(tab.url); }); 替换为tabs.query 版本?
  • @dev02:检查我的答案的编辑 1
  • 谢谢,但我认为迭代 tabs 会影响所有选项卡。再次感谢您的帮助:)
  • 看起来chrome说没有网络的页面无论您在manifest.js中说什么权限都无法在其上运行代码
【解决方案2】:

清单 v3 使用不同的 permission schema。这对我有用:

    "host_permissions": [
        "https://music.youtube.com/*"
    ],

【讨论】:

  • "host_permissions": ["https://*/*"] 适用于所有主机,以防其他人需要。
  • 我使用 "host_permissions": [*://*/"] 这样我就不受限于 https。
猜你喜欢
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
相关资源
最近更新 更多