-2

I am trying to get the URL of all opened Tabs and Selected Tab using thiese two snippet

// For Getting URL of All Opened Tabs
chrome.tabs.getCurrent(function(tabs) {
for (var i = 0; i < tabs.length; i++) {
    console.log(tab.url);
     }
});

// For Getting URL of Selected Tab
chrome.tabs.getSelected(function(tab) {
     console.log(tab.url);
});

but neither of them working. For getting All Tabs I am getting this error:

Error in response to tabs.getCurrent: TypeError: Cannot read property 'length' of undefined

and for getting the selected Tab

undefined

can you please let me know why this is happening and how can I fix it?

enter image description here

Keith
  • 150,284
  • 78
  • 298
  • 434
Behseini
  • 6,066
  • 23
  • 78
  • 125
  • 2
    Always check the parameters in the [documentation](https://developer.chrome.com/extensions/tabs). Even I do that. `getCurrent` returns a single tab where the invoking code runs. `getSelected` is deprecated, don't use it. Also your question has conflicting tags: apps aren't extensions. – wOxxOm Mar 29 '17 at 05:13
  • Also, the devtools built-in debugger is an excellent tool where you can set breakpoints and inspect parameters and anything instead of guessing what's wrong. – wOxxOm Mar 29 '17 at 05:14
  • Your first error is because you are using the variable name `tab`, which you have never defined. Thus, it is undefined. That portion of your code needs to be completely reworked for other reasons. The second one is unclear. Please supply a *manifest.json* so we can see if you have declared the `tabs` permission. Not doing so would cause what you are seeing. – Makyen Mar 29 '17 at 23:43
  • Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Mar 29 '17 at 23:43

1 Answers1

2

chrome.tabs.getSelected has been deprecated. so we should use tabs.query({active: true}... instead.

chrome.tabs.getCurrent passes a single tab to the callback function. It doesn't "Getting URL of All Opened Tabs", it:

Gets the tab that this script call is being made from. May be undefined if called from a non-tab context (for example: a background page or popup view).

So:

// Write the URL of the current tab to the console
chrome.tabs.getCurrent(tab => console.log(tab.url));

This requires the "activeTab" or "tabs" permission in the manifest. If there is an error it won't throw an exception, instead it will populate chrome.runtime.lastError.

I find it easier to deal with all the callbacks using an asynchronous or promise wrapper library like chrome-extension-async. This let's us use async/await syntax and a regular try-catch:

try {
    const currentTab = await chrome.tabs.getCurrent();
    console.log(currentTab.url);
}
catch(err) {
    // Handle errors
}

In your popup.html you can't access chrome.tabs.getCurrent - you have to use chrome.tabs.query instead:

async function writeAllTabUrlToConsole() {
    try {
        // Get all the tabs
        const tabs = await chrome.tabs.query({});

        // Write all their URLs to the popup's console
        for(let t of tabs)
            console.log(t.url, t.active);
    }
    catch(err) {
        // Handle errors
    }
}
Keith
  • 150,284
  • 78
  • 298
  • 434
  • Thanks Keith but neither of you hints are not working on my side! the `chrome.tabs.getCurrent(tab => console.log(tab.url));` is genereting this error `Error in response to tabs.getCurrent: TypeError: Cannot read property 'url' of undefined` and the last code I am getting `Invocation of form tabs.query() doesn't match definition tabs.query(object queryInfo, function callback)` – Behseini Mar 30 '17 at 04:40
  • @Behseini I think `chrome.tabs.getCurrent` returns `undefined` because your code is running in a pop-up. The last code had a bug in it (my fault) - `chrome.tabs.query` always needs a `queryInfo` object, even if getting all tabs. Try changing it to `chrome.tabs.query({})` – Keith Mar 30 '17 at 06:55