Save webextension toggle state

Save whether the snowflake extension is currently enabled in local
storage so that users do not need to opt-in again every time the browser
is restarted
This commit is contained in:
Cecylia Bocovich 2019-07-02 11:51:47 -04:00
parent e93584029a
commit 2cd69f6f25
2 changed files with 16 additions and 1 deletions

View file

@ -59,6 +59,7 @@ class WebExtUI extends UI
constructor: ->
@initStats()
@initToggle()
chrome.runtime.onConnect.addListener @onConnect
initStats: ->
@ -69,6 +70,18 @@ class WebExtUI extends UI
@postActive()
), 60 * 60 * 1000
initToggle: ->
try
getting = chrome.storage.local.get("snowflake-enabled", (result) =>
@enabled = result['snowflake-enabled']
update()
chrome.browserAction.setIcon
path:
32: "icons/status-" + (if @enabled then "on" else "off") + ".png"
)
catch
log "Toggle state not yet saved"
postActive: ->
@port?.postMessage
active: @active
@ -90,6 +103,7 @@ class WebExtUI extends UI
path:
32: "icons/status-" + (if @enabled then "on" else "off") + ".png"
@postActive()
storing = chrome.storage.local.set({"snowflake-enabled":@enabled}, () -> log "Stored toggle state")
onDisconnect: (port) =>
@port = null