theme.update()

根据 Theme 对象的内容更新浏览器主题。

要使用此方法,扩展必须在其 manifest.json 文件中请求 "theme" 权限

语法

js
browser.theme.update(
  windowId,    // integer
  theme        // object
)

参数

windowId 可选

integer。窗口的 ID。如果提供了此 ID,则主题仅应用于该窗口。如果省略此 ID,则主题将应用于所有窗口。

theme

object。一个 Theme 对象,指定要修改的 UI 元素的

示例

将浏览器主题设置为使用太阳图形和互补的背景色

js
const sunTheme = {
  images: {
    theme_frame: "sun.jpg",
  },
  colors: {
    frame: "#CF723F",
    tab_background_text: "#111111",
  },
};

browser.theme.update(sunTheme);

仅为当前焦点窗口设置主题

js
const day = {
  images: {
    theme_frame: "sun.jpg",
  },
  colors: {
    frame: "#CF723F",
    tab_background_text: "#111111",
  },
};

browser.menus.create({
  id: "set-theme",
  title: "set theme",
  contexts: ["all"],
});

async function updateThemeForCurrentWindow() {
  let currentWindow = await browser.windows.getLastFocused();
  browser.theme.update(currentWindow.id, day);
}

browser.menus.onClicked.addListener(updateThemeForCurrentWindow);

扩展程序示例

浏览器兼容性