如何在 Firefox WebExtensions 中获取 openerTabId?

How to get openerTabId in Firefox WebExtensions?

我开发了一个 Chrome 扩展,并尝试使用 Firefox WebExtensions 将它移植到 Firefox。

这是我面临的问题。在我的扩展中,我需要使用 Firefox WebExtensions 不支持的 chrome.tabs.Tab.openerTabId(参见 documentation)。

我想出了一个解决方法,通过查询当前 window:

中的活动选项卡,自己获得 openerTabId
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
    // tabs has only one element, which is the one I just opened
});

但它不起作用,因为此查询的结果是 我刚刚打开的选项卡,而不是我单击“新建选项卡”按钮之前的选项卡

现在我想在单击“新建选项卡”按钮之前查询活动选项卡,但我在 chrome.tabs.query() 中没有看到任何参数,例如 lastFocusedTab

chrome.tabs.query()可以做到吗?或者还有其他方法吗?

可能重复:tab.openerTabId undefined in Firefox WebExtension。不幸的是,这个问题没有解决:-(.

自 Firefox 57 起在桌面版 Firefox 中受支持

截至 Firefox 57, the desktop version of Firefox supports tabs.Tab.openerTabId

Firefox 不支持 Android

Firefox Android does not support tabs.Tab.openerTabId

如果您希望您的扩展与 Android 的 Firefox 兼容,您必须自己跟踪此信息。您可以跟踪用户通过单击 link 打开的选项卡的打开器选项卡 ID。当JavaScript创建新标签时,你可以猜测它是由JavaScript在活动标签中完成的,但你无法知道。您可以通过跟踪当前活动选项卡、侦听要创建的新选项卡以及侦听以查看创建新选项卡的原因是因为用户单击 link 的组合来完成此操作。这可以通过收听来完成:

  • tabs.onActivated 跟踪当前活动的选项卡
    这是在 tabs.onCreated 之前触发的,因此您需要保留前一个活动选项卡的记录。对于立即激活新打开的选项卡的情况,这将是必需的。显然,您还需要考虑新选项卡未立即激活的情况。
  • tabs.onCreated 确定何时创建新选项卡。
  • webNavigation.onCommitted to look at the transitionType 以查看新选项卡实际上是从 link 创建的,而不是当用户从书签等打开新选项卡时恰好是活动选项卡。

作为参考,这些是当用户点击 link (<a href="http://www.example.com" target="_blank">) 时触发的事件:

Content event: mousedown  on: <a href="http://www.example.com" target="_blank">  contentMessageUI.js:16:9
Content event: mouseup    on: <a href="http://www.example.com" target="_blank">  contentMessageUI.js:16:9
Content event: click      on: <a href="http://www.example.com" target="_blank">  contentMessageUI.js:16:9
tabs.onActivated                  ->  arg[0]= Object { tabId: 8, windowId: 0 }
tabs.onHighlighted                ->  arg[0]= Object { tabIds: Array[1], windowId: 0 }
tabs.onUpdated                    ->  arg[0]= 8 :: arg[1]= Object { status: "complete" } :: arg[2]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onUpdated                    ->  arg[0]= 8 :: arg[1]= Object { status: undefined } :: arg[2]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onZoomChange                 ->  arg[0]= Object { tabId: 8, oldZoomFactor: undefined, newZoomFactor: 1, zoomSettings: Object }
tabs.onUpdated                    ->  arg[0]= 8 :: arg[1]= Object { status: "complete", url: "about:blank" } :: arg[2]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onUpdated                    ->  arg[0]= 8 :: arg[1]= Object { status: undefined } :: arg[2]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onUpdated                    ->  arg[0]= 8 :: arg[1]= Object { status: "loading" } :: arg[2]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "loading", incognito: false, width: 1100, 5 more… }
tabs.onCreated                    ->  arg[0]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "loading", incognito: false, width: 1100, 5 more… }
webNavigation.onBeforeNavigate    ->  arg[0]= Object { url: "about:blank", timeStamp: 1487869034364, frameId: 0, parentFrameId: -1, tabId: 8 }
webNavigation.onErrorOccurred     ->  arg[0]= Object { url: "about:blank", timeStamp: 1487869034368, frameId: 0, parentFrameId: -1, error: "Error code 2152398850", tabId: 8 }
webNavigation.onCommitted         ->  arg[0]= Object { url: "about:blank", timeStamp: 1487869034377, frameId: 0, parentFrameId: -1, tabId: 8, transitionType: "link", transitionQualifiers: Array[0] }
webNavigation.onBeforeNavigate    ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487869034380, frameId: 0, parentFrameId: -1, tabId: 8 }
webRequest.onBeforeRequest        ->  arg[0]= Object { requestId: "150", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487869034382, frameId: 0, parentFrameId: -1, tabId: 8 }
webRequest.onBeforeSendHeaders    ->  arg[0]= Object { requestId: "150", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487869034384, frameId: 0, parentFrameId: -1, tabId: 8, requestHeaders: Array[6] }
webRequest.onSendHeaders          ->  arg[0]= Object { requestId: "150", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487869034387, frameId: 0, parentFrameId: -1, tabId: 8, requestHeaders: Array[6] }
webRequest.onHeadersReceived      ->  arg[0]= Object { requestId: "150", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487869034416, frameId: 0, parentFrameId: -1, tabId: 8, responseHeaders: Array[11], 2 more… }
webRequest.onResponseStarted      ->  arg[0]= Object { requestId: "150", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487869034420, frameId: 0, parentFrameId: -1, tabId: 8, responseHeaders: Array[11], 2 more… }
webRequest.onCompleted            ->  arg[0]= Object { requestId: "150", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487869034429, frameId: 0, parentFrameId: -1, tabId: 8, responseHeaders: Array[11], 2 more… }
tabs.onUpdated                    ->  arg[0]= 8 :: arg[1]= Object { status: "loading", url: "http://www.example.com/" } :: arg[2]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "loading", incognito: false, width: 1100, 5 more… }
webNavigation.onCommitted         ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487869034538, frameId: 0, parentFrameId: -1, tabId: 8, transitionType: "link", transitionQualifiers: Array[0] }
tabs.onUpdated                    ->  arg[0]= 8 :: arg[1]= Object { status: "complete" } :: arg[2]= Object { id: 8, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
webNavigation.onDOMContentLoaded  ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487869034710, frameId: 0, parentFrameId: -1, tabId: 8 }
webNavigation.onCompleted         ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487869034715, frameId: 0, parentFrameId: -1, tabId: 8 }

当通过 JavaScript 打开选项卡时,transitionType 似乎也将是 link(假设用户已允许)。以下是 window.open('http://www.example.com'); 打开新选项卡时的事件(在允许来自该域的弹出窗口后,通过在内容页面的 DevTools 控制台中执行该操作来模拟):

tabs.onActivated                  ->  arg[0]= Object { tabId: 11, windowId: 0 }
tabs.onHighlighted                ->  arg[0]= Object { tabIds: Array[1], windowId: 0 }
tabs.onUpdated                    ->  arg[0]= 11 :: arg[1]= Object { status: "complete" } :: arg[2]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onUpdated                    ->  arg[0]= 11 :: arg[1]= Object { status: undefined } :: arg[2]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onZoomChange                 ->  arg[0]= Object { tabId: 11, oldZoomFactor: undefined, newZoomFactor: 1, zoomSettings: Object }
tabs.onUpdated                    ->  arg[0]= 11 :: arg[1]= Object { status: "complete", url: "about:blank" } :: arg[2]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onUpdated                    ->  arg[0]= 11 :: arg[1]= Object { status: undefined } :: arg[2]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
tabs.onUpdated                    ->  arg[0]= 11 :: arg[1]= Object { status: "loading" } :: arg[2]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "loading", incognito: false, width: 1100, 5 more… }
tabs.onCreated                    ->  arg[0]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "loading", incognito: false, width: 1100, 5 more… }
webNavigation.onBeforeNavigate    ->  arg[0]= Object { url: "about:blank", timeStamp: 1487871931270, frameId: 0, parentFrameId: -1, tabId: 11 }
webNavigation.onErrorOccurred     ->  arg[0]= Object { url: "about:blank", timeStamp: 1487871931274, frameId: 0, parentFrameId: -1, error: "Error code 2152398850", tabId: 11 }
webNavigation.onCommitted         ->  arg[0]= Object { url: "about:blank", timeStamp: 1487871931289, frameId: 0, parentFrameId: -1, tabId: 11, transitionType: "link", transitionQualifiers: Array[0] }
webNavigation.onBeforeNavigate    ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487871931292, frameId: 0, parentFrameId: -1, tabId: 11 }
webRequest.onBeforeRequest        ->  arg[0]= Object { requestId: "159", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487871931294, frameId: 0, parentFrameId: -1, tabId: 11 }
webRequest.onBeforeSendHeaders    ->  arg[0]= Object { requestId: "159", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487871931297, frameId: 0, parentFrameId: -1, tabId: 11, requestHeaders: Array[6] }
webRequest.onSendHeaders          ->  arg[0]= Object { requestId: "159", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487871931299, frameId: 0, parentFrameId: -1, tabId: 11, requestHeaders: Array[6] }
webRequest.onHeadersReceived      ->  arg[0]= Object { requestId: "159", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487871931312, frameId: 0, parentFrameId: -1, tabId: 11, responseHeaders: Array[11], 2 more… }
webRequest.onResponseStarted      ->  arg[0]= Object { requestId: "159", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487871931317, frameId: 0, parentFrameId: -1, tabId: 11, responseHeaders: Array[11], 2 more… }
webRequest.onCompleted            ->  arg[0]= Object { requestId: "159", url: "http://www.example.com/", originUrl: "http://www.example.com/", method: "GET", type: "main_frame", timeStamp: 1487871931329, frameId: 0, parentFrameId: -1, tabId: 11, responseHeaders: Array[11], 2 more… }
tabs.onUpdated                    ->  arg[0]= 11 :: arg[1]= Object { status: "loading", url: "http://www.example.com/" } :: arg[2]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "loading", incognito: false, width: 1100, 5 more… }
history.onVisited                 ->  arg[0]= Object { id: "fGv8w_MX50EI", url: "http://www.example.com/", title: "", lastVisitTime: 1487871931320, visitCount: 4, typedCount: 1 }
webNavigation.onCommitted         ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487871931480, frameId: 0, parentFrameId: -1, tabId: 11, transitionType: "link", transitionQualifiers: Array[0] }
tabs.onUpdated                    ->  arg[0]= 11 :: arg[1]= Object { status: "complete" } :: arg[2]= Object { id: 11, index: 2, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: "complete", incognito: false, width: 1100, 5 more… }
webNavigation.onDOMContentLoaded  ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487871931652, frameId: 0, parentFrameId: -1, tabId: 11 }
webNavigation.onCompleted         ->  arg[0]= Object { url: "http://www.example.com/", timeStamp: 1487871931657, frameId: 0, parentFrameId: -1, tabId: 11 }

Firefox now supports tabs.Tab.openerTabId(从 2017 年 11 月 14 日推出的 Firefox 57 开始支持)