Skip to content

Instantly share code, notes, and snippets.

@javiersantos
Last active January 3, 2026 18:56
Show Gist options
  • Select an option

  • Save javiersantos/c3e9ae2adba72e898f99 to your computer and use it in GitHub Desktop.

Select an option

Save javiersantos/c3e9ae2adba72e898f99 to your computer and use it in GitHub Desktop.
Get the current URL of the selected Chrome tab. Useful for Chrome/Chromium extensions.
/*
* Get the current URL of the selected Chrome tab. Call to getCurrent
* By Javier Santos
* https://gist.github.com/javiersantos/c3e9ae2adba72e898f99
*/
var currentURL;
chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
getCurrentURL(tabs[0].url);
});
function getCurrentURL(tab){
currentURL = tab;
}
@JTorresConsulta
Copy link

Does not work in v3

@abdulahJavaid
Copy link

abdulahJavaid commented Jan 3, 2026

async function getTabUrl() {
const tab = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
});
console.log(tab);
return tab[0].url;
}

getTabUrl().then(tab => console.log(tab));

// try this and remember to give the "permissions": ["tabs", "activeTab"] permission in manifest.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment