use localstorage so extension preserves state in firefox (#13)

* use localstorage so extension preserves state in firefox
This commit is contained in:
Dmitrii Gerasimov 2019-03-22 00:13:46 +00:00 committed by GitHub
parent a3c8e1cb94
commit 5c73796b11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 14 deletions

View file

@ -9,9 +9,21 @@ const COMMENT_ID = 'comment_id';
const TAGS_ID = 'tags_id';
const background = chrome.extension.getBackgroundPage();
type State = {
comment: string,
tag_str: string,
};
function save_state(state: ?State) {
window.localStorage.setItem('state', JSON.stringify(state));
}
function load_state(): ?State {
const sts = window.localStorage.getItem('state') || null;
return JSON.parse(sts);
}
function getComment(): HTMLInputElement {
const comment = ((document.getElementById(COMMENT_ID): any): HTMLInputElement);
return comment;
@ -27,12 +39,6 @@ function getButton(): HTMLElement {
return button;
}
type State = {
comment: string,
tag_str: string,
};
function getState(): State {
const comment_text = getComment().value;
const tag_str = getTags().value;
@ -96,12 +102,11 @@ function setupPage () {
getButton().addEventListener('click', submitComment);
const old_state = background.grasp_state || null;
restoreState(old_state);
background.grasp_state = null;
window.submitted = false;
const state = load_state();
restoreState(state);
save_state(null); // clean
}
document.addEventListener('DOMContentLoaded', setupPage);
@ -109,6 +114,6 @@ document.addEventListener('DOMContentLoaded', setupPage);
window.addEventListener('unload', () => {
if (!window.submitted) {
background.grasp_state = getState();
save_state(getState());
}
}, true);