menus.overrideContext()

隱藏所有預設的 Firefox 選單項,以提供自定義的上下文選單 UI。

overrideContext 方法將導致此擴充套件匹配的選單項顯示在預設選單中。此方法應從 contextmenu DOM 事件處理程式呼叫,並且僅適用於在該事件後開啟的選單。

此介面需要 menus.overrideContext 許可權

語法

js
browser.menus.overrideContext(
  contextOptions // object
)

引數

contextOptions

object。有關如何覆蓋上下文選單的選項。

showDefaults 可選

boolean。是否也在選單中包含預設選單項。

context 可選

string。要覆蓋的 ContextType,允許選單中包含來自其他擴充套件的選單項。目前僅支援 'bookmark''tab'showDefaults 不能與此選項一起使用。

bookmarkId 可選

string。當 context 為 'bookmark' 時必需。需要“bookmark”許可權。

tabId 可選

integer。當 context 為 'tab' 時必需。需要“tabs”許可權。

示例

在此情況下,在自定義 UI 中開啟選項卡上下文選單。

js
document.addEventListener(
  "contextmenu",
  (event) => {
    const foo = event.target.closest(".foo");
    if (foo) {
      // When the context menu is opened on an element with the foo class
      // set the context to "opening a tab context menu".
      browser.menus.overrideContext({
        context: "tab",
        tabId: parseInt(foo.dataset.tabId, 10),
      });
    }
  },
  { capture: true },
);

有關更多詳細資訊,請參閱 這篇博文

瀏覽器相容性