[英]chrome.tabs is not available: You do not have permission to access this API
我设置了这个权限
"permissions": [ "tabs" ],
在我使用的.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(...);
});
为什么我收到此错误?
chrome.tabs不可用:您无权访问此API。 确保manifest.json中包含所需的权限或清单属性。
如上所述,斯蒂芬的解决方案不再适用。 AFAICT,看起来谷歌不再允许在content-script
访问标签API中描述的回调。
所有这些意味着你必须在background.js中指定你的重定向:
(内容的script.js)
chrome.extension.sendRequest({ command: "selected-tab" });
(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
};
}
});
正如Rob W已经提到的那样,您无法访问内容脚本中的选项卡API。
您必须向后台脚本发送请求,该脚本将返回选定的选项卡。
(内容的script.js)
chrome.extension.sendRequest({ command: "selected-tab" }, function(tab) {
var page_url = tab.url;
// your code
});
background.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.command == "selected-tab") {
chrome.tabs.getSelected(null, sendResponse);
}
});
Google不允许(不再?)从内容脚本中访问tab对象。
如果要获取选项卡,可以从发送方发送到侦听器的回调函数:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
console.log("Received from tab: ", sender.tab);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.