sessions.getWindowValue() 编辑

Retrieves a value previously stored by a call to sessions.setWindowValue.

You can retrieve a value from a window even over a close/restore cycle: meaning that if you set a value, then the user closes the window, then restores the window using the browser's "restore window" feature (for example, by pressing Control+Shift+N), then you will be able to retrieve the value from the restored window. Note, though, that a restored window does not get the same ID as the original, so the ID you pass into getWindowValue() will be different from the ID you passed into setWindowValue(), even though they both refer to the same window.

This is an asynchronous function that returns a Promise.

Syntax

var retrieving = browser.sessions.getWindowValue(
  windowId,    // integer
  key          // string
)

Parameters

windowId
integer. ID of the window whose data you are trying to retrieve. Error is thrown if ID is invalid.
key
string. Key identifying the particular value to retrieve. This needs to match the key previously given in sessions.setWindowValue.

Return value

A Promise that will be resolved with the value if it exists, or undefined if it does not exist. If the call failed (for example, because the window ID could not be found) then the promise will be rejected with an error message.

Browser compatibility

BCD tables only load in the browser

Examples

Log the value of "my-key" for all newly created windows (this will include any windows that have been restored):

function onGetResolved(r) {
  console.log(`success: ${r}`);
}

function onGetRejected(e) {
  console.log(`error: ${e}`);
}

browser.windows.onCreated.addListener((window) => {
  browser.sessions.getWindowValue(window.id, "my-key").then(onGetResolved, onGetRejected);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:93 次

字数:3183

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文