0

I am working on a chrome extension. My manifest.json contains the entry

"content_scripts": [
{
    "matches": ["file:///C:/Example/*"],
    "css": ["my-styles.css"],
    "js": ["content-script.js"],
    "run_at": "document_end"    
 }],    

In the file content-script.js I need to get the url address of the active tab. I tried to define a variable, but it raises error "Uncaught ReferenceError: tabs is not defined"

var url = tabs[0].url;

I also tried defining a function to get the url, but it is also not working, raising error "Uncaught TypeError: Cannot read properties of undefined (reading 'query')"

function get_base_url() {
    var base_url = '';
    chrome.tabs.query({
        active: true,
        lastFocusedWindow: true
    }, function(tabs) {
        base_url = tabs[0].url;
        return base_url;
    }); 
}
Nick_F
  • 1,103
  • 2
  • 13
  • 30
  • I found out I can use document.URL. My mistake was that I previously tried with document.url and the expression is case sensitive. – Nick_F Sep 10 '21 at 12:37

1 Answers1

0

I found out I can use document.URL. My mistake was that I previously tried with document.url and the expression is case sensitive

Nick_F
  • 1,103
  • 2
  • 13
  • 30