扩展清单必须请求访问此主机的权限 [英] Extension manifest must request permission to access this host

查看:179
本文介绍了扩展清单必须请求访问此主机的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我的代码:(show_loader.js)

My Code: (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';

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

It works as expected and 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:

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

我的清单文件也确实有:

My Manifest file also does have:

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

所以我想知道为什么在尝试设置背景色以外的其他操作时会出现上述错误.即使仅在该页面上单独使用alertconsole.log,也会产生与上述相同的错误.

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

该如何解决?

清单:

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

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

show_loader.js

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

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

推荐答案

有效的代码

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

Try Eliminating deprecated chrome.tabs.getSelected from your code and use chrome.tabs.query instead.

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的当前窗口中捕获活动的浏览选项卡,请使用此代码

Edit 1

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

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

这篇关于扩展清单必须请求访问此主机的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆