storage.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. async function set (options) {
  7. await 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: 'markdown-it',
  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: 'default',
  53. theme: 'light',
  54. },
  55. custom: {
  56. theme: '',
  57. color: 'auto',
  58. }
  59. }
  60. Object.keys(compilers).forEach((compiler) => {
  61. defaults[compiler] = compilers[compiler].defaults
  62. })
  63. return defaults
  64. }
  65. md.storage.bug = (res) => {
  66. // reload extension bug
  67. chrome.permissions.getAll((permissions) => {
  68. var origins = Object.keys(res.origins || {})
  69. chrome.permissions.remove({
  70. origins: permissions.origins
  71. .filter((origin) => origins.indexOf(origin.slice(0, -2)) === -1)
  72. })
  73. })
  74. }
  75. md.storage.migrations = (state) => {
  76. // v3.6 -> v3.7
  77. if (typeof state.origins['file://'] === 'object') {
  78. state.origins['file://'].csp = false
  79. }
  80. if (typeof state.theme === 'string') {
  81. state.theme = {
  82. name: state.theme,
  83. url: chrome.runtime.getURL(`/themes/${state.theme}.css`)
  84. }
  85. }
  86. if (state.themes === undefined) {
  87. state.themes = []
  88. }
  89. if (state.marked.tables !== undefined) {
  90. delete state.marked.tables
  91. }
  92. // v3.9 -> v4.0
  93. if (state.remark.commonmark !== undefined) {
  94. delete state.remark.commonmark
  95. }
  96. if (state.remark.pedantic !== undefined) {
  97. delete state.remark.pedantic
  98. }
  99. if (state.content.mermaid === undefined) {
  100. state.content.mermaid = false
  101. }
  102. if (state.themes === undefined || state.themes instanceof Array) {
  103. state.themes = {wide: false}
  104. }
  105. if (typeof state.theme === 'object') {
  106. state.theme = state.theme.name
  107. }
  108. // v4.0 -> v5.0
  109. Object.keys(state.origins).forEach((origin) => {
  110. state.origins[origin].csp = false
  111. state.origins[origin].encoding = ''
  112. })
  113. if (state.marked.smartLists !== undefined) {
  114. delete state.marked.smartLists
  115. }
  116. if (state.content.syntax === undefined) {
  117. state.content.syntax = true
  118. }
  119. if (state.themes.wide !== undefined) {
  120. if (state.themes.wide) {
  121. state.themes.width = 'full'
  122. }
  123. delete state.themes.wide
  124. }
  125. if (state.icon === undefined) {
  126. state.icon = false
  127. }
  128. if (state.remark.footnotes !== undefined) {
  129. delete state.remark.footnotes
  130. }
  131. // v5.0 -> v5.1
  132. if (state.header !== null) {
  133. Object.keys(state.origins).forEach((origin) => {
  134. state.origins[origin].header = true
  135. state.origins[origin].path = true
  136. delete state.origins[origin].csp
  137. delete state.origins[origin].encoding
  138. })
  139. state.header = null
  140. }
  141. if (state.content.scroll !== undefined) {
  142. delete state.content.scroll
  143. }
  144. if (state.settings === undefined) {
  145. state.settings = {
  146. icon: state.icon === true ? 'light' : 'dark',
  147. theme: 'light'
  148. }
  149. }
  150. // v5.1 -> v5.2
  151. if (state['markdown-it'].abbr === undefined) {
  152. Object.assign(state['markdown-it'], {
  153. abbr: false,
  154. attrs: false,
  155. cjk: false,
  156. deflist: false,
  157. footnote: false,
  158. ins: false,
  159. mark: false,
  160. sub: false,
  161. sup: false,
  162. tasklists: false,
  163. })
  164. }
  165. if (state.marked.linkify === undefined) {
  166. Object.assign(state.marked, {
  167. linkify: true,
  168. })
  169. delete state.marked.sanitize
  170. }
  171. // v5.2 -> v5.3
  172. if (state.custom === undefined) {
  173. state.custom = {
  174. theme: '',
  175. color: 'auto'
  176. }
  177. }
  178. }