简体   繁体   中英

Firefox WebExtensions tabs API how to get URL being loaded

I'm trying to get the current tab URL using the WebExtensions tabs API .

I am querying the tabs using this method:

// Get the current window's active tab.
browser.tabs.query({ currentWindow: true, active: true }, function(tabs) {
    if (tabs[0].status == 'loading') {
        // The user is currently loading a new page.
    } else {
        // The user is not loading a page.
    }
});

If the user is loading a page when I run the tab query, the tabs[0] object looks like the following:

{
  "id": 114,
  "index": 102,
  "windowId": 3,
  "selected": true,
  "highlighted": true,
  "active": true,
  "pinned": false,
  "status": "loading",
  "incognito": false,
  "width": 1278,
  "height": 987,
  "audible": false,
  "mutedInfo": {
    "muted": false
  },
  "url": "http://example.com/",
  "title": "Example Domain",
  "favIconUrl": "http://example.com/favicon.ico"
}

You can see that "status" is set to "loading" . This means that the "url" could change when the page has finished loading.

Is there anyway to know what page the user is loading?

Tested in Firefox 54.

In process navigation is available from webNavigation event s. You will most likely want to only look at events with frameId:0 . The webNavigation.onBeforeNavigate event has the earliest notification of the new URL. However, you would need to also listen to onHistoryStateUpdated and onReferenceFragmentUpdated .

Updates to the tab's displayed URL are available through the tabs.onUpdated event . For what it sounds like you are wanting, notification that the displayed URL has changed, this is most likely what you will want to be listening to.

It's possible to get information that the page will be navigated (ie prior to the navigation starting) by inserting a content script into the tab and detecting user actions which will cause navigation. This will not detect all possible sources of navigation. Unless you need to do this, it should be avoided due to it being more resource intensive.

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