在firefox webextensions中,如何以哪种方式知道新的选项卡?
- 用户单击"新标签"按钮( )?
- 通过用户单击诸如
<a href="http://www.google.com/">
之类的链接?注意:我不在乎
是否打开一个新标签window.open()
我发现,在chrome.tabs.Tab.onCreated
的回调中,有一个参数,假设它命名为 firefoxTab
:
- 对于通过单击 打开的选项卡,其URL是
about:newtab
- 对于通过单击
<a href="" target="_blank">
打开的选项卡,其URL是about:blank
但,如果启动Firefox后的第二个选项卡,则通过单击" "打开,它的URL将是about:blank
,而不是about:newtab
。我认为这是一个Firefox缺陷,已在Bugzilla上发布了一个错误。
与此同时,还有其他方法可以做吗?
我可以确认这发生在Firefox 52.0中(夜间测试,Firefox 55.0A1产生了相似的结果)。
重新启动时首次单击+
发生的事件是:
tabs.onUpdated -> arg[0]= 1 :: arg[1]= Object { status: "loading" } :: arg[2]= Object { id: 1, index: 1, windowId: 1, selected: false, highlighted: false, active: false, pinned: false, status: "complete", incognito: false, width: 1098, height: 812, audible: false, mutedInfo: { muted: false}, cookieStoreId: "firefox-default", url: "about:blank", title: "New Tab"}
tabs.onActivated -> arg[0]= Object { tabId: 1, windowId: 1 }
tabs.onHighlighted -> arg[0]= Object { tabIds: Array[1], windowId: 1 }
tabs.onCreated -> arg[0]= Object { id: 1, index: 1, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1098, height: 812, audible: false, mutedInfo: { muted: false}, cookieStoreId: "firefox-default", url: "about:blank", title: "New Tab"}
tabs.onUpdated -> arg[0]= 1 :: arg[1]= Object { status: "loading", url: "about:newtab" } :: arg[2]= Object { id: 1, index: 1, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1098, height: 812, audible: false, mutedInfo: { muted: false}, cookieStoreId: "firefox-default", url: "about:newtab", title: "New Tab"}
webNavigation.onBeforeNavigate -> arg[0]= Object { url: "about:newtab", timeStamp: 1489473167445, frameId: 0, parentFrameId: -1, tabId: 1 }
webNavigation.onCommitted -> arg[0]= Object { url: "about:newtab", timeStamp: 1489473167466, frameId: 0, parentFrameId: -1, tabId: 1, transitionType: "link", transitionQualifiers: Array[0] }
webNavigation.onDOMContentLoaded -> arg[0]= Object { url: "about:newtab", timeStamp: 1489473167718, frameId: 0, parentFrameId: -1, tabId: 1 }
tabs.onUpdated -> arg[0]= 1 :: arg[1]= Object { status: "complete" } :: arg[2]= Object { id: 1, index: 1, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1098, height: 812, audible: false, mutedInfo: { muted: false}, cookieStoreId: "firefox-default", url: "about:newtab", title: "New Tab"}
webNavigation.onCompleted -> arg[0]= Object { url: "about:newtab", timeStamp: 1489473167914, frameId: 0, parentFrameId: -1, tabId: 1 }
tabs.onUpdated -> arg[0]= 1 :: arg[1]= Object { status: undefined } :: arg[2]= Object { id: 1, index: 1, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1098, height: 812, audible: false, mutedInfo: { muted: false}, cookieStoreId: "firefox-default", url: "about:newtab", title: "New Tab"}
在+
上单击第二次时的事件是(是的,事件大大少,没有webNavigation
事件):
tabs.onActivated -> arg[0]= Object { tabId: 2, windowId: 1 }
tabs.onHighlighted -> arg[0]= Object { tabIds: Array[1], windowId: 1 }
tabs.onCreated -> arg[0]= Object { id: 2, index: 2, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1098, height: 812, audible: false, mutedInfo: {"muted: false}, cookieStoreId: "firefox-default", url: "about:newtab", title: "New Tab"}
随后的+
点击导致类似的事件。有时会发射其他事件。此外,还有更多事件起火,具体取决于about:newtab
页的内容。
相反,当单击<a href="" target="_blank">
时发生了许多其他事件。仅tabs.onCreated
事件是:
tabs.onCreated -> arg[0]= Object { id: 3, index: 2, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: "loading", incognito: false, width: 1098, height: 812, audible: false, mutedInfo: {"muted: false}, cookieStoreId: "firefox-default", url: "about:blank", title: "Connecting…"}
如果要区分,看来可以查看tabs.onCreated
事件中提供的title
和url
。对于链接,您有:
url: "about:blank", title: "Connecting…"
单击+
,您有以下两个:
url: "about:blank", title: "New Tab" //First `+`
url: "about:newtab", title: "New Tab" //Subsequent `+`