I'm making a Chrome extension and I need it to detect the very first time they install the extension (not just update it) so chrome.runtime.onInstalled
won't work. I need to ask them (the users) a question the first time they install it and store it in localStorage
. e.g. localStorage.setItem("name", prompt("What is your name?"))
. You get the idea. My problem is just getting it to do it the first time it's installed and not do it every time it installs an update.
Asked
Active
Viewed 339 times
0

Brendan
- 55
- 12
-
you may test [OnInstalledReason](https://developer.chrome.com/extensions/runtime#type-OnInstalledReason) the first parameter of [onInstalled](https://developer.chrome.com/extensions/runtime#event-onInstalled). When reason is **install** you can go on... – gaetanoM Jun 26 '20 at 12:50
-
@wOxxOm why did you close this question because it was a duplicate of "Detect Chrome extension first run / update" and I thought I made it very clear that I need it only when it is installed for the first time, not updated LOL. Already got an answer so it's fine that it is closed but I thought that was pretty funny. – Brendan Jul 05 '20 at 19:03
-
That topic shows an example for both scenarios. That is also the correct solution, unlike the one posted here, which will fail if the storage is cleaned by a utility or by the user manually via devtools. – wOxxOm Jul 05 '20 at 19:22
-
@wOxxOm Thank you. I didn't think about that. I will look at that answer and see if it answers the question better (it probably will). – Brendan Jul 16 '20 at 22:41
-
@wOxxOm I was just reading through this again and realized that for my problem the answer here is actually exactly what I need. If the storage was cleaned I would need to collect it again. I've learned a lot more about development since I first asked this question :) – Brendan Dec 26 '20 at 21:52
1 Answers
0
You can try to read the stored value, and assume that if it doesn't exist it's the first time.
For example:
if (!localStorage.getItem("name")) {
// that's the first time the extension runs
} else {
// not the first time
}

Arik
- 5,266
- 1
- 27
- 26
-
Thank you! I thought of doing that before but for some reason I was thinking it wouldn't work cause there would be an error because it wouldn't exist yet (the localStorage). – Brendan Jun 26 '20 at 12:52