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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14361061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:30:49  来源:igfitidea点击:

Extension manifest must request permission to access this host

javascriptgoogle-chromegoogle-chrome-extension

提问by dev02

I am trying to append a div to page of current active tab. However I am getting this error:

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

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

My Code: (show_loader.js)

我的代码:(show_loader.js)

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

However when I put this code:

但是,当我放置此代码时:

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

It works as expectedand background color of current tab is changed with no other change except for the code in show_loader.js file which is run from popup.js like this:

它按预期工作,当前选项卡的背景颜色发生了变化,除了从 popup.js 运行的 show_loader.js 文件中的代码外,没有其他变化,如下所示:

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

My Manifest file also does have:

我的清单文件也有:

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

So I wonder why it gives above error when I try to do anything else other than setting background color. Even simple alertor console.logalone on that page gives same above mentioned error.

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

How to fix that ?

如何解决?

Update: Complete Relevant Code

更新:完整的相关代码

Manifest:

显现:

{
   ... 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

弹出窗口.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

<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

show_loader.js

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

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

回答by Sudarshan

Code which worked

有效的代码

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

<html>

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

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

</html>

back.js

返回.js

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

script.js

脚本.js

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

Try Eliminating deprecated chrome.tabs.getSelectedfrom your code and use chrome.tabs.queryinstead.

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

Sample Usage

示例用法

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

Edit 1

编辑 1

If you intention is to capture active browsing tab in current window where he clicked browser actionuse this code

如果您打算在他单击的当前窗口中捕获活动浏览选项卡,请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(); 
    }
});