Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>chrome.tabs.getSelected</code> is <strong>asynchronous</strong>. That means that when the callback function is called, <code>return url</code> "has already occurred".</p> <p>You've got two options to achieve the desired effect.</p> <ol> <li><p>Properly rewrite your code, to correctly implement the asynchronous aspect (the exact details depends on your extension's implementation).<br> Note that <code>getSelected</code> has been <a href="http://code.google.com/chrome/extensions/whats_new.html#16" rel="noreferrer">deprecated</a> and replaced with <a href="http://code.google.com/chrome/extensions/tabs.html#method-query" rel="noreferrer"><code>chrome.tabs.query</code></a> since Chrome 16.</p></li> <li><p>Maintain a hash with the current URLs using <a href="http://code.google.com/chrome/extensions/tabs.html#event-onUpdated" rel="noreferrer"><code>chrome.tabs.onUpdated</code></a> (add tabID + URL), <a href="http://code.google.com/chrome/extensions/tabs.html#event-onRemoved" rel="noreferrer"><code>chrome.tabs.onRemoved</code></a> (to remove obsolete entries) and <a href="http://code.google.com/chrome/extensions/tabs.html#event-onActivated" rel="noreferrer"><code>chrome.tabs.onActivated</code></a> (to set the current active tab).</p></li> </ol> <p>Code for 2:</p> <pre class="lang-js prettyprint-override"><code>// Our hash var tabIdToURL = {}; var currentTabId = -1; // Add changes to the hash (tab creation, tab's page load) chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { tabIdToURL[tabId] = tab.url; // also available as tab.id and changeInfo.url }); // Remove entries from closed tabs chrome.tabs.onRemoved.addListener(function(tabId) { delete tabIdToURL[tabId]; }); // Set the ID of the current active tab chrome.tabs.onActivated.addListener(function(activeInfo) { currentTabId = activeInfo.tabId; }); // Usage, based on the question's function function getURL() { return tabIdToURL[currentTabId] || ''; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload