語法
js
let removing = browser.sessions.removeWindowValue(
windowId, // integer
key // string
)
引數
windowId-
integer。您嘗試刪除資料的視窗 ID。如果 ID 無效,將丟擲錯誤。 key-
string。用於標識要刪除的特定值的鍵。這需要與之前在sessions.setWindowValue中提供的鍵匹配。
返回值
一個 Promise,如果專案已成功刪除,則會以不帶引數的形式解析。如果呼叫失敗(例如,因為找不到視窗 ID),則 Promise 將以錯誤訊息被拒絕。
示例
此程式碼添加了兩個上下文選單項:一個儲存與當前視窗關聯的值,另一個則將其刪除。
js
async function setOnActiveWindow() {
let currentWindow = await browser.windows.getLastFocused();
await browser.sessions.setWindowValue(currentWindow.id, "my-key", "my-value");
}
async function removeFromActiveWindow() {
let currentWindow = await browser.windows.getLastFocused();
await browser.sessions.removeWindowValue(currentWindow.id, "my-key");
}
browser.menus.create({
id: "add-my-item",
title: "add item",
contexts: ["all"],
});
browser.menus.create({
id: "remove-my-item",
title: "remove item",
contexts: ["all"],
});
browser.menus.onClicked.addListener((info) => {
if (info.menuItemId === "add-my-item") {
setOnActiveWindow();
} else {
removeFromActiveWindow();
}
});
瀏覽器相容性
載入中…