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
|
@ -14,7 +14,7 @@ function default_options(): Options {
|
|||
};
|
||||
}
|
||||
|
||||
export function get_options(cb: (Options) => void) {
|
||||
export function get_options(cb: (Options) => void) {
|
||||
chrome.storage.local.get(null, res => {
|
||||
res = {...default_options(), ...res};
|
||||
cb(res);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue