Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension alternatives to local storage?

I'm looking for a storage mechanism which will persist across websites. I've got primitive data types numbers / strings which I need to store. localStorage doesn't work for me because it falls under the same origin policy. I need my data to be the same across all websites but much be tab specific and accessed via contentscripts.

Could someone suggest a suitable mechanism for achieveing this?

EDIT: I'm currently implementing the first answers code and not having much look. I've got the following...

background.html

note: tabStorage is a class variable in this function

    function store(){
    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.req == "save") {
        tabStorage[sender.tab.id] = request.data;
       }

    if(request.req == "load") {
        sendResponse(tabStorage[sender.tab.id]);
    }
    });
}

contentscript.js

response is returning undefined

 chrome.extension.sendRequest({req: "load"}, function(response) {
            console.log("getting stak");
            console.log(response.data);
 });

what am I doing wrong? the code is accessing both load and save so I know it's getting there and back but undefined is returning. why?

like image 317
Skizit Avatar asked May 03 '11 16:05

Skizit


People also ask

Can Chrome extensions use local storage?

Items in local storage are local to the machine the extension is installed on. The browser may restrict the amount of data that an extension can store in the local storage area. For example: In Chrome, an extension is limited to storing 5MB of data using this API unless it has the "unlimitedStorage" permission.

Why is it better to use IndexedDB instead of localStorage?

If you want to store structured data on the client side, IndexedDB is the better choice, especially since localStorage isn't built to store sensitive information. But if you're storing a simple, small amount of key-value pair data, use localStorage.

Is Chrome local storage safe?

Local storage is inherently no more secure than using cookies. When that's understood, the object can be used to store data that's insignificant from a security standpoint.

Can other site access local storage?

No, localStorage is stored on local machine, not shared on network. So other machines would not able to access localStorage directly. Ipang Since it's client side data that localStorage stored, it doesn't used for recognizing user normally.


1 Answers

You need to use extension's own localStorage which you access from a background page. If you need this information in a content script, you need to send a request to a background page using messaging, read it there, and send a result back to a content script in a response.

UPDATE If you don't need the storage to keep values between browser restarts then you don't need the localStorage. Just store everything in a background page and identify tabs by their ids. Background page will keep the values as long as the browser is opened. For example:

content script:

//save
chrome.extension.sendRequest({cmd: "save", data: {param1: "value1", param2: "value2"});
...
//load
chrome.extension.sendRequest({cmd: "load"}, function(response) {
    console.log("tab data:", response)
});

background page:

var tabStorage = [];
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "save") {
        tabStorage[sender.tab.id] = request.data;
    }

    if(request.cmd == "load") {
        sendResponse(tabStorage[sender.tab.id]);
    }
});
like image 93
serg Avatar answered Oct 30 '22 13:10

serg