From 5c73796b11746cdf93a5d2e4e7ea94908ec66cb1 Mon Sep 17 00:00:00 2001 From: Dmitrii Gerasimov Date: Fri, 22 Mar 2019 00:13:46 +0000 Subject: [PATCH] use localstorage so extension preserves state in firefox (#13) * use localstorage so extension preserves state in firefox --- src/js/options.js | 2 +- src/js/popup.js | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/js/options.js b/src/js/options.js index 98b4d36..ff2f805 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -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); diff --git a/src/js/popup.js b/src/js/popup.js index 6e6aaf2..74f9505 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -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);