storage.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // in case of new providers from the compilers branch
  15. Object.keys(compilers).forEach((compiler) => {
  16. if (!state[compiler]) {
  17. state[compiler] = compilers[compiler].defaults
  18. }
  19. })
  20. // mutate
  21. md.storage.migrations(state)
  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. match,
  33. themes: {
  34. width: 'auto',
  35. },
  36. content: {
  37. autoreload: false,
  38. emoji: false,
  39. mathjax: false,
  40. mermaid: false,
  41. syntax: true,
  42. toc: false,
  43. },
  44. origins: {
  45. 'file://': {
  46. header: true,
  47. path: true,
  48. match,
  49. }
  50. },
  51. settings: {
  52. icon: 'dark',
  53. theme: 'light',
  54. }
  55. }
  56. Object.keys(compilers).forEach((compiler) => {
  57. defaults[compiler] = compilers[compiler].defaults
  58. })
  59. return defaults
  60. }
  61. md.storage.bug = (res) => {
  62. // reload extension bug
  63. chrome.permissions.getAll((permissions) => {
  64. var origins = Object.keys(res.origins || {})
  65. chrome.permissions.remove({
  66. origins: permissions.origins
  67. .filter((origin) => origins.indexOf(origin.slice(0, -2)) === -1)
  68. })
  69. })
  70. }
  71. md.storage.migrations = (state) => {
  72. // v3.6 -> v3.7
  73. if (typeof state.origins['file://'] === 'object') {
  74. state.origins['file://'].csp = false
  75. }
  76. if (typeof state.theme === 'string') {
  77. state.theme = {
  78. name: state.theme,
  79. url: chrome.runtime.getURL(`/themes/${state.theme}.css`)
  80. }
  81. }
  82. if (state.themes === undefined) {
  83. state.themes = []
  84. }
  85. if (state.marked.tables !== undefined) {
  86. delete state.marked.tables
  87. }
  88. // v3.9 -> v4.0
  89. if (state.remark.commonmark !== undefined) {
  90. delete state.remark.commonmark
  91. }
  92. if (state.remark.pedantic !== undefined) {
  93. delete state.remark.pedantic
  94. }
  95. if (state.content.mermaid === undefined) {
  96. state.content.mermaid = false
  97. }
  98. if (state.themes === undefined || state.themes instanceof Array) {
  99. state.themes = {wide: false}
  100. }
  101. if (typeof state.theme === 'object') {
  102. state.theme = state.theme.name
  103. }
  104. // v4.0 -> v5.0
  105. Object.keys(state.origins).forEach((origin) => {
  106. state.origins[origin].csp = false
  107. state.origins[origin].encoding = ''
  108. })
  109. if (state.marked.smartLists !== undefined) {
  110. delete state.marked.smartLists
  111. }
  112. if (state.content.syntax === undefined) {
  113. state.content.syntax = true
  114. }
  115. if (state.themes.wide !== undefined) {
  116. if (state.themes.wide) {
  117. state.themes.width = 'full'
  118. }
  119. delete state.themes.wide
  120. }
  121. if (state.icon === undefined) {
  122. state.icon = false
  123. }
  124. if (state.remark.footnotes !== undefined) {
  125. delete state.remark.footnotes
  126. }
  127. // v5.0 -> v5.1
  128. if (state.header !== null) {
  129. Object.keys(state.origins).forEach((origin) => {
  130. state.origins[origin].header = true
  131. state.origins[origin].path = true
  132. delete state.origins[origin].csp
  133. delete state.origins[origin].encoding
  134. })
  135. state.header = null
  136. }
  137. if (state.content.scroll !== undefined) {
  138. delete state.content.scroll
  139. }
  140. if (state.settings === undefined) {
  141. state.settings = {
  142. icon: state.icon === true ? 'light' : 'dark',
  143. theme: 'light'
  144. }
  145. }
  146. // v5.1 -> v5.2
  147. if (state['markdown-it'].abbr === undefined) {
  148. Object.assign(state['markdown-it'], {
  149. abbr: false,
  150. attrs: false,
  151. cjk: false,
  152. deflist: false,
  153. footnote: false,
  154. ins: false,
  155. mark: false,
  156. sub: false,
  157. sup: false,
  158. tasklists: false,
  159. })
  160. }
  161. if (state.marked.linkify === undefined) {
  162. Object.assign(state.marked, {
  163. linkify: true,
  164. })
  165. delete state.marked.sanitize
  166. }
  167. }