i am designing a chrome extension that gets Email from user. each time that user close the browser the value of Email will be lost. can i define a static variable? i need an unchangeable variable.
Asked
Active
Viewed 574 times
1
-
I'm fairly sure you don't want a static unchangable variable if it's used to store the users email..? Sounds like dynamic behavior to me – Scherling Dec 17 '14 at 10:58
-
The term "static variable" has very specific meanings in programming but your question has nothing to do with that. You ask for a variable that exists outside a program, something that is physically impossible. What you really want is to know the storage options provided by Chrome. – Álvaro González Dec 17 '14 at 11:05
2 Answers
3
You can save it to extensions storage https://developer.chrome.com/extensions/storage
// Save it using the Chrome extension storage API.
chrome.storage.sync.set({'value': theValue}, function() {
// Notify that we saved.
message('Settings saved');
});

polacekpavel
- 431
- 3
- 9
0
Another option, though outdated, is to use localStorage
in your extension's pages, like the background page and the popup.
An advantage of that method is synchronous access.
However, it is not available to content scripts (or rather, content scripts share localStorage with the page), and you have to serialize values yourself. chrome.storage
was made to address those limitations.
Edit: take a look at this question for a comparison.
-
please reed my extension code and tell me your point of viwe. – Kiarash Vazirzade Nobary Dec 19 '14 at 08:29
-
I am strictly against radically changing questions _after_ they receive responses. If you have a follow-up question about your concrete code, post it separately. – Xan Dec 19 '14 at 08:31
-
you are right but site suggested my to edit my post and i assumed it's common thing. – Kiarash Vazirzade Nobary Dec 19 '14 at 10:32
-
I agree - it is when you're adding information. But you changed the question from "how to, in theory" into "why this doesn't work". – Xan Dec 19 '14 at 13:13
-
actually i think the answers are correct but i cant implement them into my extension so i put the entire file here.i hope somebody like you reed the storage part and tell me what is wrong. do you think i need to create another topic for this ? thank you – Kiarash Vazirzade Nobary Dec 19 '14 at 15:08
-
can you visit my new question? [link](http://stackoverflow.com/questions/27574012/how-can-i-keep-the-value-of-variable-after-reload-browser-in-chrome-estensions) – Kiarash Vazirzade Nobary Dec 20 '14 at 21:58