4

this.window.location.href is not working in chrome extension in the html file i tried this function in the script:

function myFunction()
{
   var pl = this.window.location.href;
   var sWords= localStorage.getItem(pl);
   document.write(pl);
}

and it gives me:

chrome-extension://ebeadbfnnghmakkbimckpdmocjffkbjc/popup.html

so what i should do to get the link of the page?

Manse
  • 37,765
  • 10
  • 83
  • 108
elianore
  • 319
  • 2
  • 5
  • 9

1 Answers1

5

You can get the currently selected tab via chrome.tabs.query method. You need to pass two options:

  1. currentWindow : true
  2. active : true

It will return an array of tabs that match the criteria. You can get the URL from there. Like this:

chrome.tabs.query(
    {
        currentWindow: true,    // currently focused window
        active: true            // selected tab
    },
    function (foundTabs) {
        if (foundTabs.length > 0) {
            var url = foundTabs[0].url; // <--- this is what you are looking for
        } else {
            // there's no window or no selected tab
        }
    }
);
Fczbkk
  • 1,477
  • 1
  • 11
  • 23