Trying to get tab.id using the chrome.tabs.getSelected call as follows:

 var tabId = ''; chrome.windows.getCurrent(function(w){ chrome.tabs.getSelected(w.id,function(t){ tabId = t.id + ''; alert(tabId); }) }); alert(tabId); 

When executing, an external alert first executed, setting an empty value, then an internal one, showing the required identifier. Introducing a delay to an external alert fixes the problem, but cannot be a solution. As far as I understand, a separate stream is created for the execution of the request and is not synchronized with the main application.

How to build a query excluding race race correctly?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

getSelected :

Deprecated since Chrome 33.

This feature is deprecated and not recommended for use.

You can use the query function. Sort of:

 chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { alert(tabs[0].id); }); 

We get the active tab in the current window. You can operate on the result in the callback function, which is passed as a parameter to the query function.

When executing, an external alert is first executed, setting an empty value, then an internal one, showing the required identifier.

Read about Asynchronous vs. synchronous methods on developer.chrome.com

Code

 chrome.windows.getCurrent(function(w){ chrome.tabs.getSelected(w.id,function(t){ tabId = t.id + ''; alert(tabId); }) }); 

asynchronous. That is, at the time of calling the external alert'a it is in the process of being executed and therefore you first receive an external alert with an empty value, and after it has been processed, you receive an internal alert with the desired value.

UPD to comment

With all due respect, I did not understand. I don’t want to operate with the tabId within the callback function. I don’t understand how to output it using the “other function” that accepts tabId.

 function someFunction(tabId) { // Делаем что-то с tabId. // Например alert(tabId); } chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { someFunction(tabs[0].id); }); 

Or you can use promise :

 function getTabId() { return new Promise(function (resolve, reject) { chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { resolve(tabs[0].id); // Может еще что-то. reject или.. }); }); } 

Next, call the getTabId function getTabId

 getTabId().then(function (tabId) { // Делаем что-то с tabId // Например alert(tabId); }); 

In short

Promise need to work with asynchronous code.

In the first part, we wrote a wrapper function that returns a promise object. It accepts a callback function with two parameters, resolve and reject . resolve used when the operation is successfully completed, that is, we return the result with tabId . In case of any errors we use reject .

Next, when calling the getTabId function, getTabId use the then method which accepts the callback function with the tabId parameter (we passed its value to resolve).

There is a good article about their use (translation). Original

And some more material on MDN - Promise

I think this is enough for understanding.

  • I know about using chome.tabs.query, I also tried to use this method as an alternative to chrome.tabs.getCurrent, but since it is also asynchronous, as confirmed by developer.chrome.com/extensions/overview#sync, the situation remains the same although it would be desirable to find a solution that allows tabId to be moved outside the method. - avs100
  • @ avs100 I wrote, you can perform actions with tabId in the aisles of the callback function, which is passed to the query. Just pull another function out of it that will accept this tabId. - UserName
  • With all due respect, I did not understand. I don’t want to operate with the tabId within the callback function. - avs100
  • @ avs100 updated the answer - UserName
  • Thank you, valuable information. I'm new to JS, now I think I'll figure it out. - avs100