Javascript 如何在我的 chrome 扩展程序中本地保存信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5364062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:52:49  来源:igfitidea点击:

How can I save information locally in my chrome extension?

javascriptgoogle-chromegoogle-chrome-extension

提问by Ariel

I want my chrome extension to save some information, and I'm not sure how to start the code... I need it to save strings. For example - The user inputs a string (In a text area over the popup) and this string is shown on the popup. When the user exits it and returns I want the string to remain the same. (I belive it has to save it) I don't want to save it on my server or anything like that, I want it to be saved on the users cache or something.

我想让我的 chrome 扩展程序保存一些信息,但我不知道如何启动代码......我需要它来保存字符串。例如 - 用户输入一个字符串(在弹出窗口上方的文本区域中),该字符串显示在弹出窗口中。当用户退出并返回时,我希望字符串保持不变。(我相信它必须保存它)我不想将它保存在我的服务器或类似的东西上,我希望它保存在用户缓存或其他东西上。

回答by mattsven

You can now leverage Google Chrome's storageAPI to do this as well. Unlike localStorage, this is accessible from content scripts as well.

您现在也可以利用 Google Chrome 的storageAPI 来执行此操作。与 不同localStorage,这也可以从内容脚本访问。

//  Somewhere in your manifest...

{
    "permissions": [
        "storage"
    ]
}

//  Usage:

//  PERSISTENT Storage - Globally
//  Save data to storage across their browsers...

chrome.storage.sync.set({ "yourBody": "myBody" }, function(){
    //  A data saved callback omg so fancy
});

chrome.storage.sync.get(/* String or Array */["yourBody"], function(items){
    //  items = [ { "yourBody": "myBody" } ]
});

//  LOCAL Storage

// Save data to storage locally, in just this browser...

chrome.storage.local.set({ "phasersTo": "awesome" }, function(){
    //  Data's been saved boys and girls, go on home
});

chrome.storage.local.get(/* String or Array */["phasersTo"], function(items){
    //  items = [ { "phasersTo": "awesome" } ]
});

More info on how these shenanigans work here: https://developer.chrome.com/extensions/storage#type-StorageArea

有关这些恶作剧如何在这里工作的更多信息:https: //developer.chrome.com/extensions/storage#type-StorageArea

Former answer:

以前的回答:

Use localStorage. Google Chrome implements some features of HTML5, and it is one of them.

使用localStorage。谷歌浏览器实现了 HTML5 的一些特性,它就是其中之一。

//Pull text from user inputbox
var data = document.getElementById("this_input").value;
//Save it to the localStorage variable which will always remember what you store in it
localStorage["inputText"] = data; 

You should note you can only access your storage from the background page (no content scripts) so you'll have to use messaging for that.

您应该注意,您只能从后台页面访问您的存储(无内容脚本),因此您必须为此使用消息传递。

回答by BeauCielBleu

There is a specific API now, check this here: https://developer.chrome.com/extensions/storage

现在有一个特定的 API,请在此处查看:https: //developer.chrome.com/extensions/storage

Note that it is only available on your background page and works in an isolated world (you don't access the values contained in the HTML5 localStorage of the web site).

请注意,它仅在您的后台页面上可用并且在一个孤立的世界中工作(您无法访问网站的 HTML5 localStorage 中包含的值)。