Reputation: 482
I set this permission
"permissions": [ "tabs" ],
and in the .js I use
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. Ensure that the required permission or manifest property is included in your manifest.json.
Upvotes: 3
Views: 2935
Reputation: 8301
Google doesn't allow (anymore?) to access the tab object from inside content scripts.
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);
});
Upvotes: 0
Reputation: 1003
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.
All this means is that you'll have to specify your redirect in your background.js instead:
(content-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
};
}
});
Upvotes: 1
Reputation: 82
As Rob W already mentioned, you can't access the tabs API in the content-script.
You have to send a request to the background-script which returns the selected tab.
(content-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);
}
});
Upvotes: 0