+6

Use chrome.storage.local rather than localStorage

Sae Hirak 11 years ago updated by nao 7 years ago 8

As the title says, it might be better to use chrome.storage.local rather than localStorage. There's two reasons for this: chrome.storage is asynchronous, which supposedly makes it faster. But more importantly, localStorage is limited to either 2.5MB or 5MB of data. But if you use chrome.storage, you can use the "unlimitedStorage" permission.


Speaking from experience writing Chrome Extensions, chrome.storage.local is the best way I've found to persistently store user data. And if you're worried about it being async, it's easy to write a buffer so that the API remains synchronous, but behind the scenes it's still async. I can give more details if you like, including working code.

So... This is why my Sidewise has been breaking after a while. My localstorage is 10 MB big, and Sidewise just won't load anymore. Fantastic.
https://hacks.mozilla.org/2012/03/there-is-no-simple-solution-for-local-storage/

Hello .I want to download" chrome of 2.5mb "but in vain.

Hi!

If anyone finds this post while looking for a solution to the storage size, I've got a quick fix for you.


Symptom: you opened a ton of tabs (let's say over 2000 but it can be less or more, depends on what's in the session list), then you quit Chrome (or had it crash), relaunched it, and Sidewise no longer loads your tabs.


Solution: go to your browser's extension folder.


More precisely, in my case with Opera (I'll let you look for the equivalent Chrome folder):

C:\Users\**Your user name**\AppData\Roaming\Opera Software\Opera Stable\Extensions\biiammgklaefagjclmnlialkmaemifgo\2016.5.5.0_0\js\bg\


Open the file 'background.js', and find this:

localStorage.backupPageTreeLastSession=localStorage.backupPageTree


Replace with:

localStorage.backupPageTreeLastSession=''

(That's just two single quotes.)


Now just disable Sidewise and re-enable it (or restart your browser).


What does this do..? Sidewise has 3 copies of your complete tab session: pageTree (current one), backupPageTree (latest "safe" tab tree), and backupPageTreeLastSession (last working tree before Sidewise got initialized, i.e. usually before you launched Chrome or Opera). Since the maximum storage space is 5MB, having a session at around 1.6 or 1.7MB in size will fill the maximum space allowed, and the line I gave you above will quite simply crash your extension. Replacing it with an empty string ('') will reduce your storage use by 1.7MB, thus allowing you to go up to 2.3MB or something in size-- should be more than enough to take time to close as many tabs as you can. After that, quit your browser, and if you want to restore the last session feature, just restore the background.js file to what it used to be.


Please note, my first attempt was to convert the localStorage stuff to chrome.storage.local, I first wasted hours trying to add the unlimitedStorage permission (turns out I needed to package a new extension otherwise it just ignored my manifest changes), then after that I realized the whole asynchronous thing, which makes it hard to just mindlessly convert the code (although it has to be said, localStorage isn't used MUCH... Only in the line above, and in settings.js. Maybe I'll get back to it in the future. I can always write a synchronous function that simply waits for a flag to be set by the asynchronous one. I was just too tired for it.)

Or better yet, use Tabs Outliner. Sidewise is dead. I have 8000+ tabs in TabsOutliner. No performance issues. Never lost a single tab (unlike the thousands lost with Sidewise).

Hi all, I have a fix for the localstorage limitation / data loss problem implemented and am testing it now. Apologies for the very extended delay.

I have released an update that implements usage of chrome.storage.local for the tree data. From the changelog:


## 2017.2.12.0

* Fix a problem where Sidewise tree data could get corrupted if it exceeded several thousand total tabs. Sidewise should now be able to store an essentially unlimited number of tabs.


Please try out the new version and let me know how it goes for you.

First of all, thanks a lot for tackling this issue! I won't be able to see if it works before I quit Opera because, well, I'm working & all, but I really thought development was left aside(wise), and given the number of issues opened, it's great to know the only important one for tab hoarders has been addressed ;-)))))


Also, thanks a lot for publishing a non-uglified version. I don't know if it wasn't by mistake, but your source code for this version has all of the variable names intact, which should make it easier for everyone to debug as needed. (Plus, uglification is needed for websites to make them load a bit faster, but extensions are cached so it shouldn't be a concern...)

I see that you've made a lot of changes, not just to the way you're using the storage object instead of HTML5 local storage. I wasn't even aware there was an 'async' keyword for function declarations. ;)

I restarted, and I can confirm it's working with 2365 tabs open. Congratulations! And thanks for making life easier!

I'll post another message with a tip, because I feel like being helpful :)