Browser extension to hide view-reducing screen junk from websites.

listenableStorage.js 809 B

123456789101112131415161718192021222324252627
  1. /* Augments the WebExtension storage API to allow listening for changes on a
  2. * single variable of a single storage area (i.e. local or sync).
  3. *
  4. * Usage example:
  5. * const storage = listenableStorage(browser.storage)
  6. * storage.local.onChanged('someVar', newValue => { .... })
  7. */
  8. export default storage => {
  9. const mkOnChange = storageName => {
  10. return (key, cb) => {
  11. const listener = (changes, areaName) => {
  12. if (changes[key]) {
  13. const newValue = changes[key].newValue
  14. cb(newValue)
  15. }
  16. }
  17. storage.onChanged.addListener(listener)
  18. }
  19. }
  20. return storage && ({
  21. ...storage,
  22. local: storage.local && { ...storage.local, onChanged: mkOnChange('local') },
  23. sync: storage.sync && { ...storage.sync, onChanged: mkOnChange('sync') },
  24. })
  25. }