2017-06-29 34 views
0

我试图使用WebExtensions tabs API获取当前选项卡网址。Firefox WebExtensions选项卡API如何获取正在加载的URL

我使用这种方法查询标签:

// 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. 
    } 
}); 

如果用户加载页面时,我跑的标签查询时,tabs[0]对象如下所示:

{ 
    "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" 
} 

你可以看到"status"设置为"loading"。这意味着当页面加载完成后"url"可能会改变。

有无论如何知道用户正在加载什么页面?

在Firefox 54.

回答

1

测试在过程导航可从webNavigation event秒。你很可能只想看看frameId:0的事件。 webNavigation.onBeforeNavigate事件具有最早的新URL通知。但是,您还需要收听onHistoryStateUpdatedonReferenceFragmentUpdated

通过tabs.onUpdated event可以更新标签显示的URL。对于您想要的内容,通知显示的网址已更改,这很可能是您希望收听的内容。

通过将内容脚本插入标签并检测将导致导航的用户动作,可以获得导航(即在导航开始之前)页面将为的信息。这不会检测到所有可能的导航源。除非您需要需要来做到这一点,否则应该避免,因为它更耗费资源。

+0

谢谢,我会研究第一个解决方案。看起来像一个聪明的人。如果有效,我会接受这个答案。 – Harvey

+0

工作很好!谢谢! – Harvey

相关问题