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

标签 javascript firefox-addon-webextensions

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

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

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

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

  • 对于通过单击 + 打开的选项卡,其 URL 为 about:newtab
  • 对于通过单击 <a href="" target="_blank"> 打开的选项卡,其网址为about:blank

但是,有一个异常(exception),如果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…"}

如果您想区分,似乎可以查看 titleurl tabs.onCreated中提供事件。对于链接,您有:

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

点击 +您有以下两项之一:

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

关于javascript - 如何知道用户单击新选项卡按钮是否打开了新选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42778493/

相关文章:

javascript - 根据当前日期更改 div ID

Javascript 不在本地页面上运行

javascript - 单击按钮时从内容脚本打开选项页面?

javascript - Firefox:添加自定义键盘快捷键,例如用 'Saved Logins' 打开 'Show Passwords'

javascript - 复选框强制换行

javascript - 是否可以以编程方式模拟按键事件?

javascript - 如何在不复制粘贴代码和编辑变量的情况下执行此操作 2 次?

javascript - 调整表格大小超出屏幕范围

javascript - 数组键路径上的索引找不到任何值

javascript - 忽略未声明的变量 (TypeScript)