Chrome扩展程序将属性附加到每个标签 [英] Chrome extension attach properties to each tab

查看:122
本文介绍了Chrome扩展程序将属性附加到每个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome扩展中,我创建了试图与每个选项卡一起存储的选项卡属性。做这个的最好方式是什么?我研究过使用localStorage,但似乎可能有一个更简单的方法。数据绝不是永久性的,它只存在于选项卡中。

In a chrome extension, I've created tab properties that I'm trying to store with each tab. What is the best way to do this? I've looked into using localStorage, but it seems like there could be an easier way. The data is by no means permanent, it only exists as long as the tab does.

推荐答案

绝对不需要使用 localStorage的。如果没有数据绝不是永久的概念,人们已经知道:标签ID在会话中是唯一的。从这个事实可以看出,数据是非持久的。

There's definitely no need to use localStorage. Without the notion "data is by no means permanent", one already knows that: tab IDs are unique within a session. From this fact, it follows that the data is non-persistent.

实现它的最好方法是维护一个标签属性的散列:

The best method to implement it is to maintain a hash of tab properties:

  • chrome.tabs.onCreated (optional, add initial info to tab hash)
  • chrome.tabs.onUpdated - (Add/) Update tab hash (URL is available)
  • chrome.tabs.onRemoved - Remove hash entry

标签对象并不昂贵:所有属性都是原始的(布尔值,数字,字符串)。

Tab objects are not expensive: All properties are primitives (booleans, numbers, strings).

例如(仅限背景页面):

For instance (background page only):

var tabStore = {};         // <-- Collection of tabs
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    tabStore[tabId] = tab;
});
chrome.tabs.onRemoved.addListener(function(tabId) {
    delete tabStore[tabId];
});

这篇关于Chrome扩展程序将属性附加到每个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆