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

倾然丶 夕夏残阳落幕 提交于 2020-05-15 19:39:12

问题


In Firefox WebExtensions, how to know in which way is the new tab opened?

  • By user click on the new tab button (+)?
  • By User click on a link such as <a href="http://www.google.com/">?

    Note: I don't care if a new tab is opened by window.open()

I found that, in callback of chrome.tabs.Tab.onCreated, there is a parameter passed in, assume it is named as firefoxTab:

  • For tabs opened by click on +, its URL is about:newtab
  • For tabs opened by click on <a href="" target="_blank">, its URL is 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. I think it is a Firefox defect, have posted a bug on Bugzilla.

In the meantime, is there any other way to do this?


回答1:


I can confirm that this happens in Firefox 52.0 (testing on Nightly, Firefox 55.0a1 produced similar results).

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):

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.

In contrast, there are numerous additional events which occur when <a href="" target="_blank"> is clicked. Just the tabs.onCreated event is:

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. 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 `+`


来源:https://stackoverflow.com/questions/42778493/how-to-know-if-a-new-tab-is-opened-by-user-click-on-new-tab-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!