我开发了Chrome扩展程序,并尝试使用Firefox WebExtensions将其移植到Firefox。
这是我面临的问题。在我的扩展程序中,我需要使用chrome.tabs.Tab.openerTabId
,这是Firefox WebExtensions不支持的(请参阅documentation)。
我想出了一个解决方法,通过查询当前窗口中的活动标签来自己获取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。不幸的是,这个问题没有解决: - (。
答案 0 :(得分:2)
Firefox now supports tabs.Tab.openerTabId
(自Firefox 57(于2017年11月14日发布以来受支持)
答案 1 :(得分:1)
自Firefox 57起,Firefox的桌面版支持tabs.Tab.openerTabId
。
适用于Android的Firefox does not support tabs.Tab.openerTabId
如果您希望自己的扩展程序与Android版Firefox兼容,则必须自行跟踪此信息。您可以跟踪用户通过单击链接打开的选项卡的开启者选项卡ID。当JavaScript创建一个新选项卡时,您可以猜测它是由活动选项卡中的JavaScript完成的,但您无法知道。您可以通过跟踪当前活动选项卡,侦听要创建的新选项卡以及监听创建新选项卡的原因是因为用户单击了链接。这可以通过听取:
来完成tabs.onActivated
跟踪当前有效的标签页
这是在tabs.onCreated
之前触发的,因此您需要保留先前活动标签的记录。对于立即激活新打开的选项卡的情况,将需要这样做。显然,您还需要考虑新选项卡未立即激活的情况。tabs.onCreated
确定何时创建新标签页。webNavigation.onCommitted
查看transitionType
以查看新标签实际上是从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 }