runtime.getContexts()

返回有關擴充套件程式相關聯的上下文的資訊。

語法

js
let gettingContexts = await browser.runtime.getContexts(
    filter           // object
);

引數

filter

一個包含用於匹配返回的上下文的條件的標準的物件。匹配的上下文必須匹配所有指定的篩選條件。如果物件為空,則返回所有上下文。

contextIds 可選

一個 string 陣列。要返回的上下文的 ID。

contextTypes 可選

一個 string 陣列。要返回的與上下文相關聯的擴充套件檢視的型別。接受值 "BACKGROUND""POPUP""SIDE_PANEL""TAB"

documentIds 可選

一個 string 陣列。要返回的與上下文相關聯的文件的 UUID。

documentOrigins 可選

一個 string 陣列。要返回的與上下文相關聯的文件的源。

documentUrls 可選

一個 string 陣列。要返回的與上下文相關聯的文件的 URL。

frameIds 可選

一個 integer 陣列。要返回的上下文的幀 ID。

incognito 可選

boolean。是否僅返回託管在隱私瀏覽標籤頁中的上下文。

tabIds 可選

一個 integer 陣列。要返回的上下文的標籤 ID。

windowIds 可選

一個 integer 陣列。要返回的上下文的視窗 ID。

返回值

一個 Promise,它將以一個物件陣列的形式解析,每個物件代表一個託管擴充套件內容的上下文。這些物件具有以下屬性:

contextId

string。上下文的 ID。

contextType

string。擴充套件檢視的型別。返回值為 "BACKGROUND""POPUP""SIDE_PANEL""TAB" 之一。

documentId

string。與上下文相關聯的文件的 UUID,如果上下文未託管在文件中,則為 undefined

documentOrigin

string。與上下文相關聯的文件的源,如果上下文未託管在文件中,則為 undefined

documentUrl

string。與上下文相關聯的文件的 URL,如果上下文未託管在文件中,則為 undefined

frameId

integer。上下文的幀 ID,如果上下文未託管在幀中,則為 -1

incognito

boolean。上下文是否託管在隱私瀏覽標籤頁中。

tabId

integer。上下文的標籤 ID,如果上下文未託管在標籤頁中,則為 -1

windowId

integer。上下文的視窗 ID,如果上下文未託管在視窗中,則為 -1

如果沒有匹配的上下文,則以空陣列解析。

示例

此示例獲取擴充套件程式在隱私瀏覽標籤頁中關聯的所有上下文,並將每個上下文的標籤 ID、幀 ID 和文件 URL 列印到控制檯。

js
function gotContextInfo(contexts) {
  for (const context of contexts) {
    if (context.tabId === -1) {
      console.log("Not hosted in a tab");
    } else {
      console.log(
        `Hosted in tab: ${context.tabId} and frame ${context.frameId} with URL ${context.documentUrl}`,
      );
    }
  }
}

let gettingContextInfo = browser.runtime.getContexts({ incognito: true });
gettingContextInfo.then(gotContextInfo);

瀏覽器相容性