简体   繁体   English

chrome.tabs不可用:您无权访问此API

[英]chrome.tabs is not available: You do not have permission to access this API

I set this permission 我设置了这个权限

"permissions": [ "tabs" ],

and in the .js I use 在我使用的.js中

chrome.tabs.getSelected(null, function(tab) {
var page_url = tab.url;
$("#chrome_ext_qr_code img").attr("src", ...);
$("#chrome_ext_qr_code input").val(...);
});

Why I got this error? 为什么我收到此错误?

chrome.tabs is not available: You do not have permission to access this API. chrome.tabs不可用:您无权访问此API。 Ensure that the required permission or manifest property is included in your manifest.json. 确保manifest.json中包含所需的权限或清单属性。

stephan's solution, as described, no longer works. 如上所述,斯蒂芬的解决方案不再适用。 AFAICT, it looks like google no longer allows callbacks described in content-script access to the tabs API either. AFAICT,看起来谷歌不再允许在content-script访问标签API中描述的回调。

All this means is that you'll have to specify your redirect in your background.js instead: 所有这些意味着你必须在background.js中指定你的重定向:

(content-script.js) (内容的script.js)

chrome.extension.sendRequest({ command: "selected-tab" });

(background.js) (background.js)

chrome.extension.onRequest.addListener(function(request, sender) { 
  if (request.command == "selected-tab") { 
    chrome.tabs.getSelected(null, function(){
      // your code here
      // var page_url = tab.url etc, etc
    }; 
  } 
});

As Rob W already mentioned, you can't access the tabs API in the content-script. 正如Rob W已经提到的那样,您无法访问内容脚本中的选项卡API。

You have to send a request to the background-script which returns the selected tab. 您必须向后台脚本发送请求,该脚本将返回选定的选项卡。

(content-script.js) (内容的script.js)

chrome.extension.sendRequest({ command: "selected-tab" }, function(tab) {
    var page_url = tab.url;
    // your code
});

background.js background.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.command == "selected-tab") {
        chrome.tabs.getSelected(null, sendResponse);
    }
});

Google doesn't allow (anymore?) to access the tab object from inside content scripts. Google不允许(不再?)从内容脚本中访问tab对象。

If you want to get the tab you can do so from the sender sent to callback function of the listener: 如果要获取选项卡,可以从发送方发送到侦听器的回调函数:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    console.log("Received from tab: ", sender.tab);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM