storage.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // chrome.storage.sync.clear()
  2. // chrome.permissions.getAll((p) => chrome.permissions.remove({origins: p.origins}))
  3. md.storage = ({compilers}) => {
  4. var defaults = md.storage.defaults(compilers)
  5. var state = {}
  6. function set (options) {
  7. chrome.storage.sync.set(options)
  8. Object.assign(state, options)
  9. }
  10. chrome.storage.sync.get((res) => {
  11. md.storage.bug(res)
  12. Object.assign(state, JSON.parse(JSON.stringify(
  13. !Object.keys(res).length ? defaults : res)))
  14. // mutate
  15. md.storage.migrations(state)
  16. // in case of new providers from the compilers branch
  17. Object.keys(compilers).forEach((compiler) => {
  18. if (!state[compiler]) {
  19. state[compiler] = compilers[compiler].defaults
  20. }
  21. })
  22. set(state)
  23. })
  24. return {defaults, state, set}
  25. }
  26. md.storage.defaults = (compilers) => {
  27. var match = '\\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\\?.*)?$'
  28. var defaults = {
  29. theme: 'github',
  30. compiler: 'marked',
  31. raw: false,
  32. header: true,
  33. match,
  34. themes: {
  35. width: 'auto',
  36. },
  37. content: {
  38. autoreload: false,
  39. emoji: false,
  40. mathjax: false,
  41. mermaid: false,
  42. scroll: true,
  43. syntax: true,
  44. toc: false,
  45. },
  46. origins: {
  47. 'file://': {
  48. match,
  49. csp: false,
  50. encoding: '',
  51. }
  52. },
  53. icon: false,
  54. }
  55. Object.keys(compilers).forEach((compiler) => {
  56. defaults[compiler] = compilers[compiler].defaults
  57. })
  58. return defaults
  59. }
  60. md.storage.bug = (res) => {
  61. // reload extension bug
  62. chrome.permissions.getAll((permissions) => {
  63. var origins = Object.keys(res.origins || {})
  64. chrome.permissions.remove({
  65. origins: permissions.origins
  66. .filter((origin) => origins.indexOf(origin.slice(0, -2)) === -1)
  67. })
  68. })
  69. }
  70. md.storage.migrations = (state) => {
  71. // v3.6 -> v3.7
  72. if (typeof state.origins['file://'] === 'object') {
  73. state.origins['file://'].csp = false
  74. }
  75. if (typeof state.theme === 'string') {
  76. state.theme = {
  77. name: state.theme,
  78. url: chrome.runtime.getURL(`/themes/${state.theme}.css`)
  79. }
  80. }
  81. if (state.themes === undefined) {
  82. state.themes = []
  83. }
  84. if (state.marked.tables !== undefined) {
  85. delete state.marked.tables
  86. }
  87. // v3.9 -> v4.0
  88. if (state.remark.commonmark !== undefined) {
  89. delete state.remark.commonmark
  90. }
  91. if (state.remark.pedantic !== undefined) {
  92. delete state.remark.pedantic
  93. }
  94. if (state.content.mermaid === undefined) {
  95. state.content.mermaid = false
  96. }
  97. if (state.themes === undefined || state.themes instanceof Array) {
  98. state.themes = {wide: false}
  99. }
  100. if (typeof state.theme === 'object') {
  101. state.theme = state.theme.name
  102. }
  103. // v4.0 -> v5.0
  104. Object.keys(state.origins).forEach((origin) => {
  105. state.origins[origin].csp = false
  106. state.origins[origin].encoding = ''
  107. })
  108. if (state.marked.smartLists !== undefined) {
  109. delete state.marked.smartLists
  110. }
  111. if (state.content.syntax === undefined) {
  112. state.content.syntax = true
  113. }
  114. if (state.themes.wide !== undefined) {
  115. if (state.themes.wide) {
  116. state.themes.width = 'full'
  117. }
  118. delete state.themes.wide
  119. }
  120. if (state.icon === undefined) {
  121. state.icon = false
  122. }
  123. if (state.remark.footnotes !== undefined) {
  124. delete state.remark.footnotes
  125. }
  126. }