Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location.href not working in chrome extension [duplicate]

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?

like image 365
elianore Avatar asked Oct 19 '25 04:10

elianore


1 Answers

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
        }
    }
);
like image 75
Fczbkk Avatar answered Oct 21 '25 16:10

Fczbkk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!