如何知道用户点击新标签按钮是否打开了新标签?

时间:2017-03-14 05:39:55

标签: javascript firefox-webextensions

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

  • 用户点击新标签按钮(+)?
  • 按用户点击<a href="http://www.google.com/">等链接?
      

    注意:我不在乎window.open()

    是否打开了新标签页

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

  • 对于通过点击+打开的标签,其网址为about:newtab
  • 对于通过点击<a href="" target="_blank">打开的标签,其网址为about:blank

但是,有一个例外,如果Firefox启动后的第二个标签通过点击&#34; +&#34;打开,其网址将为about:blank,不是about:newtab。我认为这是一个Firefox缺陷,在Bugzilla上发布了一个错误。

与此同时,还有其他办法吗?

1 个答案:

答案 0 :(得分:1)

我可以确认这种情况发生在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…"}

如果您想要区分,可以查看title事件中提供的urltabs.onCreated。对于链接,您有:

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

点击+,你可以选择以下两种方法之一:

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