Recently I started developing some extensions for Chrome. I am doing this in manifest v3 and vanilla javascript. I have managed to make my extensions work. However, part of the fun of creating them is in learning to code better. I have been wondering whether I am handling variables in a correct way.
I have some data that I like to access from various points in my extension. Some of it is loaded dynamically and some I set in the code directly, like this:
const myExtensionData = {
someData: [],
config: [],
links: [],
}
1) Is it a good idea to store often-used data in a variable like myExtensionData.someData? Is there a better alternative?
myExtensionData.someData
is loaded from externally at some point in time. As I use this data in various functions, I have exposed it like this, so that I don't have to pass the data every time. I could use the local storage, but I have been holding that back as that requires another permission.
2) Same question, but this time with stuff that is rarely used (myExtensionData.config)?
The data in myExtensionData.config
is also created dynamically, but the contents are not used as much. For example: I store whether one of my other extensions is also installed. If it is, I enable a button to access it from the popup. Rather single-use.
3) How about static variables? (myExtensionData.links)?
I keep some links I use for onUpdate, onInstall and for release notes. They change regularly, so I needed to store them somewhere. How about storing them in myExtensionData (I actually keep them in a global const at the moment).
4) Any pointers are appreciated! I hope this is a worthy question. It's not very specific at all, but I am sure I can learn a lot here. I have searched for good practices/best practices, but they are usually outside the context of extension. If I have been looking at all the wrong places, please let me know.
Trying to answer my own question
I found some interesting stuff in this very old question. As I understand it, it's not a bad thing to store a variable in an object. Additionally, I like that it is also suggested to add some methods to the object as well.