use localstorage so extension preserves state in firefox (#13)
* use localstorage so extension preserves state in firefox
This commit is contained in:
parent
a3c8e1cb94
commit
5c73796b11
|
@ -9,9 +9,21 @@ const COMMENT_ID = 'comment_id';
|
||||||
const TAGS_ID = 'tags_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 {
|
function getComment(): HTMLInputElement {
|
||||||
const comment = ((document.getElementById(COMMENT_ID): any): HTMLInputElement);
|
const comment = ((document.getElementById(COMMENT_ID): any): HTMLInputElement);
|
||||||
return comment;
|
return comment;
|
||||||
|
@ -27,12 +39,6 @@ function getButton(): HTMLElement {
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
type State = {
|
|
||||||
comment: string,
|
|
||||||
tag_str: string,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function getState(): State {
|
function getState(): State {
|
||||||
const comment_text = getComment().value;
|
const comment_text = getComment().value;
|
||||||
const tag_str = getTags().value;
|
const tag_str = getTags().value;
|
||||||
|
@ -96,12 +102,11 @@ function setupPage () {
|
||||||
|
|
||||||
getButton().addEventListener('click', submitComment);
|
getButton().addEventListener('click', submitComment);
|
||||||
|
|
||||||
|
|
||||||
const old_state = background.grasp_state || null;
|
|
||||||
restoreState(old_state);
|
|
||||||
background.grasp_state = null;
|
|
||||||
|
|
||||||
window.submitted = false;
|
window.submitted = false;
|
||||||
|
|
||||||
|
const state = load_state();
|
||||||
|
restoreState(state);
|
||||||
|
save_state(null); // clean
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', setupPage);
|
document.addEventListener('DOMContentLoaded', setupPage);
|
||||||
|
@ -109,6 +114,6 @@ document.addEventListener('DOMContentLoaded', setupPage);
|
||||||
|
|
||||||
window.addEventListener('unload', () => {
|
window.addEventListener('unload', () => {
|
||||||
if (!window.submitted) {
|
if (!window.submitted) {
|
||||||
background.grasp_state = getState();
|
save_state(getState());
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
|
|
Loading…
Reference in a new issue