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: -> constructor: ->
@initStats() @initStats()
@initToggle()
chrome.runtime.onConnect.addListener @onConnect chrome.runtime.onConnect.addListener @onConnect
initStats: -> initStats: ->
@ -69,6 +70,18 @@ class WebExtUI extends UI
@postActive() @postActive()
), 60 * 60 * 1000 ), 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: -> postActive: ->
@port?.postMessage @port?.postMessage
active: @active active: @active
@ -90,6 +103,7 @@ class WebExtUI extends UI
path: path:
32: "icons/status-" + (if @enabled then "on" else "off") + ".png" 32: "icons/status-" + (if @enabled then "on" else "off") + ".png"
@postActive() @postActive()
storing = chrome.storage.local.set({"snowflake-enabled":@enabled}, () -> log "Stored toggle state")
onDisconnect: (port) => onDisconnect: (port) =>
@port = null @port = null

View file

@ -13,5 +13,6 @@
}, },
"default_title": "Snowflake", "default_title": "Snowflake",
"default_popup": "popup.html" "default_popup": "popup.html"
} },
"permissions": ["storage"]
} }