简体   繁体   中英

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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