1

I need to fetch the URL of current tab using a chrome extension. The extension is a page action extension. So I'm thinking of doing this in content.js.

I was trying to use something like below in content.js

chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
    url = new URL(tabs[0].url)
    console.log(`URL: ${url}`)
})

However, it appears that Chrome.tabs api is not available in content scripts.

How can I get the current tab url in content.js without having use background.js and use message passing from there.

user3206440
  • 4,749
  • 15
  • 75
  • 132
  • https://stackoverflow.com/questions/18436245/how-to-fetch-url-of-current-tab-in-my-chrome-extension-using-javascript – chintuyadavsara Oct 06 '18 at 07:23
  • 3
    1) If your content.js is running in the main page of the tab you can use the web standard `location.href`, otherwise you'll have to use a background page script via messaging. 2) OTOH, a page action is a separate extension page where you can directly use chrome.tabs API, **it's not a web page** so it makes no sense to run content scripts there. – wOxxOm Oct 06 '18 at 07:47

0 Answers0