如何知道用户单击新选项卡按钮是否打开了新选项卡?

How to know if a new tab is opened by user click on new tab button?

在 Firefox WebExtensions 中,如何知道新标签页以何种方式打开?

我发现,在chrome.tabs.Tab.onCreated的回调中,有一个参数传入,假设它被命名为firefoxTab:

但是有一个例外,如果Firefox启动后的第二个标签是点击“+”打开的,它的URL会是about:blank,而不是 about:newtab。我认为这是 Firefox 的一个缺陷,已在 Bugzilla 上发布了一个错误。

请问还有其他方法吗?

我可以确认这发生在 Firefox 52.0 中(在 Nightly 上测试,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事件中提供的titleurl。对于 link,您有:

url: "about:blank", title: "Connecting…"

如果点击 +,您会遇到以下两种情况之一:

url: "about:blank", title: "New Tab"   //First `+`
url: "about:newtab", title: "New Tab"  //Subsequent `+`