2017-06-29 51 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

相關問題