Firefox WebExtensions 选项卡 API 如何加载 URL
Firefox WebExtensions tabs API how to get URL being loaded
我正在尝试使用 WebExtensions tabs 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 中测试。
进程内导航可从 webNavigation
events. 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
获得。
选项卡显示的 URL 更新可通过 tabs.onUpdated
event 获得。对于您想要的,显示的 URL 已更改的通知,这很可能是您想要收听的内容。
通过将内容脚本插入选项卡并检测将导致导航的用户操作,可以获得页面 将被导航(即在导航开始之前)的信息.这不会检测所有可能的导航源。除非你需要这样做,否则应该避免这样做,因为它会占用更多资源。
我正在尝试使用 WebExtensions tabs 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 中测试。
进程内导航可从 webNavigation
events. 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
获得。
选项卡显示的 URL 更新可通过 tabs.onUpdated
event 获得。对于您想要的,显示的 URL 已更改的通知,这很可能是您想要收听的内容。
通过将内容脚本插入选项卡并检测将导致导航的用户操作,可以获得页面 将被导航(即在导航开始之前)的信息.这不会检测所有可能的导航源。除非你需要这样做,否则应该避免这样做,因为它会占用更多资源。