![](/img/trans.png)
[英]WebExtensions and Firefox Android: How to get a BrowserApp object
[英]How to get openerTabId in Firefox WebExtensions?
我開發了Chrome擴展程序,並嘗試使用Firefox WebExtensions將其移植到Firefox。
這是我面臨的問題。 在我的擴展中,我需要使用chrome.tabs.Tab.openerTabId
,這是Firefox WebExtensions不支持的(參見文檔 )。
我想出了一個解決方法,通過查詢當前窗口中的活動選項卡來獲取openerTabId
:
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
// tabs has only one element, which is the one I just opened
});
但它不起作用,因為此查詢的結果是我剛打開的選項卡,而不是我單擊“新建選項卡”按鈕之前的選項卡 。
現在我想在單擊New Tab按鈕之前查詢活動選項卡,但我沒有在chrome.tabs.query()
看到任何參數,如lastFocusedTab
。
這可以通過chrome.tabs.query()
來完成嗎? 或者還有其他方法嗎?
可能重復: Firefox WebExtension中未定義tab.openerTabId 。 不幸的是,這個沒有解決:-(。
Firefox 現在支持tabs.Tab.openerTabId
(自2017年11月14日推出的Firefox 57以來一直支持)
從Firefox 57開始 ,桌面版Firefox支持tabs.Tab.openerTabId
。
Firefox for Android 不支持 tabs.Tab.openerTabId
如果您希望您的擴展程序與Android版Firefox兼容,則必須自行跟蹤此信息。 您可以跟蹤用戶通過單擊鏈接打開的選項卡的開啟者選項卡ID。 當JavaScript創建一個新選項卡時,您可以猜測它是由活動選項卡中的JavaScript完成的,但您無法知道。 您可以通過跟蹤當前活動選項卡,偵聽要創建的新選項卡以及監聽創建新選項卡的原因是因為用戶單擊了鏈接。 這可以通過聽取:
tabs.onActivated
以跟蹤當前活動的選項卡 tabs.onCreated
之前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');
打開新標簽頁時的事件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 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.