get window url from a browser extension 1

get window url from a browser extension

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
  var tab = tabs[0];
  var url = new URL(tab.url)
  var domain = url.hostname
  // `domain` now has a value like 'example.com'
})

Here is what the above code is Doing:
1. We’re using the chrome.tabs API to get the current tab.
2. We’re using the URL API to parse the URL of the current tab.
3. We’re using the URL API to get the hostname of the current tab.

Similar Posts