Chrome 扩展清单必须请求访问此主机的权限

nxowjjhe  于 2023-10-14  发布在  Go
关注(0)|答案(3)|浏览(198)

我正试图追加一个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
lyfkaqu1

lyfkaqu11#

清单v3使用不同的permission schema。这就是我的工作:

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

gab6jxml2#

工作的代码

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(); 
    }
});
neskvpey

neskvpey3#

下面的代码对我有用:

"manifest_version": 3,
"host_permissions": ["<all_urls>"],

相关问题