DEV Community

Cover image for Chrome extensions: Local storage

Chrome extensions: Local storage

Paula Santamaría on February 24, 2021

I'm back with another post about Chrome extensions! This time I wanted to explore how to store data locally using the chrome.storage API. In this ...
Collapse
 
dilbwagsingh profile image
Dilbwag Singh

It was quite easy to follow along and understand as a beginner. But I have a doubt regarding the "page.service.js" file. What is that file actually? If I rename it to say just "page.js" at appropriate places, the code breaks and suddenly it cannot find the PageService.savePage function in "background.js" script. Can anyone explain? Links to further resources on the same will be much appreciated. Thank you.

Collapse
 
paulasantamaria profile image
Paula Santamaría • Edited

Hi Dilbwag! The page.service.js is a regular file containing a class. I just got into the habit of naming my data access files using the suffix .service, but it could've been called page-service.js too, for example.
The reason your code breaks when you change the file's name is that you must also change the references to the file in the following places:

  • manifest.json: Where you declare the background scripts, search for the page.service.js entry at the end of the scripts array and replace it with the new filename (e.g. page.js)
  • popup.html: At the end of the body tag, we added a script tag referencing page.service.js. It should be replaced to reference the new filename.

Updating the filename in those 2 places should do it. Hope it helps! :)

Collapse
 
dilbwagsingh profile image
Dilbwag Singh • Edited

It does work now. Thank you so much. Also, are you planning on continuing this series for updating it to manifest V3?

Thread Thread
 
paulasantamaria profile image
Paula Santamaría

Yes! You read my mind 😂 I'm actually working on an article about Manifest V3 right now. My plan is to do an overview and then go through the steps to migrate this sample extension to v3 :)

Thread Thread
 
dilbwagsingh profile image
Dilbwag Singh

Cool😁 Will be waiting for it!!!

Collapse
 
krishan111 profile image
Krishan111

This post is really creative, I have never thought about doing so👍👍

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thank you! 😊

Collapse
 
chan_austria777 profile image
chan 🤖

For example:
X extension is installed in Google Profile 1
X extension is installed in Google Profile 2

Is the data chrome.storage.local accessible to other google profiles where the same extension is installed under the same device?
Or is it only local to one google profile?

Collapse
 
jacobkim9881 profile image
Jacobkim • Edited

I use local storage for chrome extensions. For me I use this as send state like redux. Local storage is good to save not sensible data on a browser. But I think the point is local storages are 3 for a tab(contents.js), background, and popup. It's nice information!

Collapse
 
inquisitivedev2 profile image
Patrick Daneshyar

Hi,

Thank you for making this guide I trying to make an extension for the first time and this is really helpful. I've followed your steps here and created a service class called PageService that contains my storage methods(static methods) and added the file to my popup.html as you've shown but when I try to call any of the static methods from my popup.js file I keep getting an error saying PageService is not defined. I've tried numerous attempts to fix it, but do you have any suggestions?

I can see you are able to call them without any trouble so not sure what I'm missing.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Hi! Could it be that the <script src='page.service.js'></script> is missing in your popup.html? Or maybe the filename/path is different than mine?

Collapse
 
inquisitivedev2 profile image
Patrick Daneshyar

This is the error I get
popup.js:64 Uncaught (in promise) ReferenceError: PageService is not defined at HTMLButtonElement.

Collapse
 
exharrison profile image
exharrison

This has been a great help as I was planning on building a simple chrome extension, but have no background in javascript. Helped me get through a bunch of the pieces. I could not find a license declared in your repo. What license do you intend the code to be shared under?

Collapse
 
nikhilmwarrier profile image
nikhilmwarrier

Super useful. How can you do the same for a Firefox Addon?

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thanks! I'm planning to cover how to adapt a chrome extension so it works on firefox in a future post :)

Collapse
 
ra1nbow1 profile image
Matvey Romanov

Pretty nice and useful guide, thanks a lot

Collapse
 
paulasantamaria profile image
Paula Santamaría

Glad you found it useful 😊