/* Tells or toggles for any URL whether the extension should be enabled. * Groups URL by their origin; a whole origin is enabled or disabled. */ import browser from 'webextension-polyfill' import listenableStorage from './listenableStorage' const browserStorage = listenableStorage(browser.storage) // Whichever is supported. // XXX Causes data loss when the browser suddenly supports the sync storage area. const storage = browserStorage.sync || browserStorage.local const keyForUrl = url => { const origin = new URL(url).origin return `enable_on_origin_${origin}` } export async function disable(url) { await storage.set({ [keyForUrl(url)]: false }) } export async function enable(url) { await storage.set({ [keyForUrl(url)]: true }) } export async function toggleEnabled(url) { const enabled = await isEnabled(url) if (enabled) { await disable(url) } else { await enable(url) } } export async function isEnabled(url, defaultValue = true) { const key = keyForUrl(url) const result = await storage.get({ [key]: defaultValue }) return result[key] } export function onEnabledChange(url, cb) { storage.onChanged(keyForUrl(url), cb) }