简体   繁体   中英

Get URL of popup window opened from a Chrome Extension

I've got a Chrome extension that creates a popup window that the user needs to use for a login system. You click on the extension icon and it opens up its application (AngularJS in my case). The user then clicks on a button which calls chrome.windows.create() to open a popup.

I would like the main extension app to monitor the URL of that popup for changes.

I create the popup from the extension this way:

chrome.windows.create(
    {
      url: 'https://some.external.url.com/whatever',
      type: 'panel',
      width: 600,
      height: 600
    },
    function (windowReference) {
      console.log('My Window:', windowReference);
      // start monitoring URL of windowReference somehow
      // could be as simple as a setInterval() loop
    }
)

The problem is that the windowReference object passed to the callback doesn't have the current URL of the popup. Since the user can interact with the page in the popup (I'm pointing it at out OAuth2 system), the URL will change at times. I want to see that - either actively as changes are made, or by simply querying the current URL periodically.

This is what the windowReference object contains:

{
    alwaysOnTop:false,
    focused:false,
    height:600,
    id:1089,
    incognito:false,
    left:61,
    state:"normal",
    top:23,
    type:"popup",
    width:600
}

You can see that there is an ID there, and that, to me, suggest that I might be able to use it to call some other method to get the real URL information I'm after.

Any help would be appreciated.

So the answer turns out to be pretty simple. As Rob W mentioned in a comment, you use the chrome.tabs.query() method to do the search as you would for any other tab.

The missing part for me was that you can use the id from the window reference you get when the popup is created to get the desired results from the tabs query:

chrome.tabs.query(
    { windowId: windowReference.id }, 
    function callback(tabs) {
        var popup = tabs[0];
        $log.debug("Popup URL:", popup.url);
    }
);

You can see that I passed the ID as the value of the windowId parameter in the search query object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM