简体   繁体   English

如何知道用户是否单击新标签页按钮来打开新标签页?

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

In Firefox WebExtensions, how to know in which way is the new tab opened? 在Firefox WebExtensions中,如何知道以新的方式打开新标签页?

  • By user click on the new tab button (+)? 通过用户单击新选项卡按钮(+)?
  • By User click on a link such as <a href="http://www.google.com/"> ? 按用户单击诸如<a href="http://www.google.com/">类的链接?

    Note: I don't care if a new tab is opened by window.open() 注意:我不在乎window.open()是否打开新标签页

I found that, in callback of chrome.tabs.Tab.onCreated , there is a parameter passed in, assume it is named as firefoxTab : 我发现,在chrome.tabs.Tab.onCreated回调中,传入了一个参数,假定它名为firefoxTab

  • For tabs opened by click on +, its URL is about:newtab 对于通过单击+打开的选项卡,其URL about:newtab
  • For tabs opened by click on <a href="" target="_blank"> , its URL is about:blank 对于通过单击<a href="" target="_blank">打开的选项卡,其URL about:blank

But , there is an exception, if the second tab after Firefox started, is opened by click on "+", its URL will be about:blank , not about:newtab . 但是 ,有一个例外,如果通过单击“ +”打开Firefox启动后的第二个选项卡,则其URL将是about:blank ,而不是about:newtab I think it is a Firefox defect, have posted a bug on Bugzilla. 我认为这是Firefox的缺陷,已经在Bugzilla上发布了一个错误。

In the meantime, is there any other way to do this? 同时,还有其他方法可以做到这一点吗?

I can confirm that this happens in Firefox 52.0 (testing on Nightly, Firefox 55.0a1 produced similar results). 我可以确认在Firefox 52.0中会发生这种情况(在Nightly上进行测试,Firefox 55.0a1产生了类似的结果)。

The events that happen for clicking on the + for the first time upon restart are: 重新启动后首次单击+发生的事件是:

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"}

The events upon clicking a second time on + are (yes, significantly fewer events, and no webNavigation events): 第二次单击+的事件是(是的,事件明显更少,并且没有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"}

Subsequent clicks on + resulted in similar events. 随后单击+导致类似事件。 Sometimes additional events are fired. 有时会触发其他事件。 In addition, some more events fire, depending on the contents of the about:newtab page. 此外,还会根据about:newtab页面的内容触发更多事件。

In contrast, there are numerous additional events which occur when <a href="" target="_blank"> is clicked. 相反,单击<a href="" target="_blank">时,会发生许多其他事件。 Just the tabs.onCreated event is: 只是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…"}

If you are wanting to differentiate, it appears that it is possible to look at the title and url provided in the tabs.onCreated event. 如果您想区分,似乎可以查看tabs.onCreated事件中提供的titleurl For a link, you have: 对于链接,您具有:

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

For clicking on the + you have either of the two following: 要单击+您可以使用以下两个选项之一:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM